C++ Programming

Qt Creator: Signal Widgets and Slot Widgets with Example programs

Creating own Slots Widgets in Qt Creator C++:

Often you will want to add your own slots to your widgets. Take a look at the following example code.

QObject :: connect (button, SIGNAL (clicked ()),

label, SLOT (setText (“new text”)));

allpcb circuit

Here we have tried a signal-slot link. The aim of these two lines of code is when the button is pressed (e.g QPushButton) the text of the label (QLabel) is changed. The button uses the clicked() signal and the label uses the slot setText(). clicked() does not give any arguments to the slot setText(). To implement the following example, let’s direct our new class MyWidgets from QWidget.


Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Compared to the example as explained in my previous article, in which we used our own signals and slots, You won’t find anything new here either, except that we’re now out of the class QWidget (line 9) – instead of QObject – and here only one slot (line 18 and 19) is used. The definition of the constructor and the slot event function can be found in the following mywidgets.cpp source file:

First, as usual, the individual widgets such as buttons (QPushButton) and label (QLabel) are added to the vertical box (QVBoxLayout). Then (lines 11 and 12) the signal-slot links are set up. Particular attention should be paid to the slot in line 11. This is no longer the one provided by QLabel Slot setText(), but our self-defined slot without arguments, You can find the implementation in lines 14 to 16. If you now click on the button with the label »old text« in the example, it will Signal clicked() triggered. However, this signal is not connected to the label, but with the window or QWidget. Therefore you will find here too the pointer. Only with the slot event function MyWidgets :: setText() the text label is changed.




Now create the main.cpp file and paste the below code:

output:

widgets

widgets


Creating own Signal Widgets:

Of course, I want to demonstrate something similar to you with a signal. Therefore we want to add the signal clicked(int) to the QPushButton class. Originally, this class only had the clicked() signal without an argument. We want to add a new clicked(int) with the argument, which when a button is pressed, a signal with an identification number(ID) of the button is send.

First of all, the MyClass.h class, which we derive from the QPushButton class:

In addition to a new signal that is defined herein lines 17 to 19, we overwrite the slot click() defined in the QAbstractButton class. The click() slot literally performs a click. Of course, you need still no definition of the signal, as this, as already described, is the Meta-Object Compiler takes over for us. To better understand the process, you need the implementation of the MyClass.cpp class:


The two connect() in the constructors are also noticeable (line 04 and 07). With this we connect the old (or call it the real one) clicked() – Signal with its own home-made slot click() of the MyClass.cpp class. The decisive factor here is that the slot element function really is by MyClass.cpp (lines 9 to 11) is executed. Would be the super method Using QPushButton :: click() would result in an infinite recursion, because QPushButton :: click() triggers the clicked() signal again. As soon as the slot MyClass :: click() was triggered, you can send the new signal with the identification number Send (ID) (line 10). To make a stylish Qt program out of this example, let’s do this Another class MyWindow (derived from QWidget) with our own buttons (MyButton) and a slot that deals with the output on the Screen cares. Here is the header file of our class mywindow.h:

You can find our button in line 10 MyButton with its own implemented Signal, again in lines 11 to 13 we declare a separate slot for the class MyWindow. The slot member function is basically a simple one Message box (QMessageBox), which contains the identification number of the signal clicked(int) of our buttons. To do this, the implementation of the MyWindow.cpp class:

In line 12, our new clicked(int) signal is sent to the output slot myMessageBox connected. So as soon as our new signal arrives, a Message box with the ID of the button is displayed.



Finally, our main.cpp class code:

Output:

widgets

widgets

widgets

widgets

widgets

 

 

 

Related Articles

One Comment

  1. That is really attention-grabbing, You are a very
    skilled blogger. I have joined your feed and look ahead to seeking more of your magnificent post.
    Also, I’ve shared your site in my social networks

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button