QVariant holds pointer data

QVariant holds pointer data

QVariant cannot hold pointer data by default, because the QVariant constructor that takes void * as parameter is private.

But through the Meta Type mechanism provided by QT, any pointer can be stored in QVariant.

Types need to be registered using the Q_DECLARE_METATYPE macro.

Q_DECLARE_METATYPE(QStandardItemModel*)

After that, you can use QVariant::fromValue to store data and QVariant::value to get data.

return QVariant::fromValue(model_);
//....
QStandardItemModel* model = some_value.value();

Using QVariant and Qt::ItemDataRole, which identifies the type of data in an item, makes it easy to store arbitrary data into the predefined models provided by Qt.

For example, use different Roles in the same QStandardItem to store multiple data.

custom data roles

enum CustomItemRole
{
    LevelModelRole =  Qt::UserRole + 1000,
    TimeModelRole = Qt::UserRole + 1010,
    FileModelRole = Qt::UserRole + 1020
};

Store character-specific data

type_of_level_item->setData(QVariant::fromValue(level_list_model),LevelModelRole);

Get data for a specific role

QVariant data = item->data(Qt::LevelModelRole);

http://windrocblog.sinaapp.com/?p=1166  

  

  

  

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324682096&siteId=291194637