Sharanabasu Angadi
Sharanabasu Angadi

Reputation: 4372

Image is not Displaying at center in QtForm

Hi I am using QLabel to show an image in QtForm. My code goes like this

QVBoxLayout *layout = new QVBoxLayout;
QHBoxLayout *hLayout = new QHBoxLayout;
layout->setMargin(5);
QLabel *imageLabel = new QLabel;
QPixmap pixmap("/images/test.jpg");
imageLabel->setPixmap(pixmap);
imageLabel->setMask(pixmap.mask());
imageLabel->setMinimumSize(160, 160);
imageLabel->resize(500, 320);

layout->addWidget(imageLabel,0,Qt::AlignTop | Qt::AlignCenter);
hLayout->addItem(layout);

widget->setLayout(hLayout);

scrollArea->setWidget(widget);
setCentralWidget(scrollArea);

but the image is displaying at the left corner can any one suggest me to bring the image to centre to the form

Upvotes: 5

Views: 6389

Answers (3)

Marco167
Marco167

Reputation: 391

If the setAlignment function didn't work alone, then use it with setSizePolicy.

imageLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
imageLabel->setAlignment(Qt::AlignCenter);

Upvotes: 0

Sharanabasu Angadi
Sharanabasu Angadi

Reputation: 4372

I got solution,

QPixmap pixmap("images/test.png");
imageLabel->setPixmap(pixmap);
imageLabel->setMinimumSize(160, 160);
imageLabel->resize(500, 320);
imageLabel->setAlignment(Qt::AlignCenter);


scrollArea->setWidget(widget);
setCentralWidget(imageLabel);

Upvotes: 7

Nikos C.
Nikos C.

Reputation: 51840

Change the alignment of your QLabel's content:

imageLabel->setAlignment(Qt::AlignCenter);

Upvotes: 4

Related Questions