2014-05-10 100 views
0

我以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

您想使用信號和槽的窗口之間進行通信。 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); 
} 
相關問題