Results 1 to 12 of 12

Thread: QTabWidget - Add Tabs dynamically

  1. #1
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default QTabWidget - Add Tabs dynamically

    HI...I have a problem with the dynamically add of tabs to a tabwidget.
    I create a GUI with a TabWidget...now, by code, when I add a tab wich has inside a widget created by mine Alarm1 (it has some qlabel and qbuttons inside), the tab was created and added but It don't display anything about the widget Alarm1. Strange because the same code in the constructor works fine like I want. Why?
    This is part of the code:

    Qt Code:
    1. //Add Alarm to Alarms List
    2. tab = new QWidget( twAlarms, "tab" );
    3. tabLayout = new QVBoxLayout( tab, 11, 6, "tabLayout");
    4. Alarm1 * alm = new Alarm1(alarm,tab);
    5. tabLayout->addWidget(alm);
    6. twAlarms->insertTab( tab, QString::fromLatin1("Alarm 1") );
    7. connect(alm, SIGNAL(buttonClicked()), this, SLOT(alarmSent()));
    To copy to clipboard, switch view to plain text mode 

    In the constructor instead the code is:

    Qt Code:
    1. twAlarms->removePage(tab);
    2. tab->close();
    3.  
    4. for (int i=0; i<3; i++) {
    5. tab = new QWidget( twAlarms, "tab" );
    6. tabLayout = new QVBoxLayout( tab, 11, 6, "tabLayout");
    7. Alarm1 * alm = new Alarm1(alarm, tab);
    8. tabLayout->addWidget(alm);
    9. twAlarms->insertTab( tab, QString::fromLatin1("Alarm"+QString::number(i)) );
    10. connect(alm, SIGNAL(buttonClicked()), this, SLOT(alarmSent()));
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTabWidget - Add Tabs dynamically

    A widget is hidden by default. Tabs created in constructor are shown together with their parent. You must make tabs added dynamically at runtime visible by yourself. Try adding:
    Qt Code:
    1. tab->show();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTabWidget - Add Tabs dynamically

    ok I put the line tab->show(); after the connect and so work fine.....now I have a problem with the layout.....the alarm1 widget is printed outside the tab too and not all in the tab, why?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTabWidget - Add Tabs dynamically

    Hard to say, it seems to be a custom widget we know nothing about. Since it's properly added to a layout (is it?), it presumably does something that breaks the layout.
    J-P Nurmi

  5. #5
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTabWidget - Add Tabs dynamically

    mhmmm...it's strange because I have the same problem if I add in the tab a simble button instead the widget alarm1

  6. #6
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTabWidget - Add Tabs dynamically

    Quote Originally Posted by fruzzo View Post
    mhmmm...it's strange because I have the same problem if I add in the tab a simble button instead the widget alarm1
    Take a look of undo shape example of QtDemo. It's work fine.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  7. #7
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTabWidget - Add Tabs dynamically

    Quote Originally Posted by ashukla View Post
    Take a look of undo shape example of QtDemo. It's work fine.
    I don't find it...link?

  8. #8
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QTabWidget - Add Tabs dynamically

    You can see it in your system folder undo folder.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  9. #9
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTabWidget - Add Tabs dynamically

    And just to comment: I've read in the docs that widget. which will be used, must be previously created WITHOUT parent.
    Qt 5.3 Opensource & Creator 3.1.2

  10. #10
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTabWidget - Add Tabs dynamically

    ok I've resolved the problem...it was releated to tab->show() that doesn't work well.
    I use instead twAlarms->showPage(...) and now the layout is fine.

  11. #11
    Join Date
    Dec 2007
    Posts
    119
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default Re: QTabWidget - Add Tabs dynamically

    another problem.... in my QTabWidget I have several tabs that may be of two kinds: tab with inside Alarm1 Widget and tab with inside Alarm2 Widget....Alarm1 and Alarm2 have both a QLabel like an unique identifier of the alarm type.
    Now I want to search in the whole QTabWidget which tab containe the Alarm with a particular "identifier"....any idea how to do this?

  12. #12
    Join Date
    Sep 2006
    Posts
    46
    Thanks
    2
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTabWidget - Add Tabs dynamically

    Use a QHash with either your QLabel or a QString as a lookup index for your alarm widget. There are many examples of similar lookups in QtDemo.

    Qt Code:
    1. QHash <QLabel *, QWidget *> labelForWidget;
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTabWidget with same tabs
    By Djony in forum Qt Programming
    Replies: 20
    Last Post: 24th December 2011, 12:20
  2. Delayed Rendering of QTabWidget Tabs
    By mclark in forum Qt Tools
    Replies: 13
    Last Post: 14th May 2007, 22:53
  3. QTabWidget tab visibility
    By dcss-design in forum Qt Programming
    Replies: 2
    Last Post: 30th January 2007, 23:24
  4. Switching off all tabs in QTabWidget
    By Gopala Krishna in forum Qt Programming
    Replies: 7
    Last Post: 30th August 2006, 17:10
  5. Dynamically adding tabs
    By larry104 in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2006, 20:27

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.