How to Retrieve the Selected Check Boxes from a Check Box Group in a Qt Widget Application in C++


C++


In this article, we show how to retrieve the selected check boxes from a check box group in a Qt widget application in C++.

This is shown below.

Room service
Meals
Laundry service
Internet service


So check boxes are elements in which a program can obtain either a single selection or multiple selection that a user chooses. The difference between a check box selection and a radio button selection is that a check box selection can select multiple options, while in a radio button group, only one option can be selected.

So, say, for example, in the example above is a hotel form for booking a room. A user can decide if they want to add in room service, meals delivered to their rookm, laundry service, and internet service, as a package to their hotel stay.

So how do we place check boxes in a program and retrieve the selection(s) which a user enters? This may also include no selections at all, such as in the case where a user wants to save money and doesn't want room service, meals, laundry service, or internet service.

This is what we go over in this program.

So to place check boxes in a program, we place the following in the "widgets.ui" file.



So in order to understand this check box widget, you can see that the widget is of class "QCheckBox". Every check box element is of this class in a Qt widget application. The name, however, can change to identify the specific check box you are referring to. By default, the first check box element in an application will be "checkBox". Each additional check box element will be enumerated. For example, the second check box element in an application will have the name, "checkBox_2". The third will have the name, "checkBox_3", etc. Of course, these name values can be changed manually. Each has a unique name to be able to reference each specific check box element if you have more than 1 in an application.

So now that we have these check boxes in our "widget.ui" application, we need to go to our next page.

Next we go to the "widget.h" header file.

Since we have to create a variable that stores the value from the check box element, we need to declare a variable that will be used to store this value. The declaration of variables goes into the "widget.h" header file under the private keyword.

So the full contents of the "widget.h" file is shown below.



So you see that under the private keyword, we have an QVector of type QString named checkboxoutcome. This will be used in our application to store the options of the check boxes that the user selects.

We create a vector instead of a string because multiple check boxes can be selected at the same time. Therefore, a vector is more than likely the best way to be able to store all of the values of the check boxes.

So now, the last file we go to is the "widget.cpp" file.

In this file, we retrieve the selected value(s) from the check box element that the user enters and we output this value in the application output using the qDebug() function.

The contents of this file is shown below.

In this application, we retrieve the selected values from the check boxes and store it into the checkboxoutcome variable we created in the "widget.h" file.

This application assumes that you have a push button in your application, because it retrieves the value(s) from the check boxes upon the pressing the pushbutton.

So in our Qt widget application, we have the check boxes and a pushbutton whose name is "pushButton".

The retrieval of the value(s) of the check boxes is done when the push button is clicked.

ui references the user interface.

checkBox is the name attribute of the check box element we are targeting, followed by checkBox_2, checkBox_3, and checkBox_4

currentText() is the value specified in the check box element.

We use an if statement to check if each check box is selected by using the isChecked() function. If it is checked, then we will append the value of this check box to the checkboxoutcome variable, which is a vector which stores the value of all selected check boxes. Since multiple check boxes can be selected, we use multiple if statements and not if-else statements.

Now to show you what value we get, we use the qDebug() function to output the data from the check boxes.

We create a variable, vectorsize, which we set equal to the size of the checkboxoutcome variable. The reason we do this is that we want to iterate through this variable to see what choices were selected.

We then create a for loop to actually iterate through the selected check boxes.

We use the qDebug() function to output the selected check boxes.

And this is how to retrieve the selected check boxes from a check box group in a Qt widget in C++.


Related Resources

How to Write to a File in C++

HTML Comment Box is loading comments...