Results 1 to 13 of 13

Thread: TabWidget's Tab's parent

  1. #1
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default TabWidget's Tab's parent

    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

  2. #2
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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:
    Qt Code:
    1. public:
    2. QTabWidget *tabWidget;
    To copy to clipboard, switch view to plain text mode 

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

    Qt Code:
    1. private:
    2. UI::UiFileName ui;
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: TabWidget's Tab's parent

    can you show as how you add that button in tabwidget?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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":

    Qt Code:
    1. QWidget* Form;
    2. QTabWidget *tabWidget;
    3. QWidget *tab;
    4. QPushButton *pushButton;
    5.  
    6. Form = new QWidget();
    7. tabWidget = new QTabWidget(Form);
    8. tabWidget->setGeometry(QRect(20, 20, 421, 291));
    9. tab = new QWidget();
    10. pushButton = new QPushButton(tab);
    11. pushButton->setGeometry(QRect(30, 40, 171, 71));
    12. tabWidget->addTab(tab, QString());
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: TabWidget's Tab's parent

    so this code doesn't work, right?
    Qt Code:
    1. QWidget *parent = pushButton->parentWidget();//parent holds "tab"
    2. if (!parent)
    3. return;
    4. parent = parent->parentWidget();//parent holds "tabWidget"
    5. if (!parent)
    6. return;
    7. parent = parent->parentWidget();//parent holds "form"
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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:

    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.

  8. #8
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default 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?

  9. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: TabWidget's Tab's parent

    works fine for me
    Qt Code:
    1. Test::Test(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. QHBoxLayout *hbl = new QHBoxLayout(this);
    5.  
    6. QTabWidget *tabWidget = new QTabWidget();
    7. hbl->addWidget(tabWidget);
    8.  
    9. QWidget *page = new QWidget();
    10. QHBoxLayout *layout = new QHBoxLayout(page);
    11.  
    12. m_pbUpdateQuery = new QPushButton(tr("Update"));
    13. layout->addWidget(m_pbUpdateQuery);
    14.  
    15. tabWidget->addTab(page, tr("Test"));
    16.  
    17. connect(m_pbUpdateQuery, SIGNAL(clicked()), SLOT(updateQuery()));
    18. }
    19.  
    20. void Test::updateQuery()
    21. {
    22. QWidget *parent = m_pbUpdateQuery->parentWidget();
    23. if (!parent)
    24. return;
    25. parent = parent->parentWidget();
    26. if (!parent)
    27. return;
    28. parent = parent->parentWidget();
    29. if (!parent)
    30. return;
    31. parent = parent->parentWidget();
    32. }
    To copy to clipboard, switch view to plain text mode 
    btw, what is reason of using this approach? why don't just store "parent" in some variable?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  10. The following user says thank you to spirit for this useful post:

    alisami (31st March 2009)

  11. #10
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  12. #11
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: TabWidget's Tab's parent

    Quote Originally Posted by alisami View Post
    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.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  13. #12
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: TabWidget's Tab's parent

    Quote Originally Posted by alisami View Post
    Is it possible to achieve this?
    so, create method in QMainWindow which will return your tabwidget.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  14. #13
    Join Date
    May 2008
    Posts
    61
    Thanks
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

Similar Threads

  1. How to close parent window from child window?
    By montylee in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2008, 11:40
  2. parent() heirarchy misunderstanding?
    By KShots in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2007, 18:18
  3. How to make "west" tabs horizontal?
    By plumbum in forum Qt Programming
    Replies: 2
    Last Post: 7th June 2007, 10:32
  4. Removing Tabs
    By NewGuy in forum Qt Programming
    Replies: 6
    Last Post: 22nd July 2006, 22:46
  5. Move child widget along with the parent widget
    By sreedhar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.