How to Retrieve Data from a Combo Box (Drop-down List) in a Qt Widget Application in C++


C++


In this article, we show how to retrieve the selected value from a combo box (drop-down list) in a Qt widget application in C++.

A combo box is an element that is the equivalent of a drop-down list in HTML.

This is shown below.



So a combo box is an element in which a program can obtain a selection that user chooses. This could be for anything such as the food the user desires or the type of credit card he/she wants to pay with.

So how do we place a combo box element in a program and retrieve the selection which a user enters?

This is what we go over in this program.

So to place a combo box element in a program, we place the following in the "widgets.ui" file.



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

So now that we have this comboBox element 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 comboBox 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 integer named selectionvalue. This will be used in our application to store the time the user selects.

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

In this file, we retrieve the selected value from the comboBox 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 value from the comboBox element and store it into the selectionvalue 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 from the comboBox element upon the pressing the pushbutton.

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

The retrieval of the value of the comboBox element is done when the push button is clicked.

So inside of this clicked function, we specify the variable, selectionvalue, and set it equal to, ui->comboBox->currentText();

ui references the user interface.

comboBox is the name attribute of the comboBox element we are targeting.

currentText() is the value specified in the comboBox element.

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

Let's say, you were to create another comboBox element. Say you need to create a program that retrieves multiple values from a user, such as one being the food choice selection and another being the credit card selection used to pay for the order.

So in order to now deal with 2 comboBox elements, you need to add another comboBox element.

If you use the Design element of the Qt software to add another comboBox element, this will be the code you will add.



After this, you need to declare another variable in the "widget.h" file in order to store the value of the second comboBox element. So add, int selectionvalue2, to the "widget.h" file under the private keyword.

Then after that, you need to modify the "widget.ui" file to save the value from the second comboBox element to the variable, selectionvalue2.

This is shown below.



So now we've retrieved both values from the 2 comboBox elements.

Since the name attribute of the second element is "comboBox_2", we use this to retrieve the value from the second comboBox element.

Again, that is the name that the software automatically generates if you don't change it. You can change that name. You just have to make sure that when referencing the element, you refer to it with the changed name.

And this is how to retrieve the selected value from a combo box element (drop-down list) in a Qt widget in C++.


Related Resources

How to Write to a File in C++

HTML Comment Box is loading comments...