PDA

View Full Version : Qt5 handling multiple widgets proficiently



known
23rd June 2015, 12:43
Hi all,

I enclosed a screenshot of my current project.

11241

I have a QTreeView on the left panel and a custom QWidget on the right panel. What I would like to have is switching QWidget when the user clicks on an element of the QTreeView. Each "radio" element (as it is mentionned on the screenshot) would have its own QWidget where I intend to have lots of information on the radio element.

What I first tried to do, is to attach a Widget (or its pointer) to a QStandardItem or the model index of the QTreeView/QStandardItemModel. I tried to do it with QVariant and the setData() method but it doesn't work. I get error messages from the compiler or if I manage to avoid the error messages I get NULL pointers to my widget.

I'm not sure now if it's a good idea to attach a QWidget pointer to a Qt element.

So what I'm asking is : is there a good way to handle multiple widget instances on a layout ? Can someone hint me an idea on how to achieve what I want to do ?

I hope I'm clear enough. Don't hesitate to ask for more info if it wasn't clear.

Thanks for your help

Is there a best way to switch

yeye_olive
23rd June 2015, 13:08
A simple solution is to populate a QStackedWidget with one widget for each "radio" item, and call QStackedWidget::setCurrentIndex() with the index of the element.

known
23rd June 2015, 14:22
Thanks for your answer, I didn't know this widget.

One cool thing is I can store the index of the QStackedWidget in the QStandardItemModel element as it is an integer.

So I'll be able to switch easily between widgets.