Qt - 无法将图像放入表格中 [英] Qt - Cannot put an image in a table
本文介绍了Qt - 无法将图像放入表格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么使用以下代码我只得到一个空表小部件?
Why with the following code I just get an empty table widget?
QString imgPath = "C:\\path\\to\\image.jpg";
QImage *img = new QImage(imgPath);
QTableWidget *thumbnailsWidget = new QTableWidget;
QTableWidgetItem *thumbnail = new QTableWidgetItem;
thumbnail->setData(Qt::DecorationRole, QPixmap::fromImage(*img));
thumbnailsWidget->setColumnCount(5);
thumbnailsWidget->setRowCount(3);
thumbnailsWidget->setItem(0, 0, thumbnail);
setCentralWidget(thumbnailsWidget);
如何将图像放入QTableWidgetItem?
How can I put an image into a QTableWidgetItem?
谢谢。
PS:
我注意到表格并不是空的。单击不同的QTableWidgetItem元素,空的元素变为蓝色,坐标为[0,0]的元素以不同的方式突出显示:青色,左侧有一个细蓝色条...
P.S.:
I noticed that the table is not really empty. Clicking on the different QTableWidgetItem elements, the empty ones become blue, the one with coordinates [0,0] is highlighted differently: cyan with a thin blue bar on the left...
推荐答案
好的,我不得不重新缩放图像:
OK, I had to rescale the image:
thumbnail->setData(Qt::DecorationRole, QPixmap::fromImage(*img).scaled(100, 100));
这篇关于Qt - 无法将图像放入表格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文