How to Add or Delete an Item in a List Widget in a Qt Widget Application in C++


C++


In this article, we show how to add or delete an item in a list widget in a Qt widget application in C++.

A list widget is a widget that lists various items.

This is shown below.

A list widget in a Qt widget application

The list widget allows us to have a list of various items. For example, in an operating system, this may represent various environment variables, in which we can add or delete or select various environment variables.

So how can we create a list widget in C++ and add functionality to it so that we can add new items or delete existing items?

This is what we go over in this program.

So to place a list widget in a program, we place the following in the "widgets.ui" file.



So in order to understand this list widget, you can see that the widget is of class "QListWidget". The items that make up the list widget are within the item tags. We have have 4 items within this list widget.

The next thing we need are push buttons, because these buttons will be used to control the 'Add Item' feature and the 'Delete Item' feature of the list widget.

The code to add into the "widgets.ui" file, in addition to the above code, is the following.



So now we have our push buttons which will control the 'Add Item' and 'Delete Item' features for the list widget.

So now that we have this list widget 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 item, 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 private slotskeyword, we have two functions. These two functions allow us to add or delete items in the list widget.

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

In this file, we perform the function of adding items and deleting items in the list widget.

The contents of this file is shown below.

In this application, we perform the task of adding a new item to the list widget and deleting an item from the widget.

This application works upon the push buttons being clicked. The 'Add Item' push button will add an item called 'New Item' to the list widget. The 'Delete Item' will delete the selected item from the list widget.

We'll now first go over adding an item to a list widget.

ui references the user interface.

listWidget is the name attribute of the list widget, which references it.

addItem() is a function that allows us to add an item to the list widget. The text you pass into it is the name of the added item. In this case, the item added will be called "New Item".

With this functionality, you can add as many new items as you want. This is kind of how it works when adding environment variable in a Windows operating system.

Now we go to deleting an item from a list widget.

Deleting an item uses the takeItem() function.

To delete the current item selected, we need to know the current row of the selected item. This is what the function takes as its parameter. To find the current row, we use the statement, ui->listWidget->currentRow()

This deletes the current selected row.

And this is how to add an item or delete an item 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...