PDA

View Full Version : TabWidget's Tab's parent



alisami
31st March 2009, 10:01
Hi,

I have created a tabwidget and add tabs to the tabwidget using the designer. On one of the tabs, added a pushbutton. The problem is that, the widgets added to the tabwidget as a tab does not have a parent. What I want to do is to access the parent window of the tabwidget using the pushbutton. How can I access the parent ?

Using parent(), parentWindow(), window() returns NULL for the tab pages.

Sami

talk2amulya
31st March 2009, 10:49
i dont totally understand what you wanna do, but if you wanna find any widget's parent, you should be able to access that widget from the ui file and calling parent() on it should return your value. For eg. if in ui file we have:


public:
QTabWidget *tabWidget;

and in the class with ui file included, we have:


private:
UI::UiFileName ui;

u can call ui.tabWidget->parent() which will definitely give you parent, if its there...

alisami
31st March 2009, 11:46
Yeah for sure I can access tabwidget's parent by using its parent() method. But I want to access that through the pushbutton I have added to one of the tabs of the tabwidget.

spirit
31st March 2009, 11:48
can you show as how you add that button in tabwidget?

alisami
31st March 2009, 12:07
Sure, the following is the code and what I want to do is to access the "Form" using "pushButton":



QWidget* Form;
QTabWidget *tabWidget;
QWidget *tab;
QPushButton *pushButton;

Form = new QWidget();
tabWidget = new QTabWidget(Form);
tabWidget->setGeometry(QRect(20, 20, 421, 291));
tab = new QWidget();
pushButton = new QPushButton(tab);
pushButton->setGeometry(QRect(30, 40, 171, 71));
tabWidget->addTab(tab, QString());

spirit
31st March 2009, 12:11
so this code doesn't work, right?


QWidget *parent = pushButton->parentWidget();//parent holds "tab"
if (!parent)
return;
parent = parent->parentWidget();//parent holds "tabWidget"
if (!parent)
return;
parent = parent->parentWidget();//parent holds "form"

alisami
31st March 2009, 12:22
Yeah it does not work since we create the "tab" without a parent, as it is advised in the documentation of the QTabWidget:


The normal way to use QTabWidget is to do the following:
1- Create a QTabWidget.
2- Create a QWidget for each of the pages in the tab dialog, but do not specify parent widgets for them.
3- Insert child widgets into the page widget, using layouts to position them as normal.
4- Call addTab() or insertTab() to put the page widgets into the tab widget, giving each tab a suitable label with an optional keyboard shortcut.

talk2amulya
31st March 2009, 12:34
i dont get it..u know the parent is Form, then why dont u just use it? plus do u want to access this on click of pushbutton or what?

spirit
31st March 2009, 12:34
works fine for me


Test::Test(QWidget *parent)
: QWidget(parent)
{
QHBoxLayout *hbl = new QHBoxLayout(this);

QTabWidget *tabWidget = new QTabWidget();
hbl->addWidget(tabWidget);

QWidget *page = new QWidget();
QHBoxLayout *layout = new QHBoxLayout(page);

m_pbUpdateQuery = new QPushButton(tr("Update"));
layout->addWidget(m_pbUpdateQuery);

tabWidget->addTab(page, tr("Test"));

connect(m_pbUpdateQuery, SIGNAL(clicked()), SLOT(updateQuery()));
}

void Test::updateQuery()
{
QWidget *parent = m_pbUpdateQuery->parentWidget();
if (!parent)
return;
parent = parent->parentWidget();
if (!parent)
return;
parent = parent->parentWidget();
if (!parent)
return;
parent = parent->parentWidget();
}

btw, what is reason of using this approach? why don't just store "parent" in some variable?

alisami
31st March 2009, 13:15
Spirit thanks for your interest.

First I shall describe the reason for doing such a thing. I have created a custom widget and part of it hides all the windows related to the widget it is located. So I connect one of its clicked() signals to the underlying QMainWindow but the level of the QMainWindow is not known to the widget. So I traverse the parents.

On the constructor of the widget, I traverse the parents but since during the construction, the page is not added to the tab widget, and so the parent is absent.

So, what I have to do is I should trigger a slot of my custom widget to make the connections to the QMainWindow when setupUi is finished.

Is it possible to achieve this? Or can I change the order of the construction of the ui_XXX.h files.

spirit
31st March 2009, 13:19
Or can I change the order of the construction of the ui_XXX.h files.
chaching something in ui_xxx.h it's bad idea, because this file is generated automatically by uic, so when you change you ui file, ui_xxx.h will be regenerated and all data which you added will be lost.

spirit
31st March 2009, 13:22
Is it possible to achieve this?

so, create method in QMainWindow which will return your tabwidget.

alisami
31st March 2009, 13:30
The problem is that the custom widget will be inserted to various places. Not only to a tabwidget. I just don't want to add some code to every widget containing my custom widget. So I decided to add a flag to my custom flag to see if the QMainWindow is successfully connected to the signal and if not, it tries to connect before firing any signals.