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. Center a QLabel in a Qwidget
QtWS25 Last Chance

Center a QLabel in a Qwidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 1.4k Views
  • 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.
  • M Offline
    M Offline
    maria.v
    wrote on last edited by
    #1

    I want to do like a toolbar, 3 buttons on the left and a label in the middle of the screen, no matter the size of my app.

    I tried to do this with QToolBar but can't add a QLabel, so I'm trying with a QWidget.

    My QWidget has Horizontal Layout, and I added 3 QPushButton and a QLabel.

    How can I center the QLabel to always stay in the middle?

    Screen Shot 2020-04-02 at 17.26.42.png

    Screen Shot 2020-04-02 at 17.25.48.png

    I have tried different things, like putting the three buttons in a widget and the label in another widget and play with the layouts and size constrains but I haven't achieved it

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Out of curiosity, why not use the toolbar feature of QMainWindow for that ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • M Offline
        M Offline
        maria.v
        wrote on last edited by
        #3

        I tried but I couldn't add a label. Is that possible?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maria.v
          wrote on last edited by maria.v
          #4

          Well, I saw that I could do something like

          ui->mainToolBar->addWidget(new QLabel("MY LABEL"));,

          But have the same problem of centering the label

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maria.v
            wrote on last edited by
            #5

            Okay! I research a little bit more and found what I was looking for. Example code:

                QLabel* label = new QLabel("MY LABEL");
                label->setAlignment(Qt::AlignCenter);
            
                QHBoxLayout* layout = new QHBoxLayout();
                layout->addWidget(label, Qt::AlignCenter);
            
                QWidget* wid = new QWidget;
                wid->setContentsMargins(0, 0, 0, 0);
                wid->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
                wid->setLayout(layout);
            
                QToolButton* p_butt_1 = new QToolButton;
                QToolButton* p_butt_2 = new QToolButton;
                QToolButton* p_butt_3 = new QToolButton;
            
                setStyleSheet("QToolButton { border: 1px solid magenta; }"); // Just for visibility in this example
            
                label->setStyleSheet("border: 1px solid blue;"
                                    "background-color: yellow;");
            
                ui->mainToolBar->addWidget(p_butt_1);
                ui->mainToolBar->addWidget(p_butt_2);
                ui->mainToolBar->addWidget(wid);
                ui->mainToolBar->addWidget(p_butt_3);
            
            
            1 Reply Last reply
            2

            • Login

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