Qt second step slot and signal (1) Click the button and pop up the window

Signals and slots are the core of Qt and are unique to Qt. The keywords of signals and slots in Qt are slots, signals, emit.
The specific signals and slots will be explained in detail in the next section, because they are worried that some readers will directly talk about them before they have understood the slots and signals, which will lead to higher thresholds and difficulty in reading. Therefore, the current article will Under the realization principle, click the button and pop up the window to feel the use of signals and slots.

Create a Qt Widget project, double-click mainwindow.ui under Forms, in the pop-up UI design panel widget selection area, drag Push Button to the form design panel: After
Insert picture description here
dragging is completed, the interface is as follows:
Insert picture description here
Right-click PushButton and select Go to slot :
Insert picture description here
Then pop up to the slot panel, select clicked() under QAstractButton:
Insert picture description here
this will jump to a method: the
Insert picture description here
above operation makes the button bind a method, this method can be called a slot, and the slot responds to the signal The function. The slot can associate the function with a signal. When the signal is transmitted, the slot function will respond and execute the slot function.
We just selected clicked() under QAstractButton under the slot panel, and clicked is the signal sent. clicked is the signal sent when the button is clicked.

In summary, we can know that a slot is a function, and when a signal is emitted, the bound slot will be executed.

We can write some code in the slot function to detect whether the function will respond when the PushButton is clicked.

We try to use a pop-up window as a manifestation of the slot function. You can introduce the header file of the pop-up window at the head of the mainwindow.cpp file:

#include <QMessageBox>

Add a QMessageBox pop-up window (QMessageBox is a pop-up window) in the slot function. The function prototype of QMessageBox is as follows:

static StandardButton QMessageBox::information ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton );
  • parent: the parent widget type is QWidget
  • title: the type of the pop-up window title is QString
  • text: the display content type is QString
  • buttons: There is pop a default button is OK if you want to display a NO OK and can be written as QMessageBox::Yes | QMessageBox::Notype StandardButton
  • defaultButton: The button NoButton selected by default is not selected, and the type is StandardButton

With the above description, we can add the following code to the slot function (fill in the value directly without assigning it in the parameter):

QMessageBox::information(NULL, "这是标题", "@1_bit", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);

The above code server component is NULL, the title value is "This is the title", and the content is "@_bit". The pop-up window has 2 buttons, one is Yes and the other is No. Yes is selected by default.

Click to run to see the effect:
Insert picture description here
readers may have coding problems when setting the displayed value, which causes some values ​​to be displayed incorrectly, which will be explained in the following chapters.
The screenshot of the complete code is as follows, the purple framed area is the code I added:
Insert picture description here

Guess you like

Origin blog.csdn.net/A757291228/article/details/107169021