Results 1 to 3 of 3

Thread: Adding QGroupBox to QTabWidget

  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

    Default Adding QGroupBox to QTabWidget

    Greetings,

    I am required to add QGroupBox objects (each containing a QTableWidget) to the QTabWIdget which resides on the central widget of my applications main window.

    I have been unable to add the QGroupBox objects to the QTabWidget object. I assume it is because I don't know what to pass into the ui.setupUI( ??? ) function. The group box with the table in it appears in a separate window unattached to the programs main window when using the following code.

    Qt Code:
    1. MainWin::MainWin( QWidget* parent, Qt::WFlags flags ) : QMainWindow( parent, flags )
    2. {
    3. ui.setupUi( this );
    4.  
    5. m_pCentralWidget = new QWidget;
    6. setCentralWidget( m_pCentralWidget );
    7.  
    8. // create tabs
    9. m_tabWidget = new QTabWidget( this );
    10.  
    11. ui.tabWidget = m_tabWidget;
    12.  
    13. m_tabDMX = new DMXTable( this );
    14. ui.tabWidget->addTab( m_tabDMX, tr("First Tab") );
    15.  
    16. m_tabAll = new AllTab( this );
    17. ui.tabWidget->addTab( m_tabAll, tr("Second Tab") );
    18.  
    19. // Load frame with required stuff; Place widgets in frame; left to right
    20. QFrame* qFrame = new QFrame( this );
    21. qFrame->setFrameRect( QRect( 0, 0, 0, 0 ) );
    22. qFrame->setFrameStyle( QFrame::Box | QFrame::Plain );
    23.  
    24. // Checkbox in frame
    25. m_onlineChk = new QCheckBox( "Show Online Table ", qFrame );
    26. m_onlineChk->setToolTip( "Show/Hide Online Table" );
    27. connect( m_onlineChk, SIGNAL( clicked() ), this, SLOT( onlineClicked() ) );
    28. m_onlineChk->setCheckState( Qt::Checked );
    29.  
    30. QHBoxLayout* frameLayout = new QHBoxLayout( qFrame );
    31. frameLayout->setSizeConstraint( QLayout::SetMaximumSize );
    32. frameLayout->addWidget( m_onlineChk, 0, Qt::AlignRight );
    33. frameLayout->insertStretch( 0, 0 );
    34. qFrame->setLayout( frameLayout );
    35.  
    36. // Put everything onto the main window.
    37. m_centralLayout = new QGridLayout;
    38.  
    39. // Tab widget is 1st
    40. m_centralLayout->addWidget( m_tabWidget, 1, 0, 1, 1);
    41.  
    42. // Frame is on bottom (just above the status bar)
    43. m_centralLayout->addWidget( qFrame, 2, 0, 1, 1 );
    44.  
    45. m_pCentralWidget->setLayout( m_centralLayout );
    46.  
    47. m_pExitAct = new QAction( tr("E&xit"), this );
    48. m_pExitAct->setShortcut( tr("Ctrl+Q") );
    49. connect( m_pExitAct, SIGNAL(triggered()), this, SLOT(close()) );
    50.  
    51. m_pFileMenu = menuBar()->addMenu( tr("&File") );
    52. m_pFileMenu->addAction( m_pExitAct );
    53.  
    54. show();
    55. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // DMX Table Tab Class
    2. DMXTable::DMXTable( QWidget* pParent ) : QWidget( /*pParent*/ )
    3. {
    4. m_parent = reinterpret_cast<MainWin*>( pParent );
    5.  
    6. ui.setupUi( m_parent );
    7. // ui.setupUi( this );
    8.  
    9. m_onlineGroupBox = new QGroupBox( tr("Online Devices") );
    10.  
    11. QScrollArea* pTableArea = new QScrollArea;
    12. m_pOnlineTable = new QTableWidget( 1, 5, m_parent );
    13.  
    14. QStringList sLabels;
    15. sLabels << "Status" << "Name" << "Type" << "IP Address" << "Model Name";
    16.  
    17. m_pOnlineTable->setHorizontalHeaderLabels( sLabels );
    18. m_pOnlineTable->setColumnWidth( 0, 40 );
    19. m_pOnlineTable->setColumnWidth( 1, 150 );
    20. m_pOnlineTable->setColumnWidth( 2, 130 );
    21. m_pOnlineTable->setColumnWidth( 3, 120 );
    22. m_pOnlineTable->setColumnWidth( 4, 200 );
    23.  
    24. pTableArea->setWidgetResizable( true );
    25. pTableArea->setWidget( m_pOnlineTable );
    26.  
    27. QGridLayout* layout = new QGridLayout;
    28. layout->addWidget( pTableArea, 0, 0 );
    29.  
    30. m_onlineGroupBox->setLayout( layout );
    31. m_onlineGroupBox->show();
    32. }
    To copy to clipboard, switch view to plain text mode 
    If I use "ui.setupUi( this )" in the DMXTable class I get the following compiler error: 'Ui_DMXTableClass::setupUi': cannot convert parameter 1 from 'DMXTable *const' to 'QMainWindow *'.

    I don't understand what to pass to the setupUI() function to get the QGroupBox to show on the tab widget. Anyone know what I am doing wrong?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Adding QGroupBox to QTabWidget

    You don't have to pass anything there. Just create your tab widget and set it as the central widget. You shouldn't touch setupUi() at all.

    Qt Code:
    1. MyWindow::MyWindow(QWidget *parent) : QMainWindow(parent){
    2. ui.setupUi(this);
    3. QTabWidget *tw = new QTabWidget(this);
    4. setCentralWidget(tw);
    5. QGroupBox *gb1 = new QGroupBox(tw);
    6. tw->addPage(gb1, "first");
    7. QHBoxLayout *l = new QHBoxLayout(gb1);
    8. DMXTable *t1 = new DMXTable;
    9. l->addWidget(t1);
    10. //...
    11. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to wysota for this useful post:

    mclark (17th January 2007)

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

    Smile Re: Adding QGroupBox to QTabWidget

    wysota, thanks for the reply.

    Your solution works fine. I was trying to copy code used in setting up tabs in a dialog and was not being careful about what I culled.

    This is, by far, the most helpful forum I've ever seen. You folks are great!

Similar Threads

  1. QTabWidget Placement and Display
    By mclark in forum Newbie
    Replies: 1
    Last Post: 16th January 2007, 20:34
  2. QGroupBox Issues
    By ToddAtWSU in forum Qt Programming
    Replies: 8
    Last Post: 16th January 2007, 13:45
  3. Adding buttons on the tab part of a tabwidget
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 20th December 2006, 17:52
  4. QGroupBox title truncated on both sides
    By Gopala Krishna in forum Qt Programming
    Replies: 2
    Last Post: 7th October 2006, 15:02
  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.