reza
reza

Reputation: 100

Center QLabel derived widget inside a QScrollArea

How should I center my QLabel derived widget papyrus inside a QScrollArea?

QScrollArea *scroll_area = new QScrollArea(this);
scroll_area->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
scroll_area->setWidgetResizable(true);
scroll_area->setBackgroundRole(QPalette::Dark);
papyrus = new Papyrus(scroll_area);
scroll_area->setWidget(papyrus);
setCentralWidget(scroll_area);
resize(800, 600);

This is the snippet I am using but my widget sticks to the top left...

Upvotes: 1

Views: 476

Answers (1)

Alexander Chernin
Alexander Chernin

Reputation: 444

(main.cpp example):

#include <QApplication>
#include "MainWindow.h"

#include <QScrollArea>
#include <QLabel>
#include <QHBoxLayout>

int main(int argc, char** argv)
{
    QApplication a(argc, argv);

    QLabel label("Label");
    label.setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

    QScrollArea area;
    area.setWidgetResizable(true);

    area.setWidget(&label);
    area.show();

    return a.exec();
}

Upvotes: 0

Related Questions