PDA

View Full Version : Set widget as central widget in a QMainWindow



gtnoob
7th January 2013, 22:12
Hi

Suppose i have w (a QWidget) and W (a QMainWindow).
This means i have a w.h+w.cpp+w.ui and W.h, W.cpp and W.ui, as well as the rest of the project files.

How can i use the w widget as the W central widget, as well as keep changing from w to other similar widgets (... x, y, z , ...)?
Does this apply to QDialogs instead of QWidgets as well?

Summarizing, i want to write several QDialogs or QWidgets and show them inside a QMainWindow, one at a time, in it's central widget.

Thanks

Lykurg
7th January 2013, 22:43
The easiest approach would be to put all widgets (w,x,y,z...) in a QStackedWidget. Then set that stacked widget as central widget. This way you can easily change between the widgets.

Santosh Reddy
8th January 2013, 07:59
Do you want it do it from QtCreator/QtDesigner? If Yes, then you need to use the "promote widget" feature.

gtnoob
8th January 2013, 10:16
I'm doing this from QtCreator/QtDesigner.

I used the Lykurg approach and did something like this (see detailed description): http://qt-project.org/doc/qt-4.8/qstackedwidget.html

Then i created a private slot associated to an action on the QMainWindow menu.
But it is not changing the central widget.
I used:

myComboBox->setCurrentIndex(1);

Showldn't this be enought?

Santosh Reddy
8th January 2013, 10:31
That should work. Did the slot execute?

BTW, reply to first post: Only QWdigets can be set on QMainWindow (not QDialogs)

gtnoob
8th January 2013, 10:44
Yess ... used a cout to let me know the index.

ops ...

BTW, reply to first post: Only QWdigets can be set on QMainWindow (not QDialogs)
This might be the problem ... i will let you know ...

Lykurg
8th January 2013, 11:12
Then i created a private slot associated to an action on the QMainWindow menu.
But it is not changing the central widget.
I used:

myComboBox->setCurrentIndex(1);
Is myComboBox your stacked widget?

Santosh Reddy
8th January 2013, 11:25
I assume a connection is made between QComboBox and QStackedWidget as mentioned in the documentaion referred.

gtnoob
8th January 2013, 13:14
Well ... i must be missing something really obvious!
I attach a sample code.
Could someone please take a look?

Santosh Reddy
8th January 2013, 13:33
You changing the QComboBox item programatically, this is emit currentIndexChanged(int) , not activated(int). Do the changes below


//connect(opComboBox, SIGNAL(activated(int)), myStack, SLOT(setCurrentIndex(int))); //<<<<<<<<<<<<<<<< Change to next line
connect(opComboBox, SIGNAL(currentIndexChanged(int)), myStack, SLOT(setCurrentIndex(int)));


Note that your QComboBox is hidded, if you show it and change the item directly in combo box then the activated(int) signal will be emited (and also currentIndexChanged(int) signal)

gtnoob
8th January 2013, 14:16
I see ... thanks.

One final "open" question:)
Could this be a valid approach for a larger scale application?

Santosh Reddy
8th January 2013, 14:27
Yes and No.

Yes for using QStakedWidget, and signalling it from QComboBox is just fine.

No for
1. Hiding QComboBox (that too floating, with out parent)
2. Most the objects in the example code does not have default parent objects, this will lead to memory leaks once the application grows. (I understand this was just an example, but don't do in real application)

gtnoob
8th January 2013, 14:44
Most the objects in the example code does not have default parent objects
What exactly does this mean?
Are you refering to w1 and w2?

Santosh Reddy
8th January 2013, 15:04
Yes, they should look like


widget1 = new w1(this);
widget2 = new w2(this);

gtnoob
8th January 2013, 15:45
Does this ( http://qt-project.org/doc/qt-5.0/qtcore/objecttrees.html ) explain the "because..." of your point of view?

Santosh Reddy
8th January 2013, 15:51
Yes, I mean the same :)