PDA

View Full Version : Need Example of QTabWidget



davethomaspilot
14th December 2012, 18:11
I've created a QTabWidget, but I can't figure out the right way to add widgets to the pages.

If I use Qt Designer, I can put a unique instance on each page of something like a List Widget by dragging them on top of each page of the QTabWidget. But, in code, I see the List Widget as "listWidget", listWidget_2, ..., instead of something like tabWidget.index(blah)->listWidget. So, it seems like the parent of the list widget isn't the tabWidget, but rather my top level widget.

I want every page to have a List Widget (and identical layout). So, I think this means I have to add the pages in my code and also create the List widgets for each new page in code. If I want to add data to one of the listwidgets, I need to find the list associated with, say ,the current page, and then add content by referencing that widget.

Is that the correct way to do it? Since pages can be added and deleted, it doesn't make sense to put widgets on the pages in Qt Designer?

Also, is there somewhere I can see what a typical use of QTabWidget looks like? Something that supports add and delete of pages and with some content on the pages, not just the tab names.

Thanks,

Dave Thomas

Added after 1 9 minutes:

Ok, I found what I need if there's only one widget on each tab, or just one of the type of interest:

http://stackoverflow.com/questions/8678684/access-the-widget-of-a-tab-in-a-qtabwidget

Works fine. I guess I"ll make sure I have only one widget if I want to be able to access it later. Otherwise, how to find it?

amleto
14th December 2012, 18:36
Otherwise, how to find it?
What? What do you mean?

QTabWidget

davethomaspilot
14th December 2012, 19:01
Keeping it simple, say I have two or more labels on each tab. The labels are dynamically created each time a new tab is created. That part's easy.

But, after creation, I want to up data the text of the labels. I can live with always updating the text on the current tab, but how to address one label versus the other?

amleto
14th December 2012, 19:44
save the address of the labels.

One sensible option is to have a wrapper widget that provides access to all the widgets you will want to update later



class Page : QWidget
{
Q_OBJECT

public:
QLabel* label1();
QLabel* label2();

void addLabel(QLabel* label);

private:
QLabel* m_label1;
// etc
};

void some_method(QTabWidget* tabs)
{
Page* pg = new Page;
QLabel* lbl = new QLabel("one");
pg->addLabel(lbl); // Page should do the layout etc

tabs->addWidget(pg);
}

You get the idea.

davethomaspilot
14th December 2012, 21:10
Thanks for replying.


But


void some_method(QTabWidget* tabs)
..
tabs->addWidget(pg);

QTabWidget doesn't have an addWidget method. It has an addTab method, but it only takes a QString & (or that and an icon).



So, the best I've figured out how to do is to create a layout, add the widget to the layout, then set the newly created tab's layout to this layout.

This makes the widget show up on the tab, but the tab isn't actually it's parent.



I




I

Zlatomir
14th December 2012, 21:19
QTabWidget::addTab (http://qt-project.org/doc/qt-4.8/qtabwidget.html#addTab) has a QWidget* as a first argument. //That should work with amleto's page class (or any of yours) derived from QWidget (he only omitted a public derivation from QWidget)

davethomaspilot
14th December 2012, 21:24
Ah, missed that. Thanks.

And thanks to Amleto too!

Dave Thomas