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. How to relocate and resize the widgets in the mainwindow
Qt 6.11 is out! See what's new in the release blog

How to relocate and resize the widgets in the mainwindow

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 9.0k 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.
  • T Offline
    T Offline
    thomas11live.com.sg
    wrote on last edited by
    #1

    Dear All,
    I am new to QT programming and how can I resize and relocate the widgets like buttons and labels proportionally to the main window's size? How can I implement it? If possible, can I have a sample program?
    Thanks,
    Thomas

    1 Reply Last reply
    0
    • A Offline
      A Offline
      AcerExtensa
      wrote on last edited by
      #2

      There is Layout system in Qt for such kind of things. Take a look here: "Layout Examples":http://qt-project.org/doc/qt-4.8/examples-layouts.html

      God is Real unless explicitly declared as Integer.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sam
        wrote on last edited by
        #3

        You can also check these "video tutorials":http://www.voidrealms.com/tutorials.aspx?filter=qt It's a good start for beginners.

        1 Reply Last reply
        0
        • T Offline
          T Offline
          thomas11live.com.sg
          wrote on last edited by
          #4

          Hi,
          What I did is I have labels in the main window.
          They are put into the vertical layout.
          Then the vertical layout is put inside the group box.
          I like to make when the main window size is changed, all sizes are changed proportionally. The code is as follow.
          But when I run, even though my window size is changed, the widget sizes are not changed, also their positions. Where could be the error?
          Thanks
          void MainWindow::ui_Implementation(Ui::MainWindow *ui)
          {
          ui->label->setText("Camera serial number:");
          ui->label_3->setText("Camera model:");
          ui->label_5->setText("Interface:");
          ui->label_7->setText("Image size:");

          QVBoxLayout *mainLayout = new QVBoxLayout;
          mainLayout->addWidget(ui->groupBox);
          mainLayout->addLayout(ui->verticalLayout);
          setLayout(mainLayout);
          

          }

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sam
            wrote on last edited by
            #5

            Since you are using a QMainWindow you need to add a QWidget and set it as centralWidget eg

            @QLabel *labelOne = new QLabel("Camera serial number:");
            QLabel *labelTwo = new QLabel("Camera model:");
            QLabel *labelThree = new QLabel("Interface:");
            QLabel *labelFour = new QLabel("Image Size:");

            QVBoxLayout *vLayout = new QVBoxLayout;
            vLayout->addWidget(labelOne);
            vLayout->addWidget(labelTwo);
            vLayout->addWidget(labelThree);
            vLayout->addWidget(labelFour);

            QGroupBox *grpBox = new QGroupBox;
            grpBox->setLayout(vLayout);

            QVBoxLayout *mainLayout = new QVBoxLayout;
            mainLayout->addWidget(grpBox);

            QWidget *cWidget = new QWidget(this);
            cWidget->setLayout(mainLayout);

            setCentralWidget(cWidget);
            @

            Check if this fulfill your requirements.

            1 Reply Last reply
            0
            • T Offline
              T Offline
              thomas11live.com.sg
              wrote on last edited by
              #6

              Hi Sam,
              Thanks for the sample.
              I did that but the problem is all label are equally spaced and take the whole area of the main window.
              Actually I want to have fixed at a position and grow/shrink according to the main window size.
              Where can I change that?
              Thanks,
              Thomas

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #7

                So which of the following you need

                1. Grow/Shrink Horizontally.
                2. Grow/Shrink Vertically
                3. Both (currently used)

                Anyhow you can add a stretch to the layout. Since it is a VBoxLayout, if u adld a stretch the labels will grow/shrink horizontally.

                eg:

                @QVBoxLayout *mainLayout = new QVBoxLayout;
                mainLayout->addWidget(grpBox);
                mainLayout->addStretch();
                @

                You can attach an image of ur requirement .

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rahul Das
                  wrote on last edited by
                  #8

                  Reimplement "resize event":http://doc.qt.nokia.com/latest/qwidget.html#resizeEvent.


                  Declaration of (Platform) independence.

                  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