2015-07-19 30 views
0

這裏是我的類主窗口:爲什麼setCentralWidget不工作?

MainWindow::MainWindow(QWidget *parent) 
    : QMainWindow(parent) 
{ 
    menu* v = new menu(this); 
    setCentralWidget(v); 
} 

我的菜單類:

menu::menu(MainWindow* parent){ 
    QLabel l = new QLabel("123"); 
    QVBoxLayout* lay = new QVBoxLayout; 
    lay->addWidget(l); 
    this->setLayout(lay); 
    QWidget* a = new QWidget; 
    QVBoxLayout* lay2 = new QVBoxLayout; 
    QLabel* ll = new QLabel("456"); 
    lay2->addWidget(ll); 
    a->setLayout(lay2); 
    parent->setCentralWidget(a); 
} 

當我運行程序時,窗口會顯示123,但我想它顯示456

方法setCentralWidget不起作用?

回答

1

setCentralWidget工作正常。 您正在將您的小工具menu構造與其在外部小工具中的位置(MainWindow)混合在一起。你應該保持這些東西分開,否則你將無法在其他小部件中使用menu

因此,您應該在構造函數中設置外觀menu,並且僅在MainWindow中調用setCentralWidget

它應該看起來像:

file.h

menu::menu(QWidget* parent = 0); 

file.cpp

menu::menu(QWidget* parent) : QWidget(parent) 
{ 
    // Create items 
    QLabel* l = new QLabel("123", this); 
    QLabel* ll = new QLabel("456", this); 

    // Put items in layout 
    QVBoxLayout* lay = new QVBoxLayout(); 
    lay->addWidget(l); 
    lay->addWidget(ll); 

    // Set "lay" as the layout of this widget 
    setLayout(lay); 
} 

UPDATE

由於萬特d行爲是具有根據按鈕點擊切換視圖的接口:

enter image description here

最好的選擇是使用一個QStackedWidget

這裏的示例代碼將使用QStackedWidget生成此接口。

widget1.h

#ifndef WIDGET1 
#define WIDGET1 

#include <QWidget> 
#include <QPushButton> 
#include <QVBoxLayout> 

class Widget1 : public QWidget 
{ 
    Q_OBJECT 
public: 
    Widget1(QWidget* parent = 0) : QWidget(parent) { 
     QPushButton* btn = new QPushButton("Button Widget 1", this); 
     QVBoxLayout* layout = new QVBoxLayout(); 
     layout->addWidget(btn); 
     setLayout(layout); 
     connect(btn, SIGNAL(clicked()), SIGNAL(buttonClicked())); 
    } 

signals: 
    void buttonClicked(); 
}; 

#endif // WIDGET1 

widget2.h

#ifndef WIDGET2 
#define WIDGET2 

#include <QWidget> 
#include <QPushButton> 
#include <QVBoxLayout> 

class Widget2 : public QWidget 
{ 
    Q_OBJECT 
public: 
    Widget2(QWidget* parent = 0) : QWidget(parent) { 
     QPushButton* btn1 = new QPushButton("Button 1 Widget 2", this); 
     QPushButton* btn2 = new QPushButton("Button 2 Widget 2", this); 
     QVBoxLayout* layout = new QVBoxLayout(); 
     layout->addWidget(btn1); 
     layout->addWidget(btn2); 
     setLayout(layout); 
     connect(btn2, SIGNAL(clicked()), SIGNAL(button2Clicked())); 
    } 

signals: 
    void button1Clicked(); 
    void button2Clicked(); 
}; 

#endif // WIDGET2 

mainwindow.h

#ifndef MAINWINDOW_H 
#define MAINWINDOW_H 

#include <QMainWindow> 
#include <QStackedWidget> 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 
public: 
    MainWindow(QWidget *parent = 0); 
    ~MainWindow(); 
public slots: 
    void buttonWidget1Clicked(); 
    void button2Widget2Clicked(); 
private: 
    QStackedWidget* m_sw; 
}; 

#endif // MAINWINDOW_H 

mainwindow.cpp

#include "mainwindow.h" 
#include <QVBoxLayout> 
#include <QLabel> 
#include "widget1.h" 
#include "widget2.h" 

MainWindow::MainWindow(QWidget *parent) 
    : QMainWindow(parent) 
{ 
    // Create Widgets 
    Widget1* w1 = new Widget1(this); 
    Widget2* w2 = new Widget2(this); 
    QLabel* w3 = new QLabel("Result", this); 

    m_sw = new QStackedWidget(this); 
    m_sw->addWidget(w1); 
    m_sw->addWidget(w2); 
    m_sw->addWidget(w3); 

    setCentralWidget(m_sw); 

    connect(w1, SIGNAL(buttonClicked()), this, SLOT(buttonWidget1Clicked())); 
    connect(w2, SIGNAL(button2Clicked()), this, SLOT(button2Widget2Clicked())); 
} 

void MainWindow::buttonWidget1Clicked() 
{ 
    m_sw->setCurrentIndex(1); // Will show Widget2 
} 

void MainWindow::button2Widget2Clicked() 
{ 
    m_sw->setCurrentIndex(2); // Will show Widgee3 
} 

MainWindow::~MainWindow() {} 
+0

我想測試它是否可以在另一個類中調用setCentralWidget。 – Woody

+0

所以我永遠不能在另一個類中調用setCentralWidget? – Woody

+0

我只想找到切換QWidget的方法。 – Woody