QT5 learning record (a)

Learning Environment: Windows 10 + QT5.13 + QT Creater4.9.1 ( 2019-08-10 22:02:30 )

1. Basic project creation

  General operation creation screen, select QDialog, MainWindow, QWidget three types. Alternatively direct ui create the corresponding file can be used to add a control code to create the edit mode, may be added by dragging design mode; properties of the control codes can be modified in edit mode, you can also design mode by modifying the property bar; each control through its unique objectName looking for, so be careful to set objectName control.

2. common and important operation

  QT-specific signal (signal) and a slot (slot): signal associated sender, recipient associated slot, both have a function corresponding to the operation; QT already carrying some signal and slot function, the user can directly use; If the signal of the user-defined signal, using the keyword emit signals; and a slot signal to be bound together to achieve normal transmission and reception, this connection code execution connect function may be used, may be designed in the Edit mode signal / slot mode via line binding function, and ultimately code signal and slot (if QT signal and slot comes, as the case may be the operation thereof will be omitted).

3. The practice exercises

  New two windows (here select the MainWindow and Dialog), choose to create a new ui file. Two on each screen QPushButton Add button controls, the main interface to achieve the jump off from the interface and to realize a pop-up from and return to the main screen interface buttons. Dialog declare an instance of a class in the mainwindow.h:

1 private:
2     Ui::MainWindow *ui;
3     Dialog *sub;

  In mainwindow.cpp, create a constructor from the main interface instance sub interface, and is connected to two pictures (signal slot and binding), create an instance must first be bound:

1 MainWindow::MainWindow(QWidget *parent) :
2     QMainWindow(parent),
3     ui(new Ui::MainWindow)
4 {
5     ui->setupUi(this);
6     sub = new Dialog(this);
7     connect(sub,SIGNAL(mysignal()),this,SLOT(re_show()));
8 }

  In sub-interface dialog.h statement in a signal:

1 public:
2     explicit Dialog(QWidget *parent = nullptr);
3     ~Dialog();
4 
5 signals:
6     void mysignal();

  In sub-cpp file interface, dialog.cpp realize sending the signal, sent using the emit keyword:

1 void Dialog::on_back_clicked()
2 {
3     this->hide();
4     emit mysignal();
5 }

 4. Summary

  The record from the main signal and slot in the form of QT understand and use basic learning two if by signal interface and slot together.

 

  Each use of a control, the need to contain the corresponding class in! ! ! eg. #include <QPushButton> // class contains buttons

/ *************************************** be continued ******* *********************************** /

Guess you like

Origin www.cnblogs.com/niu-li/p/11330607.html