Results 1 to 7 of 7

Thread: Need Example of QTabWidget

  1. #1
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need Example of QTabWidget

    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/8...n-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?
    Last edited by davethomaspilot; 14th December 2012 at 18:11.

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need Example of QTabWidget

    Otherwise, how to find it?
    What? What do you mean?

    QTabWidget
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need Example of QTabWidget

    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?

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need Example of QTabWidget

    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

    Qt Code:
    1. class Page : QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. QLabel* label1();
    7. QLabel* label2();
    8.  
    9. void addLabel(QLabel* label);
    10.  
    11. private:
    12. QLabel* m_label1;
    13. // etc
    14. };
    15.  
    16. void some_method(QTabWidget* tabs)
    17. {
    18. Page* pg = new Page;
    19. QLabel* lbl = new QLabel("one");
    20. pg->addLabel(lbl); // Page should do the layout etc
    21.  
    22. tabs->addWidget(pg);
    23. }
    To copy to clipboard, switch view to plain text mode 
    You get the idea.
    Last edited by amleto; 14th December 2012 at 19:53.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need Example of QTabWidget

    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

  6. #6
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Need Example of QTabWidget

    QTabWidget::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)

  7. #7
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need Example of QTabWidget

    Ah, missed that. Thanks.

    And thanks to Amleto too!

    Dave Thomas

Similar Threads

  1. Replies: 6
    Last Post: 21st August 2013, 12:53
  2. QTabWidget
    By Freeman551 in forum Qt Programming
    Replies: 3
    Last Post: 25th December 2009, 00:06
  3. How do I add new tab to QTabWidget
    By pcmantinker in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2008, 04:24
  4. QTabWidget
    By allstar in forum Qt Programming
    Replies: 1
    Last Post: 5th December 2007, 19:18
  5. QTabWidget tab
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2007, 00:16

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.