new to QT and I am planning to display images aligning beautifully with layouts.
I create 3 Qlabels and group them in vertical and horizontal layouts to construct a widget like:
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_3">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
in designer:
I use following to load my images and display images:
ui->setupUi(this);
src=cv::imread("image2.bmp");
cvtColor(src,src,CV_BGR2RGB);
img = QImage((const unsigned char*)(src.data),src.cols,src.rows,src.cols*src.channels(),QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(img).scaled(ui->label->width(), ui->label->height(),Qt::KeepAspectRatio));
ui->label_2->setPixmap(QPixmap::fromImage(img).scaled(ui->label_2->width(), ui->label_2->height(),Qt::KeepAspectRatio));
ui->label_3->setPixmap(QPixmap::fromImage(img).scaled(ui->label_3->width(), ui->label_3->height(),Qt::KeepAspectRatio));
displaying image spans 480*640, and I choose to keep the aspect ratio. however, the program runs like this: pic lays in screen shots here
the image is resizing in a surprisingly wrong way. I have try Qt::KeepAspectRatiobyExpanding
and Qt::ignoreAspectRatio
but none of these behavior as expect.
Any idea about this?
You can set the image to fill the label with: -
void QLabel::setScaledContents(bool)
So call: -
ui->label->setScaledContents(true);
ui->label_2->setScaledContents(true);
ui->label_3->setScaledContents(true);
Also note that you can set the pixmap in the designer. Just add the images as a resource, then set it on the label, in its properties: -
I cannot see the images you've pasted, but perhaps the problem is based on the size of QLabels: what is their size prior to:
ui->label->setPixmap(QPixmap::fromImage(img).scaled(ui->label->width(), ui->label->height(),Qt::KeepAspectRatio));
Perhaps it's very small or just enough to hold the "TextLabel" string, hence the weird resizing?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.