为什么使用以下代码我只得到一个空表小部件?

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?

谢谢。

附言:
我注意到该表不是真的为空。单击不同的QTableWidgetItem元素,空的元素变为蓝色,坐标为[0,0]的元素以不同的方式突出显示:青色,左侧带有细蓝条...

最佳答案

您几乎做得很好,但是请尝试控制img,例如:

QString imgPath = "C:\\path\\to\\image.jpg";
QImage *img = new QImage();
bool loaded = img->load(imgPath);
if (loaded)
{

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

    w.setCentralWidget(thumbnailsWidget);
} else {
    qDebug()<<"Image "<<imgPath<<" was not opened!";
}

希望对您有帮助!祝好运!

09-08 05:46