PDA

View Full Version : How to access QStackedWidget widgets



ecce
14th May 2015, 12:23
I have a QTreeWidget where the user adds a list of networking items to be scanned. Once the "scan" button is clicked it loops though the list and reads the configuration from each device. This is all stored in objects of the Device class that I've created (one for each device).

To show the result, I've created a bunch of QTabWidgets that are stacked in a QStackedWidget. The problem I have is updating the widgets in the tabs once they are shown on screen. Right now I don't save the widgets anywhere (except on the stack) so I cannot access them later on. I can't figure out how to store the widgets in a nice way for easy access. Storing them in self won't work, multiple network devices will cause overwriting. Storing them in the Device objects caused a crash ("You can't create a QPaintedObject before a QApplication" or something like that).

The only thing I can think of is creating a list in self and store the widgets there, and then try to identify widgets in the list somehow to find the right one, but there's got to be an easier way...? Is it possible to access them on the stack?

Any thoughts on how to solve this is appreciated.

Lesiok
14th May 2015, 13:32
In QStackedWidget You have two methods QStackedWidget::widget(int) and QStackedWidget::currentWidget() to take a pointer to widget.

d_stranz
15th May 2015, 18:01
except on the stack

I hope you mean "on the QStackedWidget" and not on the memory stack. If you're putting QWidget instances on the stack, they'll vanish as soon as they go out of scope.

You can always keep QWidget pointers in lists or vectors after they have been properly created and given a QWidget parent. To make sure you don't end up with dangling pointers, you can connect a slot to their QObject::destroyed() signal and use that to remove the sender from the collection of pointers.

The "can't create..." error arises when you try to create QWidget instances before you have a QApplication object created. This could happen if you declare QWidgets (not QWidget pointers) as members of a class that is constructed before QApplication (or if pointers, you try to create the instances in the construction of the class containing them), if you declare QWidgets at global scope, if you try to build lists (or whatever) of QWidget, or any other number of ways that your code tries to create a QWidget prior to the QApplication being constructed.