Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to equally distribute QLabels in QStatusBar



  • Hello, how can I equally distribute (for example three) QLabels in QStatusBar? I tried to add QHBoxLayout. But I cannot add Layout to QStatusBar, as it said it already has layout:

    QWidget::setLayout: Attempting to set QLayout "" on QStatusBar "", which already has a layout
    

  • Lifetime Qt Champion

    Hi,

    What about putting that layout on a "container" widget and then put that widget in the QStatusBar ?



  • @SGaist . Thanks, I already did similar as you suggested.

     statusBar = new QStatusBar();
     QLabel *statuslabel1 = new QLabel;
     QLabel *statuslabel2 = new QLabel;
     QLabel *statuslabel3 = new QLabel;
     statuslabel1->setText("Left");
     statuslabel2->setText("Centre");
     statuslabel3->setText("Right");
     statusLayout = new QHBoxLayout;
     statusLayout->addWidget(statuslabel1);
     statusLayout->addStretch();
     statusLayout->addWidget(statuslabel2);
     statusLayout->addStretch();
     statusLayout->addWidget(statuslabel3);
     QWidget *container = new QWidget;
     container->setLayout(statusLayout);
     statusBar->addWidget(container);
    

    This gave me like this statusBar.
    0_1537946345690_225c22e8-9304-40d7-bdcb-9cda8fa78cdf-image.png .

    This is not what I wanted, I wanted Left label to be at left, Centre to be at centre and Right label at right. Of course, they should retain their position even when the window is resized or at least when the application opens.



  • What I dont understand is why the statusBar is only 50% of available width.

    0_1537951833917_c8cce369-7e1f-4bfd-81fa-a5a9ce8f3bb2-image.png


  • Qt Champions 2019

    @milan You should rather do

    statusBar->layout()->addWidget(container);
    


  • @jsulm . I get like this after the change. I removed widget stylesheets too in meantime.
    0_1537953472135_93ff44de-6302-444d-9a99-0523f2ce00e2-image.png .

    You can see now, container widget is in right.


Log in to reply