QToolbar: Changing widget in toolbar
So I have a large view containing a tab widget. Depending on the tab chosen, the toolbar[outside the main widget] is suppose to change. I have to preserve the toolbar state as well. Hence I created multiple widgets which I can push into the toolbar depending on the tab chosen.
Now, I created a QHash<key, QWidget*> type [The widget being the widget for the tab]. And when the tab changes I removed the old toolbar by setting its parent as NULL. Also, I am inserting the new toolbar.
Here are my observations:
- If I directly add the widget to the toolbar [toolbar->addWidget(&wid)], the toolbar takes control of the widget and hence the same 'key' now results in a null variable return the second time.
- If I use a layout in the middle and add widget to the layout [toolbar->setLayout(gridLayout); gridLaout->addWidget(&wid);], the widget control stays with me and I can add/remove it as I like. But now, the toolbar's size is not affected by the widget [The resultant size is 0,0]. I can set the toolbar height and width [toolbar->setHeight(wid.height)] or something, but then the toolbar's inbuilt resizing is removed. Like when a double arrow comes [instead of the full toolbar to accommodate the cramped space] on adding another toolbar and space is cramped.
I was hence wondering if there is some method I am missing or some method I can use to preserve the toolbar's working without giving it the ownership of my widget when added.
If I directly add the widget to the toolbar [toolbar->addWidget(&wid)], the toolbar takes control of the widget and hence the same 'key' now results in a null variable return the second time.'
That's weird. So you're saying that the next time you try to get a widget from the
QHashusing the key after it has been reparented then it returns a null? It shouldn't be removing your pointer from the QHash just because something else takes ownership of it.
However that being said I wouldn't do what you want this way. Almost guaranteed at some point a widget you still want to use (after having been reparented) will get cleaned up and you will reference it's dangling pointer.
What I would do for this is just put a single QStackedWidget onto your toolbar, then change it's page based on the context of your view. That way you don't need a QHash at all, just an index of what QStackedWidget page to change to.
hi @Ankit.Jain ,
if QHash is anything like std::Hash, thank make sure, that your have no typo in your hash-Key,
counter intuitively, asking for an undefined hash-key will not crash or throw a warning or anything, it will append a new Key with the value 0.
Isn't it much easier to create and add all the toolbars and then show/hide them based on the tab you are in instead of receating the same toolbar from scratch every time?
@VRonin I think he was only changing 1 widget inside a toolbar not the whole bar. If he is changing the whole bar then absolutely easier to just show and hide entire bars.
I read it as him wanting to replace just a single widget (or group of widgets) based on context.
I'll explain a bit more,
The toolbar has only 1 widget [which I keep changing].
The widget corresponds to a scenario [think of it as a sheet in excel, you can have a infinite number of them, and you can add/remove them anytime you want].
I can technically save the toolbar state as a structure of values, but then I need to refurbish the toolbar with the values and states [frame colors which I have set]. I'd also have to move the scenario specific stuff I am doing in that toolbar to a different class.
To avoid that, I decided to just have multiple widgets and show the corresponding widget at the correct time in the toolbar space holder.
P.S. I cannot create too many toolbars as the same thing is a little tricky to do in my code base [it cannot be done dynamically at all]
@Ankit.Jain Would my idea of a QStackedWidget work for that then? It should be nice and easy to change and allow you an infinite amount (well not really, but nothing is infinite in computers).
@ambershark I apologize for the late reply, I fell sick.
Anyway, yea using StackWidget did the job.