Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved]Placing Widgets in the MainWindow

[Solved]Placing Widgets in the MainWindow

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peggy
    wrote on last edited by
    #1

    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:

    @
    QWidget* centralWidget = new QWidget;
    QHBoxLayout* layout = new QHBoxLayout;
    QLabel* tool1 = new QLabel(tr("Tool1: "));
    QComboBox* combo_tool1 = new QComboBox();
    combo_tool1->addItem(tr("Select the tool"));
    combo_tool1->setEnabled(true);

    layout->addWidget(tool1);
    layout->addWidget(combo_tool1);
    centralWidget->setLayout(layout);
    
    setCentralWidget(centralWidget);
    

    @

    How am I supposed to fix it. :(

    Thank you

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on last edited by
      #2

      There are basically two options:

      • pass an "alignment":http://developer.qt.nokia.com/doc/qt-4.8/qt.html#AlignmentFlag-enum to QLayout::addWidget() or
        @
        layout->addWidget(tool1, 0, Qt::AlignTop);
        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.
        @
        layout->addWidget(tool1);
        layout->addWidget(combo_tool1);
        layout->addStretch();

      QVBoxLayout* centralLayout = new QVBoxLayout;
      centralLayout->addLayout(layout);
      centralLayout->addStretch();

      centralWidget->setLayout(centralLayout);
      @

      1 Reply Last reply
      0
      • P Offline
        P Offline
        Peggy
        wrote on last edited by
        #3

        Thank you..it works:)

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lgeyer
          wrote on last edited by
          #4

          You're welcome ;-)

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved