在你点击登录时,如果密码正确,则进入主窗口,这时,不要关闭登录界面,而是haid,隐藏。当你需要退出到登录窗口时,你可以show显示,就ok了
试试看
供参考:
main.cpp 文件
#include <QApplication>#include "clogindialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv)
CLoginDialog *loginDlg = new CLoginDialog()
loginDlg->show()
return app.exec()
}
clogindialog.h 文件
#ifndef CLOGINDIALOG_H#define CLOGINDIALOG_H
#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QComboBox>
#include <QLayout>
class CLoginDialog : public QDialog
{
Q_OBJECT
public:
explicit CLoginDialog(QWidget *parent = nullptr)
private slots:
void showMainWindow()
private:
QComboBox *userTypeCombo
}
#endif // CLOGINDIALOG_H
clogindialog.cpp 文件
#include "clogindialog.h"#include "cwidgeta.h"
#include "cwidgetb.h"
CLoginDialog::CLoginDialog(QWidget *parent) : QDialog(parent)
{
QHBoxLayout *lineLayout1 = new QHBoxLayout()
lineLayout1->addWidget(new QLabel(tr("登录类型")))
userTypeCombo = new QComboBox()
userTypeCombo->addItem(tr("教师"),0)
userTypeCombo->addItem(tr("学生"),1)
lineLayout1->addWidget(userTypeCombo)
QHBoxLayout *lineLayout2 = new QHBoxLayout()
lineLayout2->addStretch()
QPushButton *loginBt = new QPushButton(tr("登录"))
connect(loginBt,&QPushButton::clicked,this,&CLoginDialog::showMainWindow)
lineLayout2->addWidget(loginBt)
QVBoxLayout *mainLayout = new QVBoxLayout()
mainLayout->addLayout(lineLayout1)
mainLayout->addLayout(lineLayout2)
setLayout(mainLayout)
}
void CLoginDialog::showMainWindow()
{
if(userTypeCombo->currentData() == 0){
CWidgetA *a = new CWidgetA()
a->show()
}else{
CWidgetB *b = new CWidgetB()
b->show()
}
close()
}
cwidgeta.h 文件
#ifndef CWIDGETA_H#define CWIDGETA_H
#include <QMainWindow>
class CWidgetA : public QMainWindow
{
Q_OBJECT
public:
explicit CWidgetA(QWidget *parent = nullptr)
}
#endif // CWIDGETA_H
cwidgeta.cpp 文件
#include "cwidgeta.h"#include <QLabel>
CWidgetA::CWidgetA(QWidget *parent) : QMainWindow(parent)
{
setCentralWidget(new QLabel(tr("这是窗口A")))
}
cwidgetb.h
#ifndef CWIDGETB_H
#define CWIDGETB_H
#include <QMainWindow>
class CWidgetB : public QMainWindow
{
Q_OBJECT
public:
explicit CWidgetB(QWidget *parent = nullptr)
}
#endif // CWIDGETB_H
cwidgetb.cpp
#include "cwidgetb.h"#include <QLabel>
CWidgetB::CWidgetB(QWidget *parent) : QMainWindow(parent)
{
setCentralWidget(new QLabel(tr("这是窗口B")))
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)