1、Show.pro工程文件
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp \
widget2.cpp
HEADERS += \
widget.h \
widget2.h
FORMS += \
widget.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
2、widget.h文件
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
3、widget2.h文件
#ifndef WIDGET2_H
#define WIDGET2_H
#include <QWidget>
class Widget2 : public QWidget
{
Q_OBJECT
public:
explicit Widget2(QWidget *parent = nullptr);
QWidget * w = new QWidget;
signals:
void start();
public slots:
void showOtherWindow(); //设置一个出现另外一个window的槽
void closeOtherWindow(); //关闭另外一个窗口
};
#endif // WIDGET2_H
4、main.cpp
#include "widget.h"
#include"widget2.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
5、widget.cpp
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//QWidget *w = new QWidget; //这里要用QWidget不能用Widegt不然会无限循环
Widget2 *w = new Widget2;
QPushButton * btn1 = new QPushButton("open",this); //设置open按钮
QPushButton * btn2 = new QPushButton("close",this); //设置close按钮
this->setFixedSize(600,800); //不可拖拉界面大小
btn1->move(200,200); //移动按钮位置
btn2->move(200,400);
btn1->resize(200,100); //改变按钮大小
btn2->resize(200,100);
connect(btn1,&QPushButton::clicked,w,&Widget2::showOtherWindow);
connect(btn2,&QPushButton::clicked,w,&Widget2::closeOtherWindow);
}
Widget::~Widget()
{
delete ui;
}
6、widget2.cpp
#include "widget2.h"
Widget2::Widget2(QWidget *parent) : QWidget(parent)
{
w->setFixedSize(600,800);
}
void Widget2::showOtherWindow()
{
w->show();
}
void Widget2::closeOtherWindow()
{
w->close();
}
文章来源地址https://www.toymoban.com/news/detail-522742.html
文章来源:https://www.toymoban.com/news/detail-522742.html
到了这里,关于四、QT设置两个开关,可以控制另外一个页面的开启和关闭的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!