Problem aligning widgets in QVBoxLayout

  • 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);


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

  • QLabel is by default left-aligned and takes up all available space to the right. Try setting the text alignment of the labels.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.