QScrollArea - TIFF图像未显示在标签中

时间:2015-09-21 18:10:33

标签: image qt tiff qwidget qscrollarea

我实际上正在尝试使用Qt 5.5开发图形用户界面(GUI),其目标是加载和打开Tiff图像(16.1 MB)。

我使用以下代码成功加载并显示了TIFF图像:

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;

QLabel *label = new QLabel(&fenetre);
QPixmap pixmap("D:/IS5337_2_010_00092.tif"); 
label->setPixmap(pixmap);
label->resize(pixmap.size());

window.show();

return app.exec();
}

但是,当我尝试使用此代码添加scrollarea时:

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget window;

QLabel *label = new QLabel(&fenetre);
QPixmap pixmap("D:/IS5337_2_010_00092.tif");
label->setPixmap(pixmap);
label->resize(pixmap.size());

QScrollArea *scrollArea = new QScrollArea;
scrollArea->setBackgroundRole(QPalette::Light);
scrollArea->setWidget(label);

window.show();

return app.exec();
}

它根本不起作用... GUI没有显示TIFF图像......只是一个空的灰色窗口。

你能帮我吗?

1 个答案:

答案 0 :(得分:1)

您使用的scrollArea对象应该与某个父窗口小部件连接:

// in that case we have window object of QWidget type that is running as
// main window for the app and that has label for embedding the pixmap in it

QScrollArea *scrollArea = new QScrollArea(&window); // set window as parent for scroll
scrollArea->setBackgroundRole(QPalette::Light);

QLabel *label = new QLabel;   // (scrollArea) either set scroll as parent for label
// put the image in label, etc.
scrollArea->setWidget(label); // or set is as widget for scroll
// put the image in label, etc.