PDA

View Full Version : QTabWidget Parenting Problem



mclark
17th January 2007, 18:48
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?


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

// create tabs
m_tabWidget = new QTabWidget( this );
ui.tabWidget = m_tabWidget;

m_tabDMX = new DMXTable( m_tabWidget );
ui.tabWidget->addTab( m_tabDMX, tr("Tab_1") );

m_tabAll = new AllTable( m_tabWidget );
ui.tabWidget->addTab( m_tabAll, tr("Tab_2") );

// 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 );
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);
m_centralLayout->setRowMinimumHeight( 1, 300 );
// Frame is on bottom (just above the status bar)
m_centralLayout->addWidget( qFrame, 2, 0, 1, 1 );
m_centralLayout->setRowMinimumHeight( 2, 30 );

setCentralWidget( m_pCentralWidget );
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();

} // MainWin


DMXTable::DMXTable( QWidget* pParent ) : QWidget( pParent )
{
m_parent = pParent;

createConfigTable();
createOnlineTable();

QVBoxLayout* layout = new QVBoxLayout( pParent );
layout->addSpacing( 25 );
layout->addWidget( m_configGroupBox );
layout->addWidget( m_onlineGroupBox );
}

void DMXTable::createConfigTable( void )
{
QStringList sLabels;
sLabels << "Status" << "Name" << "Type" << "IP Address" << "Model Name";

// Set up the table area.
m_configGroupBox = new QGroupBox( tr("Configured Devices"), m_parent );
m_pConfigTable = new QTableWidget( 1, 5, m_configGroupBox );

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

QHBoxLayout* layout = new QHBoxLayout( m_configGroupBox );
layout->addWidget( m_pConfigTable );
m_configGroupBox->setLayout( layout );
}


void DMXTable::createOnlineTable( void )
{
QStringList sLabels;
sLabels << "Status" << "Name" << "Type" << "IP Address" << "Model Name";

// Set up the table area.
m_onlineGroupBox = new QGroupBox( tr("Online Devices"), m_parent );
m_pOnlineTable = new QTableWidget( 1, 5, m_onlineGroupBox );

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, 150 );

QHBoxLayout* layout = new QHBoxLayout( m_onlineGroupBox );
layout->addWidget( m_pOnlineTable );
m_onlineGroupBox->setLayout( layout );
}

VireX
17th January 2007, 23:17
Try using m_tabwidget... dont do the ui.tabwidget = m_tabwidget... or tell us how ui.tabwidget is declared.

mclark
17th January 2007, 23:42
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.


m_tabWidget = new QTabWidget( this );

m_tabDMX = new DMXTable( m_tabWidget );
m_tabWidget->addTab( m_tabDMX, tr("Tab_1") );

m_tabAll = new AllTable( m_tabWidget );
m_tabWidget->addTab( m_tabAll, tr("Tab_3") );
...
m_centralLayout->addWidget( m_tabWidget, 1, 0, 1, 1);

jacek
18th January 2007, 11:23
DMXTable::DMXTable( QWidget* pParent ) : QWidget( pParent )
{
...
QVBoxLayout* layout = new QVBoxLayout( pParent );
...
}

void DMXTable::createConfigTable( void )
{
...
m_configGroupBox = new QGroupBox( ..., m_parent );
...
}


void DMXTable::createOnlineTable( void )
{
...
m_onlineGroupBox = new QGroupBox( ..., m_parent );
...
}
You are creating those widgets on the tab's parent, not on the tab itself. Replace m_parent with "this".

mclark
18th January 2007, 15:20
Thanks to both VireX and jacek! These were, indeed, my problems. All is now working as expected.