环境:Win10-x64+VS2015+Qt5.9.7
首先创建一个Qt GUI Application,工程名为tabWidget。
tabWidget.h
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include "ui_tabWidget.h"
#pragma execution_character_set("utf-8")
class tabWidget : public QMainWindow
{
Q_OBJECT
public:
tabWidget(QWidget *parent = Q_NULLPTR);
private:
Ui::tabWidgetClass ui;
QPushButton *startGrabbingPushButton;
QPushButton *stopGrabbingPushButton;
private slots:
void runPushButtonSlot();
};
#include "tabWidget.h"
tabWidget::tabWidget(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QHBoxLayout *hlayout1 = new QHBoxLayout;
QRadioButton *continusModeRadioButton = new QRadioButton(tr("连续"));
QRadioButton *triggerModeRadioButton = new QRadioButton(tr("触发"));
hlayout1->addWidget(continusModeRadioButton);
hlayout1->addWidget(triggerModeRadioButton);
QHBoxLayout *hlayout2 = new QHBoxLayout;
startGrabbingPushButton = new QPushButton(tr("开始"));
startGrabbingPushButton->setStyleSheet("background-color: rgb(0, 255, 127);");
QObject::connect(startGrabbingPushButton, SIGNAL(clicked()), this, SLOT(runPushButtonSlot()));
stopGrabbingPushButton = new QPushButton(tr("停止"));
hlayout2->addWidget(startGrabbingPushButton);
hlayout2->addWidget(stopGrabbingPushButton);
QGridLayout *vlayout = new QGridLayout;
vlayout->addLayout(hlayout1, 0, 0);
vlayout->addLayout(hlayout2, 1, 0);
QWidget *widget = new QWidget();
this->setCentralWidget(widget);
centralWidget()->setLayout(vlayout);
}
void tabWidget::runPushButtonSlot()
{
QMessageBox::information(this, "提示:", "提示-------");
startGrabbingPushButton->setStyleSheet("background-color: rgb(255, 100, 100);");
}
解决方案资源管理器–>右键工程–>选择添加Qt GUI Class:
创建一个名为LoginDlg的Qt GUI 类:
然后,在LoginDlg.ui上绘制两个按钮控件,分别命名为:loginPushButton、cancelPushButton,如图:
LoginDlg.h:
#pragma once
#include
#include "ui_LoginDlg.h"
class LoginDlg : public QWidget
{
Q_OBJECT
public:
LoginDlg(QWidget *parent = Q_NULLPTR);
~LoginDlg();
private:
Ui::LoginDlg ui;
private slots:
void loginPushButtonSlot();
signals:
void getSoftwareMode(QString sMode);
};
LoginDlg.cpp:
#include "LoginDlg.h"
LoginDlg::LoginDlg(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this); //
QObject::connect(ui.loginPushButton, SIGNAL(clicked()), this, SLOT(loginPushButtonSlot()));
}
LoginDlg::~LoginDlg()
{
}
void LoginDlg::loginPushButtonSlot()
{
emit getSoftwareMode("UserMode");
this->close();
}
tabWidget.h:
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include "ui_tabWidget.h"
#include "LoginDlg.h"
#pragma execution_character_set("utf-8")
class tabWidget : public QMainWindow
{
Q_OBJECT
public:
tabWidget(QWidget *parent = Q_NULLPTR);
private:
Ui::tabWidgetClass ui;
QPushButton *startGrabbingPushButton;
QPushButton *stopGrabbingPushButton;
LoginDlg *loginDlg;
private slots:
void runPushButtonSlot();
void setSoftwareModeSlot(QString sMode);
};
tabWidget.cpp:
#include "tabWidget.h"
tabWidget::tabWidget(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QHBoxLayout *hlayout1 = new QHBoxLayout;
QRadioButton *continusModeRadioButton = new QRadioButton(tr("连续"));
QRadioButton *triggerModeRadioButton = new QRadioButton(tr("触发"));
hlayout1->addWidget(continusModeRadioButton);
hlayout1->addWidget(triggerModeRadioButton);
QHBoxLayout *hlayout2 = new QHBoxLayout;
startGrabbingPushButton = new QPushButton(tr("开始"));
startGrabbingPushButton->setStyleSheet("background-color: rgb(0, 255, 127);");
QObject::connect(startGrabbingPushButton, SIGNAL(clicked()), this, SLOT(runPushButtonSlot()));
stopGrabbingPushButton = new QPushButton(tr("停止"));
hlayout2->addWidget(startGrabbingPushButton);
hlayout2->addWidget(stopGrabbingPushButton);
QGridLayout *vlayout = new QGridLayout;
vlayout->addLayout(hlayout1, 0, 0);
vlayout->addLayout(hlayout2, 1, 0);
QWidget *widget = new QWidget();
this->setCentralWidget(widget);
centralWidget()->setLayout(vlayout);
}
void tabWidget::runPushButtonSlot()
{
Qt::WindowFlags flags = Qt::Dialog;
loginDlg = new LoginDlg(this);
loginDlg->setWindowFlags(flags);
connect(loginDlg, SIGNAL(getSoftwareMode(QString)), this, SLOT(setSoftwareModeSlot(QString)));
loginDlg->setWindowTitle("登录");
loginDlg->setWindowModality(Qt::ApplicationModal);
loginDlg->show();
}
void tabWidget::setSoftwareModeSlot(QString sMode)
{
if (sMode == "UserMode")
{
startGrabbingPushButton->setStyleSheet("background-color: rgb(255, 100, 100);");
QMessageBox::information(this, "提示:", "登录成功!");
}
}