当前位置:网站首页>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
边栏推荐
- [fastjson1.2.24 deserialization vulnerability principle code analysis]
- 全网最全:Mysql六种约束详解
- vscode国内的镜像服务器加速
- 【EndNote】文献类型与文献类型缩写汇编
- Oracle 常用函数
- Special Lecture 3 number theory + game theory learning experience (should be updated for a long time)
- Data validation typeerror: qiao Validate is not a function
- 2022-07-14 group 5 Gu Xiangquan's learning notes day07
- 外卖小哥,才是这个社会最大的托底
- This is a picture
猜你喜欢

matplotlib学习笔记

Matplotlib learning notes

BGP routing principle

NLP (natural language processing) natural language processing learning

BGP的基本配置

How WPS sets page headers page by page

Recurrence of strtus2 historical vulnerability

【时间复杂度空间复杂度】

2022-07-14 group 5 Gu Xiangquan's learning notes day07

Burp Suite-第三章 如何使用Burp Suite代理
随机推荐
2w字详解数据湖:概念、特征、架构与案例
Exam summary on July 15, 2022
Burp suite Chapter 8 how to use burp intruder
BGP --- 边界网关协议
Introduction to arrays -- array
分享高压超低噪声LDO测试结果(High Voltage Ultra-low Noise LDO)
I am 35 years old.
Vscode utility shortcut
Burp Suite-第二章 Burp Suite代理和浏览器设置
2022-07-14 group 5 Gu Xiangquan's learning notes day07
【EndNote】文献模板编排语法详解
[June 29, 2022] examination summary
Bee guitar score high octave and low octave
C# WinForm中PreviewKeyDown、KeyDown、KeyPress、KeyUp区别与联系
JSP built-in object (implicit object)
Burp Suite-第五章 如何使用Burp Target
2022/7/9 exam summary
Shell第二天作业
On some concepts involved in journal papers compilation + journal query methods
Flex three column layout