Issue with QLabel alignment properties
Solved
General and Desktop
-
Hi everyone,
I have a stupid issue, but cannot find any solutions for this. I think I may missing some understanding :
I want to display an image in a Scroll Area, and I want it at the center. I made the following code:
void MainWindow::setImage(const QImage &newImage) { scrollArea = new QScrollArea; imageLabel = new QLabel; scrollArea->setWidget(imageLabel); scrollArea->setFrameShape(QFrame::NoFrame); scrollArea->setStyleSheet("QScrollBar:vertical{width: 50px;}"); ui->horizontalLayout->addWidget(scrollArea); image = newImage; if (image.colorSpace().isValid()) image.convertToColorSpace(QColorSpace::SRgb); imageLabel->setPixmap(QPixmap::fromImage(image)); imageLabel->adjustSize(); imageLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter); }
It works, but the problem is that the image contained in the label remain aligned to the left, regardless of the setAlignment(Qt::AlignHCenter | Qt::AlignVCenter) instruction.
Any ideas why ?
PS: I can't use QtCreator to change it.
-
@CLBSII said in Issue with QLabel alignment properties:
imageLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
You've set the alignment to your label, not to your
QScrollArea