PDA

View Full Version : QListView drag error with qmetatype



baray98
26th June 2009, 23:43
I made a QListView which has a source of QStandardItemModel. I set the view to have the following settings



setMovement(QListView::Snap);
setResizeMode(QListView::Adjust);
setSpacing(5);
setViewMode(QListView::IconMode);


My model's item is a QStandardItem and i added my "metatype registered value" at Qt::UserRole + 1. When I started draging the item in my view the whole things just crushes, but when i took my metatype registered value in my item it works fine. This will only happen when i drag those icons. I have accessed and displayed my custom data through the modelIndex and its fine no crushes.

below is how i declare my metatype value




struct s_SqlListInfo
{
QVariant queryName;
QVariant dataBaseName;
QList <QString> reqTableList;
QString sqlStatement;
};

// Qt MetaType Declaration
Q_DECLARE_METATYPE(s_SqlListInfo)

//spot where i added my value to the item

QVariant myVal = hander->list().at(0); // hander->list() ,returns a QList of Qvariant with my custom data
if (myVal .canConvert<s_SqlListInfo>())
{
s_SqlListInfo info = myVal .value<s_SqlListInfo>();

QStandardItem* item = new QStandardItem();

item->setData(info.queryName.toString(),Qt::DisplayRole) ;
item->setData(QIcon(":/ListIcon.png"),Qt::DecorationRole);
item->setData(myVal ,Qt::UserRole+1); // spot where i added my custom data
item->setData(info.sqlStatement,Qt::ToolTipRole);
item->setFlags(item->flags()& ~Qt::ItemIsEditable);

m_userListModel->appendRow(item);
}

wysota
27th June 2009, 21:11
Can you provide a minimal compilable example reproducing the problem?

baray98
28th June 2009, 05:12
here we go



#include <QtGui>

struct myStruct
{
QVariant queryName;
QVariant dataBaseName;
QList <QString> reqTableList;
QString sqlStatement;
};
Q_DECLARE_METATYPE(myStruct)

int main (int argc,char *argv[])
{
QApplication app(argc,argv);
QListView *view = new QListView;

view->setViewMode(QListView::IconMode);

QStandardItemModel* model = new QStandardItemModel();

for (int i=0; i < 5; ++i)
{
QStandardItem* item = new QStandardItem();
item->setData(QString("Item %1").arg(i),Qt::DisplayRole);
// custom data
myStruct st;
QVariant v;
v.setValue(st);
item->setData(v);

model->appendRow(item);
}

view->setModel(model);
view->show();

return app.exec();
}

wysota
28th June 2009, 10:25
It works fine for me on Qt 4.5.2.

baray98
28th June 2009, 18:17
I am still @ 4.5.1 I did not realize they have 4.5.2 already....

wysota
28th June 2009, 19:10
I don't think this is a matter of different releases.

baray98
29th June 2009, 02:41
When you said it work.. did you try dragging an item somewher else?

baray98
29th June 2009, 04:41
I think it's the Q_ASSERT_X thats doing it(crashing) . When it crash it gave an ASSERT Error on line 1951 in qvariant.cpp . I tried recompiling it on release mode and it work.

Why would qvariant assert something legal , maybe a warning would be fine not QUIT.. or maybe i am using qvariant the wrong way..

wysota
29th June 2009, 09:12
When you said it work.. did you try dragging an item somewher else?

Somewhere else meaning where?