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. Problem aligning widgets in QVBoxLayout

Problem aligning widgets in QVBoxLayout

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 7.8k 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
    tonygrim
    wrote on last edited by
    #1

    I have a MainWindow with a centralWidget that contains a main QHBoxLayout. This main layout in turn contains a QHBoxLayout and, to the right of this, a QVBoxLayout. The QVBoxLayout contains three QLabel widgets that I have added using vboxlayout->addWidget(aWidget, 0, Qt::AlignHCenter). Problem is that no matter what alignment flags I pass to addWidget my labels always end up aligned to the left of the layout.

    Abridged code:

    @
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {
    QWidget centralWidget = new QWidget;

    centralLayout = new QHBoxLayout;
    
    aclockLayout = new QHBoxLayout;
    aclockLayout->addWidget(aclock, 0, Qt::AlignCenter);
    
    counterLayout = new QVBoxLayout;
    counterlayout->addWidget(vbclock, 0, Qt::AlignHCenter);
    counterlayout->addWidget(dtdisplay, 0 Qt::AlignHCenter);
    counterLayout->addWidget(tzdisplay, 0, Qt::AlignHCenter);
    
    centralLayout->addLayout(aclockLayout);
    centralLayout->addLayout(counterLayout);
    centralWidget->setLayout(centralLayout);
    setCentralWidget(centralWidget);
    

    }
    @

    All I want is for my three widgets to be aligned on the vertical center line of the QVBoxLayout. What am I missing here?

    Dona nobis pacem

    1 Reply Last reply
    1
    • frederikF Offline
      frederikF Offline
      frederik
      wrote on last edited by
      #2

      QLabel is by default left-aligned and takes up all available space to the right. Try setting the text alignment of the labels.
      http://doc.qt.digia.com/stable/qlabel.html#alignment-prop

      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