PDA

View Full Version : QTreeView Drag and Drop with MyData



baray98
19th August 2009, 21:14
I have QTreeView which is connected to QStandardItemModel and I would like to support D&D in my view.

QStandardItem in my model contains my "MyData" as QVariant in Qt::UserRole like this


s_SqlListInfo info;
//fill data
[...]

var.setValue(info);
item->setData(var,Qt::UserRole);
item->setData(info.queryName,Qt::DisplayRole);
item->setData(QIcon(":/ListIcon.png"),Qt::DecorationRole);
item->setFlags(item->flags()& ~Qt::ItemIsEditable);


I have declared myData like this



struct s_SqlListInfo
{
QVariant queryName;
QVariant dataBaseName;
QList <QString> reqTableList;
QString sqlStatement;
};
// Qt MetaType Declaration
Q_DECLARE_METATYPE(s_SqlListInfo)



Now, When I start dragging on my treeView it will assert with this message


ASSERT failure in QVariant::save "Invalid type to save", file kernel\qvariant.cpp line 1951


It seems like QVariant can not save my "MyData" that's why its asserting .... how can I fix this problem?

baray98

numbat
20th August 2009, 14:00
You can make your type savable by providing stream operators and calling qRegisterMetaTypeStreamOperators (http://doc.trolltech.com/latest/qmetatype.html#qRegisterMetaTypeStreamOperators). It might be easier to just store the data individually, in Qt::UserRole + 1, etc, as Qt understands QVariant and QString automatically.

EDIT: This should do it:


QDataStream &operator<<(QDataStream &out, const s_SqlListInfo &myObj)
{
out << myObj.queryName << myObj.dataBaseName << myObj.sqlStatement << myObj.reqTableList;
return out;
}

QDataStream &operator>>(QDataStream &in, s_SqlListInfo &myObj)
{
in >> myObj.queryName >> myObj.dataBaseName >> myObj.sqlStatement >> myObj.reqTableList;
return in;
}

//Before use...
qRegisterMetaTypeStreamOperators<s_SqlListInfo>("s_SqlListInfo");

baray98
20th August 2009, 17:13
Thanks for the reply ...

But I am not sure where to add the above code in the project .. pardon my being newbie

should i put it before my declaration of my struct ?

baray98

baray98
20th August 2009, 17:41
QDataStream &operator<<(QDataStream &out, const DMUtils::s_SqlListInfo &myObj)
{
out << myObj.queryName << myObj.dataBaseName << myObj.sqlStatement << myObj.reqTableList;
return out;
}

QDataStream &operator>>(QDataStream &in, DMUtils::s_SqlListInfo &myObj)
{
in >> myObj.queryName >> myObj.dataBaseName >> myObj.sqlStatement >> myObj.reqTableList;
return in;
}

int main (int argc,char *argv[])
{
QApplication app(argc,argv);

qRegisterMetaTypeStreamOperators<DMUtils::s_SqlListInfo>("s_SqlListInfo");
qRegisterMetaType<DMUtils::s_SqlListInfo>("s_SqlListInfo");


DMMainWindow *mainWin = new DMMainWindow;

mainWin->show();

return app.exec();
}


I did it like this and it will still crash!!!!!!!!!!!!!!!!!

baray98
20th August 2009, 17:56
i got it thanks buddy

the problem was this



qRegisterMetaTypeStreamOperators<DMUtils::s_SqlListInfo>("s_SqlListInfo");
qRegisterMetaType<DMUtils::s_SqlListInfo>("s_SqlListInfo");

//change to

qRegisterMetaTypeStreamOperators<DMUtils::s_SqlListInfo>("DMUtils::s_SqlListInfo");
qRegisterMetaType<DMUtils::s_SqlListInfo>("DMUtils::s_SqlListInfo");