How to Retrieve the Selected Item(s) in a List Widget in a Qt Widget Application in C++


C++


In this article, we show how to retrieve the selected item(s) in a list widget in a Qt widget application in C++.

A list widget is a widget that lists various items, such as that shown below.

Selecting multiple items in a list widget in a Qt widget application

By default, only one item can be selected at a time.

However, we can modify this so that multiple items can be selected at once.

We will first go over how to retrieve a single selected item, which is all you can select by default. Afteward, we will show how to retrieve multiple selected items when the default is changed.

Retrieving a Single Selected Item

So how do we retrieve a single selected item from a list widget in a Qt widget application in C++?

It turns out that we can just create a String variable and retrieve the value from the list widget's currentItem() attribute.

So we create a String variable called selectedvalue in the "widget.h" file. We will use this variable to store the value of the selected item in the "widget.cpp" file.

The full contents of the "widget.cpp" file is shown below.



So let's now go over this code.

So we create a function, on_SelectItemsButton_clicked(), that is triggered when the 'Select Items' button is clicked.

We take the variable, selectedvalue, that we created and set it equal to, selectedvalue= ui->listWidget->currentItem()->text();'

This returns to us the text of the item that is selected (Item 1, Item 2, Item 3, Item 4).

We then output this value using the qDebug() function.

Just to show how you can use if statements to check which item was selected, we do this, which you can write in your own custom code. In this example, we just output the selected value with the qDebug() function.

And this is how we can retrieve the selected item of a list widget.

However, single selection, though the default of list widgets, isn't the only type of selection. We can set the selection mode of a list weidget to allow for multiselection, or the selection of multiple items at the same time.

We can add the functionality to a list widget to be able to select multiple items simultaneously.

Selecting multiple items can allow us to work with multiple things at the same time.

So in order to do this, you simply must add the following the statement, ui->listWidget->setSelectionMode(QAbstractItemView::MultiSelection);, to the "widget.cpp" file.

The contents of this file is shown below.



This statement goes underneath, ui->listWidget->setSelectionMode(QAbstractItemView::MultiSelection);

This then allows us to select multiple items simultaneously in a list widget.

Now how do we retrieve what items were selected?

We can do this by adding the following function to the "widgets.cpp" file. This is shown in the following full code for the "widgets.cpp" file.



In this code, we add a function, on_SelectItemsButton_clicked(), which is triggered when the 'Select Items' button is clicked.

In order to get all of the elements, we need to create a list, which stores these values. This is what we do in the line, QList list= ui->listWidget->selectedItems();

This list is called list. This takes all of the selected items from the list widget whose name attribute is listWidget.

All of the selected items are now stored in this list.

To display these elements, we use a for loop to iterate through this list.

We then use the qDebug() function to show the selected items.

Just to show you how you can separate each selected item, you can set up an if statement that checks to see if an item was selected. We have if statements for each of the items in our list widget to check if each is selected.

And this is how to retrieve the selected items in a list widget in a Qt widget application in C++.


Related Resources

How to Write to a File in C++

HTML Comment Box is loading comments...