2014-05-21 42 views
0

QT新和我打算顯示圖像與佈局精美對齊。QT顯示圖像與Qlabel在佈局不起作用

創建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和I選擇保持寬高比。然而,程序像這樣運行: pic lays in screen shots here

圖像以令人驚訝的錯誤方式調整大小。我已經嘗試Qt::KeepAspectRatiobyExpandingQt::ignoreAspectRatio,但這些行爲都不像預期的那樣。

對此有何想法?

+0

你能否詳細說明「走錯了路」?此外,圖像是白色/空白(或者它只是我?) – hauron

+0

這個問題來自這樣一個事實,即當您執行'setPixmap'指令時標籤大小可能具有虛假值,並且標籤僅在稍後重新調整大小。我將賭注Merlin069選項以及'ui-> label-> setPixmap(QPixmap :: fromImage(img))'將解決您的問題。 – UmNyobe

回答

2

您可以設置圖像與填寫標籤: -

void QLabel::setScaledContents(bool) 

於是撥打: -

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

另外請注意,您可以設置在設計師的像素圖。只需添加圖片作爲一種資源,然後將其設置在標籤上,在其屬性: -

enter image description here

+0

嗨,它的工作原理!但似乎忽略了縱橫比和扭曲標籤完全扭曲的形象。有什麼辦法可以扭曲標籤的最大值並保持寬高比? – flankechen

+0

從QPixmap中讀取尺寸並設置QLabel中的最大寬度/高度 – TheDarkKnight

0

我不能看到您貼上了圖片,但也許這個問題是基於QLabels的大小:什麼是他們的大小之前:

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

也許是非常小的,或只是足以容納「TextLabel」字符串,因此奇怪的大小調整?

+0

悲傷,我看不到我上傳的圖像,或者,我上傳他們在鏈接中的其他應用程序。我很確定Qlabels的大小並不是那麼小,例如,我的標籤geometrys(1,1),215 * 171中的一個。 – flankechen

+0

@flankechen您可以使用看起來像圖像框的圖標直接在您的問題上發佈圖像。 – UmNyobe