2013-06-01 28 views
0
#include "mainwindow.h" 
#include "ui_mainwindow.h" 
#include <stdio.h> 
#include <iostream> 
#include <QDialog> 
#include <opencv2\video\video.hpp> 
#include <opencv2\opencv.hpp> 
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/flann/miniflann.hpp" 
#include <QLabel> 
#include <QScrollArea> 
#include <QScrollBar> 
cv::Mat image1; 
MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow) 
{ 
image1 = cv::imread("D:\\picture.jpg"); 
QImage  qimage1((uchar*)image1.data,image1.cols,image1.rows,image1.step,QImage::Format_RGB888); 
ui->label->setPixmap(QPixmap::fromImage(qimage1)); 
} 

MainWindow::~MainWindow() 
{ 
delete ui; 
} 

我的圖片大小爲720 * 1280。我想用尺寸爲600 * 600的標籤展示這張照片。但是,它只顯示圖片的一部分。所以我的問題是如何在不改變圖片大小的情況下顯示整個圖片。在Qlabel中顯示圖片時發生錯誤

+0

爲什麼要使用加載OpenCV的形象?您是否檢查過創建的圖像是否包含您期望的所有數據?例如,將圖像保存到文件中:'qimage1.save(「D:/picture-saved.jpg」)' – 2013-06-01 15:59:45

+0

你究竟想要做什麼?你想重新調整圖像尺寸爲600 * 600或不?無法將大小爲720 * 1280的圖像顯示爲600 * 600「而不更改圖片大小」。 – 2013-06-01 16:08:02

+0

我想使用滾動條,但我不知道如何將其添加到標籤 –

回答

3

您可以使用功能QPixmap::scaled(),看文檔here和實例here

在你的情況下,它會是這樣的:

ui->label->setPixmap(QPixmap::fromImage(qimage1).scaled(QSize(600,600), Qt::KeepAspectRatio)); 

這不會影響到圖像本身,它將構建的QPixmap從圖像中縮放到適合600x600 Qlabel,並保持寬高比。希望這會幫助你。順便說一句,你不需要使用喲OpenCV的只是讀取圖像,在Qt的QImage類可以QImage的建設只用即QString映像路徑:QImage myImg("D:\\picture.jpg");

編輯(抱歉耽擱):

要添加QScrollArea ,你必須在構造函數來創建它(讓我們假設,在你的主窗口你只有QLabel和QScrollArea)是這樣的:

// constructor, right after ui->setupUi(this); 
QScrollArea *scroll=new QScrollArea(this); // creating instance of QScrollarea with mainwindow as it's parent 
scroll->setWidget(ui->label); // sets widget, that you want to have scrollbars 
this->setCentralWidget(scroll); // sets scrollarea as centralwidget 
+0

我想使用滾動條,但我不知道如何將其添加到標籤 –

+0

已更新答案 – Shf