2015-07-19 326 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