16

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?

sashoalm
  • 75,001
  • 122
  • 434
  • 781

2 Answers2

28

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

This works:

QImage image = variant.value<QImage>();
sashoalm
  • 75,001
  • 122
  • 434
  • 781
  • 4
    In case you wonder: The same counts for any custom type you declare as a metatype and use in a user role. – leemes May 31 '13 at 14:13
0

I find the solution as follows:

QImage name_image = table_store_multi_model_->item(i_row,0)->data(Qt::DecorationRole).value().toImage();

Generally, we read data with data(), but here need a parameter "Qt::DecorationRole";

sashoalm
  • 75,001
  • 122
  • 434
  • 781
郭朋振
  • 1
  • 2