Set widget as central widget in a QMainWindow
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
Re: Set widget as central widget in a QMainWindow
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.
Re: Set widget as central widget in a QMainWindow
Do you want it do it from QtCreator/QtDesigner? If Yes, then you need to use the "promote widget" feature.
Re: Set widget as central widget in a QMainWindow
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:
Code:
myComboBox->setCurrentIndex(1);
Showldn't this be enought?
Re: Set widget as central widget in a QMainWindow
That should work. Did the slot execute?
BTW, reply to first post: Only QWdigets can be set on QMainWindow (not QDialogs)
Re: Set widget as central widget in a QMainWindow
Yess ... used a cout to let me know the index.
ops ...
Quote:
BTW, reply to first post: Only QWdigets can be set on QMainWindow (not QDialogs)
This might be the problem ... i will let you know ...
Re: Set widget as central widget in a QMainWindow
Quote:
Originally Posted by
gtnoob
Then i created a private slot associated to an action on the QMainWindow menu.
But it is not changing the central widget.
I used:
Code:
myComboBox->setCurrentIndex(1);
Is myComboBox your stacked widget?
Re: Set widget as central widget in a QMainWindow
I assume a connection is made between QComboBox and QStackedWidget as mentioned in the documentaion referred.
1 Attachment(s)
Re: Set widget as central widget in a QMainWindow
Well ... i must be missing something really obvious!
I attach a sample code.
Could someone please take a look?
Re: Set widget as central widget in a QMainWindow
You changing the QComboBox item programatically, this is emit currentIndexChanged(int) , not activated(int). Do the changes below
Code:
//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)
Re: Set widget as central widget in a QMainWindow
I see ... thanks.
One final "open" question:)
Could this be a valid approach for a larger scale application?
Re: Set widget as central widget in a QMainWindow
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)
Re: Set widget as central widget in a QMainWindow
Quote:
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?
Re: Set widget as central widget in a QMainWindow
Yes, they should look like
Code:
widget1 = new w1(this);
widget2 = new w2(this);
Re: Set widget as central widget in a QMainWindow
Does this ( http://qt-project.org/doc/qt-5.0/qtc...jecttrees.html ) explain the "because..." of your point of view?
Re: Set widget as central widget in a QMainWindow