Results 1 to 5 of 5

Thread: QTabWidget Parenting Problem

  1. #1
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question QTabWidget Parenting Problem

    Hello,

    I am using 4.1.0 and have a tab widget in my main window. I add 2 widgets to the first tab and nothing to the subsequent tabs. The Tab_1 widgets show correctly but when I switch between tabs I expect to see my Tab_1 widgets disappear when Tab_2 is selected. This doesn't happen! The tab 1 widgets always show. This would appear to be a widget parenting problem.

    I am using the QTabWidget* as the parent of the group boxes and the QGroupBox* as the parent of my table and layout. Can anyone tell me what I'm doing (or not doing) that is causing the Tab_1 objects to display when Tab_2 is displayed?

    Qt Code:
    1. MainWin::MainWin( QWidget* parent, Qt::WFlags flags ) : QMainWindow( parent, flags )
    2. {
    3. ui.setupUi( this );
    4. m_pCentralWidget = new QWidget;
    5.  
    6. // create tabs
    7. m_tabWidget = new QTabWidget( this );
    8. ui.tabWidget = m_tabWidget;
    9.  
    10. m_tabDMX = new DMXTable( m_tabWidget );
    11. ui.tabWidget->addTab( m_tabDMX, tr("Tab_1") );
    12.  
    13. m_tabAll = new AllTable( m_tabWidget );
    14. ui.tabWidget->addTab( m_tabAll, tr("Tab_2") );
    15.  
    16. // Load frame with required stuff; Place widgets in frame; left to right
    17. QFrame* qFrame = new QFrame( this );
    18. qFrame->setFrameRect( QRect( 0, 0, 0, 0 ) );
    19. qFrame->setFrameStyle( QFrame::Box | QFrame::Plain );
    20.  
    21. // Checkbox in frame
    22. m_onlineChk = new QCheckBox( "Show Online Table ", qFrame );
    23. connect( m_onlineChk, SIGNAL( clicked() ), this, SLOT( onlineClicked() ) );
    24. m_onlineChk->setCheckState( Qt::Checked );
    25.  
    26. QHBoxLayout* frameLayout = new QHBoxLayout( qFrame );
    27. frameLayout->setSizeConstraint( QLayout::SetMaximumSize );
    28. frameLayout->addWidget( m_onlineChk, 0, Qt::AlignRight );
    29. frameLayout->insertStretch( 0, 0 );
    30. qFrame->setLayout( frameLayout );
    31.  
    32. // Put everything onto the main window.
    33. m_centralLayout = new QGridLayout;
    34. // Tab widget is 1st
    35. m_centralLayout->addWidget( m_tabWidget, 1, 0, 1, 1);
    36. m_centralLayout->setRowMinimumHeight( 1, 300 );
    37. // Frame is on bottom (just above the status bar)
    38. m_centralLayout->addWidget( qFrame, 2, 0, 1, 1 );
    39. m_centralLayout->setRowMinimumHeight( 2, 30 );
    40.  
    41. setCentralWidget( m_pCentralWidget );
    42. m_pCentralWidget->setLayout( m_centralLayout );
    43.  
    44. m_pExitAct = new QAction( tr("E&xit"), this );
    45. m_pExitAct->setShortcut( tr("Ctrl+Q") );
    46. connect( m_pExitAct, SIGNAL(triggered()), this, SLOT(close()) );
    47.  
    48. m_pFileMenu = menuBar()->addMenu( tr("&File") );
    49. m_pFileMenu->addAction( m_pExitAct );
    50.  
    51. show();
    52.  
    53. } // MainWin
    54.  
    55.  
    56. DMXTable::DMXTable( QWidget* pParent ) : QWidget( pParent )
    57. {
    58. m_parent = pParent;
    59.  
    60. createConfigTable();
    61. createOnlineTable();
    62.  
    63. QVBoxLayout* layout = new QVBoxLayout( pParent );
    64. layout->addSpacing( 25 );
    65. layout->addWidget( m_configGroupBox );
    66. layout->addWidget( m_onlineGroupBox );
    67. }
    68.  
    69. void DMXTable::createConfigTable( void )
    70. {
    71. QStringList sLabels;
    72. sLabels << "Status" << "Name" << "Type" << "IP Address" << "Model Name";
    73.  
    74. // Set up the table area.
    75. m_configGroupBox = new QGroupBox( tr("Configured Devices"), m_parent );
    76. m_pConfigTable = new QTableWidget( 1, 5, m_configGroupBox );
    77.  
    78. m_pConfigTable->setHorizontalHeaderLabels( sLabels );
    79. m_pConfigTable->setColumnWidth( 0, 40 );
    80. m_pConfigTable->setColumnWidth( 1, 150 );
    81. m_pConfigTable->setColumnWidth( 2, 130 );
    82. m_pConfigTable->setColumnWidth( 3, 120 );
    83. m_pConfigTable->setColumnWidth( 4, 150 );
    84.  
    85. QHBoxLayout* layout = new QHBoxLayout( m_configGroupBox );
    86. layout->addWidget( m_pConfigTable );
    87. m_configGroupBox->setLayout( layout );
    88. }
    89.  
    90.  
    91. void DMXTable::createOnlineTable( void )
    92. {
    93. QStringList sLabels;
    94. sLabels << "Status" << "Name" << "Type" << "IP Address" << "Model Name";
    95.  
    96. // Set up the table area.
    97. m_onlineGroupBox = new QGroupBox( tr("Online Devices"), m_parent );
    98. m_pOnlineTable = new QTableWidget( 1, 5, m_onlineGroupBox );
    99.  
    100. m_pOnlineTable->setHorizontalHeaderLabels( sLabels );
    101. m_pOnlineTable->setColumnWidth( 0, 40 );
    102. m_pOnlineTable->setColumnWidth( 1, 150 );
    103. m_pOnlineTable->setColumnWidth( 2, 130 );
    104. m_pOnlineTable->setColumnWidth( 3, 120 );
    105. m_pOnlineTable->setColumnWidth( 4, 150 );
    106.  
    107. QHBoxLayout* layout = new QHBoxLayout( m_onlineGroupBox );
    108. layout->addWidget( m_pOnlineTable );
    109. m_onlineGroupBox->setLayout( layout );
    110. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTabWidget Parenting Problem

    Try using m_tabwidget... dont do the ui.tabwidget = m_tabwidget... or tell us how ui.tabwidget is declared.

  3. #3
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Red face Re: QTabWidget Parenting Problem

    I was being foolish. I placed a QTabWidget control on the MainWindow form and then was creating another in the constructor. I now use the following code, but the problem still exists. The objects on Tab_1 do not go away when Tab_2 has focus.

    Qt Code:
    1. m_tabWidget = new QTabWidget( this );
    2.  
    3. m_tabDMX = new DMXTable( m_tabWidget );
    4. m_tabWidget->addTab( m_tabDMX, tr("Tab_1") );
    5.  
    6. m_tabAll = new AllTable( m_tabWidget );
    7. m_tabWidget->addTab( m_tabAll, tr("Tab_3") );
    8. ...
    9. m_centralLayout->addWidget( m_tabWidget, 1, 0, 1, 1);
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTabWidget Parenting Problem

    Quote Originally Posted by mclark View Post
    Qt Code:
    1. DMXTable::DMXTable( QWidget* pParent ) : QWidget( pParent )
    2. {
    3. ...
    4. QVBoxLayout* layout = new QVBoxLayout( pParent );
    5. ...
    6. }
    7.  
    8. void DMXTable::createConfigTable( void )
    9. {
    10. ...
    11. m_configGroupBox = new QGroupBox( ..., m_parent );
    12. ...
    13. }
    14.  
    15.  
    16. void DMXTable::createOnlineTable( void )
    17. {
    18. ...
    19. m_onlineGroupBox = new QGroupBox( ..., m_parent );
    20. ...
    21. }
    To copy to clipboard, switch view to plain text mode 
    You are creating those widgets on the tab's parent, not on the tab itself. Replace m_parent with "this".

  5. The following user says thank you to jacek for this useful post:

    mclark (18th January 2007)

  6. #5
    Join Date
    Aug 2006
    Location
    Madison, WI USA
    Posts
    153
    Thanks
    35
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: QTabWidget Parenting Problem

    Thanks to both VireX and jacek! These were, indeed, my problems. All is now working as expected.

Similar Threads

  1. QTabWidget - problem with resizing
    By moowy in forum Qt Programming
    Replies: 5
    Last Post: 14th September 2006, 14:06
  2. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  3. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  4. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.