当前位置:网站首页>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
边栏推荐
- Special lecture 2 dynamic planning learning experience (should be updated for a long time)
- 数组的介绍--Array
- JSP built-in object (implicit object)
- Basic music theory rhythm connection problem, very important
- 2W word detailed data Lake: concept, characteristics, architecture and cases
- JSP implicit object servlet object
- [June 29, 2022] examination summary
- Software engineering -- dental clinic -- demand acquisition
- Burp Suite-第五章 如何使用Burp Target
- Beauty naked chat for a while, naked chat over the crematorium!
猜你喜欢

关于期刊论文所涉及的一些概念汇编+期刊查询方法

苹果强硬新规:用第三方支付也要抽成,开发者亏大了!

NLP (natural language processing) natural language processing learning

Burp suite Chapter 7 how to use burp scanner
![[endnote] detailed explanation of document template layout syntax](/img/fd/2caf4ff846626411fe8468f870e66a.png)
[endnote] detailed explanation of document template layout syntax

The first ide overlord in the universe, replaced...

SPSS uses kmeans, two-stage clustering and RFM model to study the behavior law data of borrowers and lenders in P2P network finance

I am 35 years old.

OSPF总结

Introduction to arrays -- array
随机推荐
Basic music theory rhythm connection problem, very important
数组的介绍--Array
【EndNote】文献模板编排语法详解
日常一记(11)--word公式输入任意矩阵
Dev gridcontrol 捕获按键事件
2022-7-6 personal qualifying 3 competition experience
OSPF summary
Why don't you tell me what long polling is?
2022-024arts: Longest valid bracket
The most complete network: detailed explanation of six constraints of MySQL
mysql函数汇总之日期和时间函数
2022-024ARTS:最长有效括号
This is a picture
Team members participate in 2022 China multimedia conference
我,35岁了。
Burp Suite-第五章 如何使用Burp Target
Burp Suite - Chapter 1 burp suite installation and environment configuration
2022 / 7 / 16 exam summary
JSP built-in object (implicit object)
Burp suite Chapter 8 how to use burp intruder