PDA

View Full Version : Adding QGroupBox to QTabWidget



mclark
16th January 2007, 19:37
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.


MainWin::MainWin( QWidget* parent, Qt::WFlags flags ) : QMainWindow( parent, flags )
{
ui.setupUi( this );

m_pCentralWidget = new QWidget;
setCentralWidget( m_pCentralWidget );

// create tabs
m_tabWidget = new QTabWidget( this );

ui.tabWidget = m_tabWidget;

m_tabDMX = new DMXTable( this );
ui.tabWidget->addTab( m_tabDMX, tr("First Tab") );

m_tabAll = new AllTab( this );
ui.tabWidget->addTab( m_tabAll, tr("Second Tab") );

// Load frame with required stuff; Place widgets in frame; left to right
QFrame* qFrame = new QFrame( this );
qFrame->setFrameRect( QRect( 0, 0, 0, 0 ) );
qFrame->setFrameStyle( QFrame::Box | QFrame::Plain );

// Checkbox in frame
m_onlineChk = new QCheckBox( "Show Online Table ", qFrame );
m_onlineChk->setToolTip( "Show/Hide Online Table" );
connect( m_onlineChk, SIGNAL( clicked() ), this, SLOT( onlineClicked() ) );
m_onlineChk->setCheckState( Qt::Checked );

QHBoxLayout* frameLayout = new QHBoxLayout( qFrame );
frameLayout->setSizeConstraint( QLayout::SetMaximumSize );
frameLayout->addWidget( m_onlineChk, 0, Qt::AlignRight );
frameLayout->insertStretch( 0, 0 );
qFrame->setLayout( frameLayout );

// Put everything onto the main window.
m_centralLayout = new QGridLayout;

// Tab widget is 1st
m_centralLayout->addWidget( m_tabWidget, 1, 0, 1, 1);

// Frame is on bottom (just above the status bar)
m_centralLayout->addWidget( qFrame, 2, 0, 1, 1 );

m_pCentralWidget->setLayout( m_centralLayout );

m_pExitAct = new QAction( tr("E&xit"), this );
m_pExitAct->setShortcut( tr("Ctrl+Q") );
connect( m_pExitAct, SIGNAL(triggered()), this, SLOT(close()) );

m_pFileMenu = menuBar()->addMenu( tr("&File") );
m_pFileMenu->addAction( m_pExitAct );

show();
}


// DMX Table Tab Class
DMXTable::DMXTable( QWidget* pParent ) : QWidget( /*pParent*/ )
{
m_parent = reinterpret_cast<MainWin*>( pParent );

ui.setupUi( m_parent );
// ui.setupUi( this );

m_onlineGroupBox = new QGroupBox( tr("Online Devices") );

QScrollArea* pTableArea = new QScrollArea;
m_pOnlineTable = new QTableWidget( 1, 5, m_parent );

QStringList sLabels;
sLabels << "Status" << "Name" << "Type" << "IP Address" << "Model Name";

m_pOnlineTable->setHorizontalHeaderLabels( sLabels );
m_pOnlineTable->setColumnWidth( 0, 40 );
m_pOnlineTable->setColumnWidth( 1, 150 );
m_pOnlineTable->setColumnWidth( 2, 130 );
m_pOnlineTable->setColumnWidth( 3, 120 );
m_pOnlineTable->setColumnWidth( 4, 200 );

pTableArea->setWidgetResizable( true );
pTableArea->setWidget( m_pOnlineTable );

QGridLayout* layout = new QGridLayout;
layout->addWidget( pTableArea, 0, 0 );

m_onlineGroupBox->setLayout( layout );
m_onlineGroupBox->show();
}
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?

wysota
17th January 2007, 11:02
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.


MyWindow::MyWindow(QWidget *parent) : QMainWindow(parent){
ui.setupUi(this);
QTabWidget *tw = new QTabWidget(this);
setCentralWidget(tw);
QGroupBox *gb1 = new QGroupBox(tw);
tw->addPage(gb1, "first");
QHBoxLayout *l = new QHBoxLayout(gb1);
DMXTable *t1 = new DMXTable;
l->addWidget(t1);
//...
}

mclark
17th January 2007, 16:27
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!