January 19, 2012

Peggy Peggy
Lab Rat
26 posts

[Solved]Placing Widgets in the MainWindow

 

Hello Everyone,

I am Qt-bigginer and am writting an application that consists of QMenuBar,QToolBar, MainWindow and QStatusBar.

I am trying to set 2 Widgets in the center of main window.
Right down the toolbar a QWidget that consists of lable, button and combobox and down to it, a table that the values have to be changed by clicking on the button at the QWidget.
I have got a problem to place my widgets right down the QToolbar. In centralWidget, the lable,comboBox…will be shown in the middel of the window. Here is part of my code:

  1.     QWidget* centralWidget = new QWidget;
  2.     QHBoxLayout* layout = new QHBoxLayout;
  3.     QLabel*  tool1 = new QLabel(tr("Tool1: "));
  4.     QComboBox* combo_tool1 = new QComboBox();
  5.     combo_tool1->addItem(tr("Select the tool"));
  6.     combo_tool1->setEnabled(true);
  7.  
  8.     layout->addWidget(tool1);
  9.     layout->addWidget(combo_tool1);
  10.     centralWidget->setLayout(layout);
  11.  
  12.     setCentralWidget(centralWidget);

How am I supposed to fix it. :(

Thank you

3 replies

January 19, 2012

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

There are basically two options:

  • pass an alignment [developer.qt.nokia.com] to QLayout::addWidget() or
    1. layout->addWidget(tool1, 0, Qt::AlignTop);
    2. layout->addWidget(combo_tool1, 0, Qt::AlignTop);
  • use another QVBoxLayout and a stretch to place it on top. This is particularly useful if you want to add other widgets below. In addition, a stretch can be used to glue the combo box to the label.
    1. layout->addWidget(tool1);
    2. layout->addWidget(combo_tool1);
    3. layout->addStretch();
    4.  
    5. QVBoxLayout* centralLayout = new QVBoxLayout;
    6. centralLayout->addLayout(layout);
    7. centralLayout->addStretch();
    8.  
    9. centralWidget->setLayout(centralLayout);

January 19, 2012

Peggy Peggy
Lab Rat
26 posts

Thank you..it works:)

January 19, 2012

Lukas Geyer Lukas Geyer
Lab Rat
2074 posts

You’re welcome ;-)

Make sure you add “[Solved]” to your initial posts title to indicate that there is a solution inside.

 
  ‹‹ Classes that are restricted to the GUI thread      how to send variable from Dialog class to main window class? ››

You must log in to post a reply. Not a member yet? Register here!