How to Perform an Action When an Item Is Clicked in a Qt Widget Application in C++


In this article, we show how to perform an action when an item is clicked in a Qt widget application in C++. This could be an item such as a button or a date on a calendar, etc.

So a Qt widget is an application that uses the Qt framework to create graphical user interfaces (GUIs). GUIs allow for visual programs. If it weren't for GUI applications, software would have to be operated by means of command lines such as CLI software such as Command Prompt in windows.

Qt widgets are written in C++.

So Qt widgets allow us to create GUIs using the C++ language.

In this example, we create a GUI that has a single button.

So creating a GUI in a C++ application is a multi-file process, meaning you need to work with multiple files in order to do so.

The first thing we need to do is add a button to our GUI.

So in order to add a button to the GUI, we need to access the "widget.ui" in the forms directory of our Qt Project folder.

You can do this in code by adding the following in the "widget.ui" file.

This adds a button to the GUI.

Now that the button is added, we next need to add functionality to the button, so that when the button is clicked by a user, we can perform some action based on that click.

We add this functionality to the "widget.cpp" file.

The full code in the "widget.cpp" file is shown below.

This now adds functionality so that the button click actually has meaning. Otherwise, there would no underlying functionality to the button click.

So to add functionality to the button click, we add the line, void Widget::on_pushButton_clicked() { }. The action you want done goes in between the curly braces. Since the button has a name attribute, pushButton, this is why it appears in this line. Whatever the name attribute of the item you want clicked is goes after on_

When the button is clicked, we want to send debug statements to the Application Output. So we use the qDebug() command and input "User clicked the button"

Now when the button is clicked, this sends the debug statement, "User clicked the button" to the Application Output.

So now we've added functionality to an item in our GUI.

And this is not just for buttons. This goes for any item that has the on_clicked() functionality in Qt widgets. This also can be a Calender. When a date is clicked on a calendar, we can perform an action such as to display the date that the user selected from the calendar.

We'll do one more example now with a calendar this time instead of a button.

You can add a calendar in code by adding the following in the "widget.ui" file.

Now that we have the calendar on our GUI, we can add functionality to do something when the user clicks on a date on the calendar.

This is shown in the code below which goes into the "widgets.cpp" file.

This code will now show the date that the user selected on the Application Output.

And this is how to perform an action when an item is clicked in a Qt widget application in C++.

Related Resources

How to Write to a File in C++

HTML Comment Box is loading comments...