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. qt label alignment center
Qt 6.11 is out! See what's new in the release blog

qt label alignment center

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 2.4k 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.
  • I Offline
    I Offline
    IknowQT
    wrote on last edited by
    #1

    38592f70-bf27-46a3-a2c9-6e419f2dfa3e-image.png

    I put a label and a button on the vertical layout.
    I want to put the label in the center, what should I do?

    Q Thank YouT 2 Replies Last reply
    0
    • I IknowQT

      38592f70-bf27-46a3-a2c9-6e419f2dfa3e-image.png

      I put a label and a button on the vertical layout.
      I want to put the label in the center, what should I do?

      Q Offline
      Q Offline
      Qt embedded developer
      wrote on last edited by Qt embedded developer
      #2

      @IknowQT If possible please take horizontal layout there when you can add label and pushbutton at that time add layout stretch for your label.

      I 1 Reply Last reply
      0
      • JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by JoeCFD
        #3

        create a horizontal layout. Align your label to the center and your button to the right. You may need some margin on the right side as well for your button.

        1 Reply Last reply
        0
        • Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by Cobra91151
          #4

          @IknowQT

          Hello!

          I suggest you check out the setAlignment (https://doc.qt.io/qt-5/qlayout.html#setAlignment) method and try it for your vertical layout. You can use the addWidget (https://doc.qt.io/qt-5/qboxlayout.html#addWidget) method and set the alignment specifically for your label, for example:

          Code:
          setAlignment example

          QVBoxLayout *mainLayout = new QVBoxLayout();
          mainLayout->setAlignment(Qt::AlignHCenter);
          mainLayout->addWidget(ui->label);
          

          addWidget exmaple

          QVBoxLayout *mainLayout = new QVBoxLayout();
          mainLayout->addWidget(ui->label, 0, Qt::AlignHCenter);
          

          Result:
          QLabel_in_the_center_example.gif

          Also, you can combine the vertical and horizontal layouts by using the addLayout (https://doc.qt.io/qt-5/qboxlayout.html#addLayout) method. In your question is unclear where the button should be placed, so please specify it and I will edit my code. Happy coding!

          1 Reply Last reply
          2
          • I IknowQT

            38592f70-bf27-46a3-a2c9-6e419f2dfa3e-image.png

            I put a label and a button on the vertical layout.
            I want to put the label in the center, what should I do?

            Thank YouT Offline
            Thank YouT Offline
            Thank You
            wrote on last edited by
            #5

            @IknowQT

            Another option to @Cobra91151 's answer is to use spacers.

            Use spacers on all side and use Grid layout.

            Let's make QT free or It will go forever

            TRUE AND FALSE <3

            1 Reply Last reply
            0
            • Q Qt embedded developer

              @IknowQT If possible please take horizontal layout there when you can add label and pushbutton at that time add layout stretch for your label.

              I Offline
              I Offline
              IknowQT
              wrote on last edited by
              #6

              @Qt-embedded-developer

              3b39c900-8e90-4c93-9eb5-c3364bc806e0-image.png

              this->ui.horizontalLayout->setStretch(0, 10);
              this->ui.horizontalLayout->setStretch(1, 300);
              this->ui.horizontalLayout->setStretch(2, 10);
              

              I changed it to a horizontal layout, but the middle is not aligned. The qt alignment is also centered.

              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