View Full Version : setCentralWidget problems

17th October 2009, 02:26
Setup: Qt 4.4.3, using visual studio 2005

I have an app ui file (which is empty) and I want to add another centralWidget UI file on top of it (so when I maximize I can have a black background in my app ui and my centralWidget UI can be center screen. I am trying to use setCentralWidget() for this. If I explained things correctly, is this the path I want to follow? I cant even get a button to display in the central widget:

QApp::QApp(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
CentralWidget cw;
QPushButton b;
setCentralWidget( &b );

Any help is appreciated. Thank you!

17th October 2009, 05:01
You should not declare cw and b in the constructor and in the stack, declare them in the class and in the heap, as pointers, because they get out of scope when then constructor method ends.

CentralWidget *cw = new CentralWidget;
QPushButton *b = new QPushButton;

19th October 2009, 22:26
Doh! What a n00b error. Thanks for the help!