[Qt-interest] Shrinking parent widget when child hidden
Brad Howes
howes at ll.mit.edu
Thu Apr 1 19:44:00 CEST 2010
On Apr 1, 2010, at 12:07 PM, George Ryan wrote:
> Hello,
>
> I have a dialog that I want to have vertically resize automatically when
> I hide one of its children in a vertical layout. For example:
>
> ---------
> | ----- |
> | | W1 | |
> | ----- |
> | ----- |
> | | W2 | |
> | ----- |
> ---------
>
> to
>
> ---------
> | ----- |
> | | W2 | |
> | ----- |
> ---------
>
> I have tried various combinations of layouts and setting minimum size
> hints, but I haven't yet come across a good solution.
>
> Has anyone done this before or have a good idea on how to implement it?
This works for me:
// ParamEditor.h:
#include <QtGui/QDialog>
class QCheckBox;
class QFrame;
class QPushButton;
class ParamEditor : public QDialog
{
Q_OBJECT
public:
ParamEditor();
private slots:
void advancedClicked( bool state );
private:
void makeParam( const QString& name, bool advanced );
QFrame* widgetContainer_;
QCheckBox* advanced_;
QPushButton* ok_;
};
// ParamEditor.cc
#include <QtGui/QApplication>
#include <QtGui/QCheckBox>
#include <QtGui/QFrame>
#include <QtGui/QHBoxLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QSpinBox>
#include <QtGui/QVBoxLayout>
#include "ParamEditor.h"
ParamEditor::ParamEditor()
: QDialog( 0 ), advanced_( 0 )
{
setModal( false );
setWindowTitle( "Parameter Editor" );
//
// Create the layout for the entire window. This will hold two other
// layouts, an hbox one for dialog buttons and another vbox one for the
// adjustable widgets collection.
//
QVBoxLayout* vbox = new QVBoxLayout( this );
vbox->setSizeConstraint( QLayout::SetFixedSize );
vbox->setMargin( 9 );
vbox->setSpacing( 6 );
//
// Place the adjust widget collection in a frame
//
widgetContainer_ = new QFrame( this );
widgetContainer_->setFrameStyle( QFrame::NoFrame );
vbox->addWidget( widgetContainer_ );
vbox->addStretch( 1 );
//
// Create an hbox to hold the 'Advanced' checkbox and the OK button
//
QHBoxLayout* buttonLayout( new QHBoxLayout );
vbox->addLayout( buttonLayout );
advanced_ = new QCheckBox( "Advanced", this );
advanced_->setChecked( false );
//
// Adjust the checkbox tool tip to reflect its state
//
advancedClicked( false );
connect( advanced_, SIGNAL( clicked( bool ) ),
SLOT( advancedClicked( bool ) ) );
buttonLayout->addWidget( advanced_ );
buttonLayout->addStretch();
//
// Simple OK button that will close the window
//
ok_ = new QPushButton( "OK", this );
ok_->setEnabled( true );
ok_->setToolTip( "Select to apply changes and close editor" );
buttonLayout->addWidget( ok_ );
connect( ok_, SIGNAL( clicked() ), SLOT( close() ) );
//
// Create another vbox layout for the widget collections
//
vbox = new QVBoxLayout( widgetContainer_ );
vbox->setMargin( 9 );
vbox->setSpacing( 6 );
makeParam( "One", false );
makeParam( "Two", false );
makeParam( "Three", true ); // advanced
makeParam( "Four", true ); // advanced
makeParam( "Five", false );
adjustSize();
}
void
ParamEditor::advancedClicked( bool state )
{
if ( state )
advanced_->setToolTip( "Click to hide advanced configuration "
"parameters" );
else
advanced_->setToolTip( "Click to show advanced configuration "
"parameters" );
}
void
ParamEditor::makeParam( const QString& name, bool advanced )
{
//
// Create a new frame to group the label and QSpinBox for the parameter
//
QFrame* frame = new QFrame( widgetContainer_ );
frame->setFrameStyle( QFrame::Box | QFrame::Sunken );
widgetContainer_->layout()->addWidget( frame );
//
// Layout the label and spin box horizontally
//
QHBoxLayout* layout = new QHBoxLayout( frame );
layout->setMargin( 6 );
layout->addStretch();
QLabel* label = new QLabel( name, frame );
label->setAlignment( Qt::AlignRight | Qt::AlignVCenter );
layout->addWidget( label );
QSpinBox* value = new QSpinBox( frame );
value->setValue( 0 );
layout->addWidget( value );
//
// Advanced items are initially hidden
//
frame->setVisible( ! advanced );
if ( advanced ) {
//
// Visibility of advanced items depends on the advanced checkbox state.
//
frame->setVisible( false );
connect( advanced_, SIGNAL( clicked( bool ) ), frame,
SLOT( setVisible( bool ) ) );
}
}
int
main( int argc, char* argv[] )
{
QApplication* app = new QApplication( argc, argv );
ParamEditor* paramEditor = new ParamEditor;
paramEditor->show();
paramEditor->raise();
return app->exec();
}
# ParamEditor.pro
TEMPLATE = app
CONFIG += QT
TARGET = ParamEditor
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += ParamEditor.h
SOURCES += ParamEditor.cc
--
Brad Howes
Group 42
MIT Lincoln Laboratory • 244 Wood St. • Lexington, MA 02173
Phone: 781.981.5292 • Fax: 781.981.3495 • Secretary: 781.981.7420
More information about the Qt-interest-old
mailing list