Re: TabWidget's Tab's parent
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:
and in the class with ui file included, we have:
Code:
private:
UI::UiFileName ui;
u can call ui.tabWidget->parent() which will definitely give you parent, if its there...
Re: TabWidget's Tab's parent
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.
Re: TabWidget's Tab's parent
can you show as how you add that button in tabwidget?
Re: TabWidget's Tab's parent
Sure, the following is the code and what I want to do is to access the "Form" using "pushButton":
Code:
tabWidget
->setGeometry
(QRect(20,
20,
421,
291));
pushButton
->setGeometry
(QRect(30,
40,
171,
71));
Re: TabWidget's Tab's parent
so this code doesn't work, right?
Code:
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"
Re: TabWidget's Tab's parent
Yeah it does not work since we create the "tab" without a parent, as it is advised in the documentation of the QTabWidget:
Quote:
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.
Re: TabWidget's Tab's parent
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?
Re: TabWidget's Tab's parent
works fine for me
Code:
{
hbl->addWidget(tabWidget);
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?
Re: TabWidget's Tab's parent
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.
Re: TabWidget's Tab's parent
Quote:
Originally Posted by
alisami
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.
Re: TabWidget's Tab's parent
Quote:
Originally Posted by
alisami
Is it possible to achieve this?
so, create method in QMainWindow which will return your tabwidget.
Re: TabWidget's Tab's parent
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.