negritot
28th September 2010, 20:05
Hello all. I want to store smart pointers in QVariants for purposes of working with Qt's model/view system. However, it appears that QVariant.operator== doesn't work when the type is a smart pointer. For example:
#include <boost/shared_ptr.hpp>
Q_DECLARE_METATYPE(boost::shared_ptr<int>);
Q_DECLARE_METATYPE(int*);
void testQVariant()
{
int* rawInt = new int(5);
QVariant rawVariant = QVariant::fromValue(rawInt);
QVariant rawVariant2 = QVariant::fromValue(rawInt);
bool equal = rawVariant2 == rawVariant;
boost::shared_ptr<int> smartInt(new int(5));
QVariant smartVariant = QVariant::fromValue(smartInt);
QVariant smartVariant2 = QVariant::fromValue(smartInt);
equal = smartVariant2 == smartVariant;
}
The QVariants are equal when using raw pointers, but not when using smart pointers (boost::shared_ptr in this case, but I see the same behavior with another smart pointer type, as well).
The upshot is that I can store these in a model (as Qt::UserRole data), but I'm unable to ask the model to find them (match() always returns an index of -1). Any ideas?
#include <boost/shared_ptr.hpp>
Q_DECLARE_METATYPE(boost::shared_ptr<int>);
Q_DECLARE_METATYPE(int*);
void testQVariant()
{
int* rawInt = new int(5);
QVariant rawVariant = QVariant::fromValue(rawInt);
QVariant rawVariant2 = QVariant::fromValue(rawInt);
bool equal = rawVariant2 == rawVariant;
boost::shared_ptr<int> smartInt(new int(5));
QVariant smartVariant = QVariant::fromValue(smartInt);
QVariant smartVariant2 = QVariant::fromValue(smartInt);
equal = smartVariant2 == smartVariant;
}
The QVariants are equal when using raw pointers, but not when using smart pointers (boost::shared_ptr in this case, but I see the same behavior with another smart pointer type, as well).
The upshot is that I can store these in a model (as Qt::UserRole data), but I'm unable to ask the model to find them (match() always returns an index of -1). Any ideas?