参考 https://blog.csdn.net/Larry_Yanan/article/details/123518788
.ui文件如下
新建的ui文件,编译一下就会生成对应的 ui_xxx.h 文件,文件内就有对应的 namespace Ui 声明的变量,这个变量要在mainwindow.h中声明,然后在mainwindow.cpp中new出来,具体使用如下
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QWidget>
namespace Ui {
class MainWindow;
class page_pdo;
class page_param;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
Ui::page_param *ui_page_param;
Ui::page_pdo *ui_page_pdo;
QWidget *parent_page_pdo;
QWidget *parent_page_param;
QWidget *parent_active=nullptr;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_page_pdo.h"
#include "ui_page_param.h"
#include <QTimer>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
parent_page_pdo=new QWidget();
parent_page_param=new QWidget();
ui_page_param=new Ui::page_param;
ui_page_pdo=new Ui::page_pdo;
ui_page_param->setupUi(parent_page_param);
ui_page_pdo->setupUi(parent_page_pdo);
parent_page_pdo->setWindowTitle("parent_page_pdo");
parent_page_param->setWindowTitle("parent_page_param");
parent_page_param->setWindowFlags(Qt::Dialog | Qt::WindowStaysOnTopHint);//这种方式主界面还能编辑,加上主界面disable和定时监测,结合定时器监测可以实现警告提示
parent_page_pdo->setWindowFlags(Qt::Dialog | Qt::WindowStaysOnTopHint);
// parent_page_param->setWindowModality(Qt::ApplicationModal);//这种方式,主界面无法修改,可以实现基本功能,但是无法实现警告提示
// parent_page_pdo->setWindowModality(Qt::ApplicationModal);
connect(ui->btn_skip_page_param,&QPushButton::clicked,this,[=]() mutable{
if(parent_active)parent_active->hide();
parent_page_param->show();
parent_active=parent_page_param;
this->setEnabled(false);
//this->setWindowOpacity(0.5); // 设置窗口半透明
});
connect(ui->btn_skip_page_pdo,&QPushButton::clicked,this,[=]() mutable{
if(parent_active)parent_active->hide();
parent_page_pdo->show();
parent_active=parent_page_pdo;
this->setEnabled(false);
});
parent_active=nullptr;
connect(ui_page_pdo->btn_cancel,&QPushButton::clicked,this,[=]() mutable{ this->setEnabled(true);parent_active=nullptr;parent_page_pdo->hide();qDebug() <<ui_page_pdo->data_1->text(); });
connect(ui_page_pdo->btn_ok,&QPushButton::clicked,this,[=]() mutable{ this->setEnabled(true);parent_active=nullptr;parent_page_pdo->hide();qDebug() << ui_page_pdo->data_1->text(); });
connect(ui_page_param->btn_cancel,&QPushButton::clicked,this,[=]() mutable{ this->setEnabled(true);parent_active=nullptr;parent_page_param->hide();qDebug() << ui_page_param->data_1->text(); });
connect(ui_page_param->btn_ok,&QPushButton::clicked,this,[=]() mutable{ this->setEnabled(true);parent_active=nullptr;parent_page_param->hide();qDebug() << ui_page_param->data_1->text(); });
QTimer* timerSendAnimCAN = new QTimer(this);
connect(timerSendAnimCAN,&QTimer::timeout,this,[=]() mutable{
QWidget *activeWindow = QApplication::activeWindow();
if (activeWindow) { qDebug() << "Active window:" << activeWindow->windowTitle(); }
if(parent_active){
if (activeWindow==parent_active) {
parent_active->setStyleSheet("");
}
else {
QString res=QString("#%1{%2}").arg(parent_active->objectName()).arg("border:2 solid red;");
parent_active->setStyleSheet(res);
}
}
});
timerSendAnimCAN->setTimerType(Qt::PreciseTimer);
timerSendAnimCAN->start(100);
}
MainWindow::~MainWindow()
{
delete ui;
}
运行效果