Rayven
17th March 2011, 17:24
I am trying to create a dynamic (read: at runtime) layout for some custom plots, using a QGridLayout. On initialization, a single plot is displayed. The user selects to add other plots through menus, changing the overall layout. Adding the first object works fine, however adding the second object causes the first object to occupy/span both widgets with the second object in the correct position. Each subsequent object added is in the correct position, but the first object still spans the entire layout (can "see" the widget in the grid spacing/background).
void RawDataWindow::layoutPlots()
{
QWidget *ui_plotAreaWidget = qFindChild<QWidget*>( this, "plotAreaWidget" );
QGridLayout *plotLayout = (QGridLayout*) ui_plotAreaWidget->layout();
// Remove all plots from the layout to adjust spacing and setup reparenting
for( int p = 0; p < mPlots.count(); p++ )
{
plotLayout->removeWidget( mPlots.at( p ).plotObject );
mPlots.at( p ).plotObject->setParent( 0 );
}
if( mPlots.count() == 1 )
{
plotLayout->addWidget( mPlots.at( 0 ).plotObject );
}
else if( mPlots.count() == 2 )
{
// Create a 1x2 grid
plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0 );
plotLayout->addWidget( mPlots.at( 1 ).plotObject, 0, 1 );
}
else if( mPlots.count() == 3 )
{
// 2x2 grid with the top plot spanning across both columns
plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0, 1, 2 );
plotLayout->addWidget( mPlots.at( 1 ).plotObject, 1, 0 );
plotLayout->addWidget( mPlots.at( 2 ).plotObject, 1, 1 );
}
else if( mPlots.count() == 4 )
{
// 2x2 grid
plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0 );
plotLayout->addWidget( mPlots.at( 1 ).plotObject, 0, 1 );
plotLayout->addWidget( mPlots.at( 2 ).plotObject, 1, 0 );
plotLayout->addWidget( mPlots.at( 3 ).plotObject, 1, 1 );
}
}
I believe this is causing further issues in that whichever object I add last does not recieve mousePress/Release/Move events, but this is just speculation until I resolve the layout issue.
Any ideas?
Windows 7
VS 2008
Qt 4.6.3
void RawDataWindow::layoutPlots()
{
QWidget *ui_plotAreaWidget = qFindChild<QWidget*>( this, "plotAreaWidget" );
QGridLayout *plotLayout = (QGridLayout*) ui_plotAreaWidget->layout();
// Remove all plots from the layout to adjust spacing and setup reparenting
for( int p = 0; p < mPlots.count(); p++ )
{
plotLayout->removeWidget( mPlots.at( p ).plotObject );
mPlots.at( p ).plotObject->setParent( 0 );
}
if( mPlots.count() == 1 )
{
plotLayout->addWidget( mPlots.at( 0 ).plotObject );
}
else if( mPlots.count() == 2 )
{
// Create a 1x2 grid
plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0 );
plotLayout->addWidget( mPlots.at( 1 ).plotObject, 0, 1 );
}
else if( mPlots.count() == 3 )
{
// 2x2 grid with the top plot spanning across both columns
plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0, 1, 2 );
plotLayout->addWidget( mPlots.at( 1 ).plotObject, 1, 0 );
plotLayout->addWidget( mPlots.at( 2 ).plotObject, 1, 1 );
}
else if( mPlots.count() == 4 )
{
// 2x2 grid
plotLayout->addWidget( mPlots.at( 0 ).plotObject, 0, 0 );
plotLayout->addWidget( mPlots.at( 1 ).plotObject, 0, 1 );
plotLayout->addWidget( mPlots.at( 2 ).plotObject, 1, 0 );
plotLayout->addWidget( mPlots.at( 3 ).plotObject, 1, 1 );
}
}
I believe this is causing further issues in that whichever object I add last does not recieve mousePress/Release/Move events, but this is just speculation until I resolve the layout issue.
Any ideas?
Windows 7
VS 2008
Qt 4.6.3