我从Qt开始。以前我用Java Swing做过,我在Card Layout中完成了这个。我有MainWindow,Login Widget和Dashboard Widget。
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// set login screen on startup
QWidget *login = new Login(this);
setCentralWidget(login);
}
Login.cpp
#include "login.h"
#include "ui_login.h"
Login::Login(QWidget *parent) :
QWidget(parent),
ui(new Ui::Login)
{
ui->setupUi(this);
}
Login::~Login()
{
delete ui;
}
void Login::on_loginButton_clicked()
{
// some logic
// here I want to create Dashboard widget in central widget
// but method setCentralWidget() can't be called from here
}
答案 0 :(得分:0)
您希望使用信号和插槽在窗口之间进行通信。 QDialog特别提供三个重要信号:accepted(),rejected(),finished(int)。所以你会想做这样的事情:
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// set login screen on startup
QWidget *login = new Login(this);
connect(login, SIGNAL(finished(int)), this, SLOT(loginFinished(int)));
setCentralWidget(login);
}
MainWindow::loginFinished(int reason)
{
Login *login = qobject_cast<Login*>(sender());
if (!login) {
qDebug() << "something bad happened!";
return;
}
login->deleteLater();
setCentralWidget(someOtherWidget);
}