当前位置:网站首页>QT note 2
QT note 2
2022-07-26 08:22:00 【Eight Liang x】
Take the book back , Children's shoes that have no foundation or can't understand can be taken a quick look QT note 1_ eight two x The blog of -CSDN Blog
This time we mainly realize the switching function between the two windows
1 New window
① Right click on the project folder , choice “ Add new file ” or Add New...( Different versions are displayed differently )

② choice C++ -> C++ Class, And then click choose

③ there Name I could just write , I wrote it SubWidget, however Base class Be sure to choose QWidget

④ Next step , complete , There will be a new window . Engineering documents will also appear in the following documents :

2 Switch between two windows
After creating a new window , Our goal has become : Click a button in the main window , Sub windows can be displayed , The main window is hidden ; Click the button of the sub window , The main window shows , The child window is hidden
2.1 main window -> child window
① Yes mainwidget.h operation . First , Declare a new button b3; secondly , Declare the existence of a window , Tell the main window : You have a little brother ; Last , Add a custom slot function changeWin() Used to perform the task of switching windows .
#ifndef MAINWIDGET_H
#define MAINWIDGET_H
#include <QWidget>
#include <QPushButton>
#include "subwidget.h" // Sub window header file
class MainWidget : public QWidget
{
Q_OBJECT
public:
MainWidget(QWidget *parent = 0);
~MainWidget();
void myslot();
void changeWin();// Switch the slot function of the window
private:
QPushButton b1;
QPushButton *b2;
QPushButton b3; // new b3 Button
SubWidget w1; // Declare child windows w1
};
#endif // MAINWIDGET_H
② Yes mainwidget.cpp operation .
First define changeWin function , Code + Explain the following
void MainWidget::changeWin()
{
// The sub window shows
w1.show();
// This window hides
this->hide();
}secondly , initialization b3, Then write the signal and slot function , Code + Explain the following
b3.setParent(this); // Set up b3 Parent class of
b3.setText(" Switch to sub window "); // Set up b3 The text of
b3.move(50, 50); // Set up b3 The location of
connect(&b3, &QPushButton::released, this, &MainWidget::changeWin);// When b3 When releasing , Switch windows Only this and nothing more , We can call the sub window through the main window .
2.2 child window -> main window
It is much more troublesome for a child window to call the main window , Like the boss can say : petty thief , Get me an express . You can't tell the boss : Lao Li , Buy me a pack of cigarettes . You can only say : Boss , Do you think this plan is feasible , The boss will deal with it if he is interested , If you are not interested, just ignore it . There is a truth about the relationship between the sub window and the main window .
The main window can call the sub window freely , But the sub window needs to call the main window and send the application signal , The main window can choose to process or not .
① Yes subwidget.h To operate . First, define a button b, Then define a slot function to send signals , Finally, define a signal . The code is as follows
#ifndef SUBWIDGET_H
#define SUBWIDGET_H
#include <QWidget>
#include <QPushButton>
class SubWidget : public QWidget
{
Q_OBJECT
public:
explicit SubWidget(QWidget *parent = nullptr);
void sendSlot(); // Slot function for sending signal
signals:
/* The signal must have signals Keyword to declare
* The signal has no return value , But you can have parameters
* A signal is a declaration of a function , Just declare , There is no need to define
* Use :emit mySignal();
*/
void mySignal(); // The transmitted signal
public slots:
private:
QPushButton b; // Declared button
};
#endif // SUBWIDGET_H② Yes subwidget.cpp To operate . First initialize the button , Then define the signal and slot , Finally, the slot function sends a signal .
#include "subwidget.h"
SubWidget::SubWidget(QWidget *parent) : QWidget(parent)
{
this->setWindowTitle("Bro"); // Set the sub window name
b.setParent(this); // Set the position of the sub window
b.setText(" Switch to the main window "); // Set button text
connect(&b, &QPushButton::clicked, this, &SubWidget::sendSlot); // When the button is clicked , Trigger sendslot function
resize(400, 300); // Control the page size to 400*300
}
void SubWidget::sendSlot()
{
emit mySignal(); // send out mySignal The signal ; Send using emit
}The next step is to see how the main window handles this signal
③ stay mainwidget.h Add a declaration of signal processing function in .
public:
void dealSub();④ stay mainwidget.cpp Signal processing in . First, in the mainwidget Add such a paragraph to the function :
connect(&w1, &SubWidget::mySignal, this, &MainWidget::dealSub);
// When child window w1 Sending signal mySignal when , current window (this) perform dealSub⑤ Definition dealSub
void MainWidget::dealSub()
{
// The child window is hidden
w1.hide();
// The main window shows
this->show();
}Only this and nothing more , The function of calling each other between the main window and the sub window is realized .
Reference course : Black horse programmer Qt( The first part )_ Bili, Bili _bilibili
边栏推荐
猜你喜欢

数组的介绍--Array

咱就是来聊聊并发编程的三大核心问题。

mysql函数汇总之日期和时间函数

Special lecture 2 dynamic planning learning experience (should be updated for a long time)

JSP implicit object servlet object

BGP选路原则

Burp suite Chapter 7 how to use burp scanner

Basic introduction of JDBC

宇宙第一 IDE 霸主,换人了。。。
分享高压超低噪声LDO测试结果(High Voltage Ultra-low Noise LDO)
随机推荐
2022/7/9 exam summary
Shell programming
[fastjson1.2.24 deserialization vulnerability principle code analysis]
JSP implicit object -- scope
2022/7/1
If the thread crashes, why doesn't it cause the JVM to crash? What about the main thread?
第四天作业
I am 35 years old.
matplotlib学习笔记
Burp Suite-第七章 如何使用Burp Scanner
[June 29, 2022] examination summary
Date and time function of MySQL function summary
2022/7/7 exam summary
第三天作业
Burp suite Chapter 9 how to use burp repeater
2022-7-8 personal qualifying 5 competition experience (supplementary)
2022-7-4 personal qualifying 1 competition experience
【EndNote】文献类型与文献类型缩写汇编
BGP选路原则
监控用户积分变化的两种方式