I'm having a hard time of converting std::string to QVariant and QVariant back to std::string. In both cases I end up with empty value (default QVariant, just like it was initialized with no parameters) and empty std::string ("").
These are relevant parts of my code:
bool TestItemListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
// processing of other function arguments, checks, etc.
(*myData)[row].setName(value.value<std::string>());
// here I end up with ""
}
QVariant TestItemListModel::data(const QModelIndex &index, int role) const
{
// again all the checks, etc.
return QVariant::fromValue<std::string>((*myData)[row].getName());
}
I found a question similar to this one and this is how it was meant to work. I also did additional thing the answer mentioned, so in my main.cpp I have this:
qRegisterMetaType<std::string>("std::string");
and in my testitemlistmodel.h I have this (before class declaration):
Q_DECLARE_METATYPE(std::string)
I use Qt5.8.
EDIT
I found the source for this type of conversion: http://ift.tt/2tMEgvR. Now I just realized it's quite old and may not work anymore. If that's the case, what would be the best way of doing that?
Aucun commentaire:
Enregistrer un commentaire