QT显示图像,Qlabel布局不起作用

时间:2014-05-21 11:55:04

标签: c++ qt qt-creator

QT的新手,我计划显示与布局完美对齐的图像。

我创建3个Qlabels并将它们分组为垂直和水平布局,以构建一个小部件,如:

<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>

在设计师:

我使用以下来加载我的图像并显示图像:

 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));

显示图像范围480 * 640,我选择保持宽高比。 但是,程序运行如下: pic位于screen shots here

图像以惊人的错误方式调整大小。我尝试了Qt::KeepAspectRatiobyExpandingQt::ignoreAspectRatio,但这些行为都没有像预期的那样。

对此有何想法?

2 个答案:

答案 0 :(得分:2)

您可以设置图像以填充标签: -

void QLabel::setScaledContents(bool)

所以打电话: -

ui->label->setScaledContents(true);
ui->label_2->setScaledContents(true);
ui->label_3->setScaledContents(true);

另请注意,您可以在设计器中设置像素图。只需将图像添加为资源,然后在标签中将其设置在其属性中: -

enter image description here

答案 1 :(得分:0)

我无法看到您粘贴的图片,但问题可能是基于QLabel的尺寸:它们之前的尺寸是多少:

ui->label->setPixmap(QPixmap::fromImage(img).scaled(ui->label->width(), ui->label->height(),Qt::KeepAspectRatio));

也许它非常小或足以容纳&#34; TextLabel&#34;字符串,因此奇怪的大小调整?

相关问题