[Qt-interest] little help with layouts
Alessandro Portale
Alessandro.Portale at trolltech.com
Mon Apr 27 13:14:05 CEST 2009
Ciao Luca,
for QMainWindow, you actually need to create that main container widget
which you assign your mainLayout to. Then you simply make the container
widget the central widget for your QMainWindow:
http://doc.qtsoftware.com/latest/qmainwindow.html#setCentralWidget
In QDialog, your current code should work fine, however.
Hope that helps,
Alessandro
Luca Ferrari wrote:
> Hi,
> moving my first steps in QT I got a behaviour I cannot understand. The window
> should display two labels and a few buttons, with different layouts, but I'm
> not getting what is wrong with the following code (that shows nothing):
>
>
> MainWindow::MainWindow(QWidget *parent)
> : QMainWindow(parent)
> {
>
> QHBoxLayout* mainLayout = new QHBoxLayout();
> this->setLayout( mainLayout );
>
> // a container for labels
> QWidget* container = new QWidget( );
> this->signalLabel = new QLabel("No signal yet!");
> this->slotLabel = new QLabel("No slot yet!");
> QVBoxLayout* layout = new QVBoxLayout( );
> layout->addWidget( this->signalLabel );
> layout->addWidget( this->slotLabel );
>
>
> QWidget* buttonContainer = new QWidget( );
> QVBoxLayout* buttons = new QVBoxLayout( );
> buttonContainer->setLayout( buttons );
> for(int i = 0; i < 2; i ++ ){
> QPushButton* currentButton = new QPushButton( QString::number(i) );
> buttons->addWidget( currentButton );
> }
>
>
> mainLayout->addWidget( container );
> mainLayout->addWidget( buttonContainer );
>
>
>
> }
>
> The idea is:
> 1) set the main layout of the window
> 2) create the labels into a container widget and add them to such container
> using its layout;
> 3) create a container for buttons and add buttons to the container
> 4) add the two containers to the above main layout.
>
> Any suggestion is welcome.
>
> Thanks,
> Luca
More information about the Qt-interest-old
mailing list