PDA

View Full Version : Set the image at the center in QLabel?



vishal.chauhan
14th August 2007, 09:20
Hi ALL,

I m using Qt 4.1.5 on my MAC.

I have a dialog in which i m showing the image on a Label and if the image is bigger than there is a scroll area to scroll it.

But if image is small then it shows it in the Upper left corner of the dialog but I want that the image should start from the center.

Im using the following code o do it..




QLabel *imageLabel = new QLabel(pFilePreviewDlg);
imageLabel->setAlignment (Qt::AlignCenter);
imageLabel->setBackgroundRole(QPalette::Base);
imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
imageLabel->setPixmap(pixmap);

QScrollArea *scrollArea = new QScrollArea(pFilePreviewDlg);
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(imageLabel);
scrollArea->setGeometry (2,0,460,490);




So if any body knows how I can set the image on the center on a dialog then plz help me.

Thanks.

marcel
14th August 2007, 09:46
imageLabel->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

vishal.chauhan
20th August 2007, 13:42
Thanks for reply.

Actually I have tried this but nothing changes.The image is still not in the center of the dialog.

jpn
20th August 2007, 13:47
Use QScrollArea::setAlignment() (http://doc.trolltech.com/4.3/qscrollarea.html#alignment-prop). And use a layout to place the scrollarea into the dialog instead of hardcoding its geometry.

vishal.chauhan
20th August 2007, 14:00
Thanks for reply.

But I m using Qt 4.1.5 and it does not have that property for QScrollArea.

wysota
20th August 2007, 15:07
Call setWidgetResizable(true) on the scroll area.