Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QVariant to QIcon/QPixmap/QImage

Tags:

c++

qt

qvariant

I want to extract a QIcon I've stored in one of a QTreeWidget's columns, as Qt::DecorationRole.

QTreeWidgetItem *item = ui->treeWidget->topLevelItem(index);
const QIcon &icon = item->data(0, Qt::DecorationRole)._howToConvert_();

However, I can only get the data as QVariant, and I could not find a function to convert from a QVariant to QIcon. Is it possible to do it?

like image 681
sashoalm Avatar asked May 31 '13 13:05

sashoalm


People also ask

How do you convert QImage to QPixmap?

A QPixmap object can be converted into a QImage using the toImage() function. Likewise, a QImage can be converted into a QPixmap using the fromImage(). If this is too expensive an operation, you can use QBitmap::fromImage() instead.

What is QPixmap?

The QPixmap class is an off-screen image representation that can be used as a paint device. The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device.


1 Answers

OK, found the answer in the docs for QVariant upon further inspection.

This works:

QImage image = variant.value<QImage>();
like image 56
sashoalm Avatar answered Oct 13 '22 09:10

sashoalm