[Interest] Tree widget: rearrange elements

Sensei senseiwa at gmail.com
Mon Aug 27 10:47:34 CEST 2012


Dear all,

I am now messing around my own QTreeWidget. I'm trying to let 
rearrangement of items possible, allowing or disallowing drop events. 
Obviously it doesn't work, otherwise I wouldn't seek your help! :)

Test case: in a tree there are "folders" and "files", and I can move 
files on folders, since they can receive files. However, I shouldn't 
drop file or folders onto files: they are leafs.

I don't know if even writing my own dropEvent is the right answer, but 
that's what I did.

In my constructor, I start a new tree by simply adding items (tag 1 is 
for folders, 2 for files):



txProjectView::txProjectView(QWidget *parent) : QTreeWidget(parent)
{
     setColumnCount(1);
     setHeaderLabel(QString("Label!"));

     root = new QTreeWidgetItem(QStringList(QString("Root")), 1);

     a = new QTreeWidgetItem(root, QStringList(QString("first!")), 1);
     b = new QTreeWidgetItem(root, QStringList(QString("second")), 1);

     i = new QTreeWidgetItem(a, QStringList(QString("i.txt")), 2);
     j = new QTreeWidgetItem(b, QStringList(QString("j.txt")), 2);
     k = new QTreeWidgetItem(b, QStringList(QString("k.txt")), 2);
     addTopLevelItem(root);

     setAcceptDrops(true);
     setDragEnabled(true);
     setDragDropMode(QAbstractItemView::InternalMove);
}



Next, I repleace the dropEvent method. In this simple case I want to 
discard them, since even this won't work:



void txProjectView::dropEvent(QDropEvent *event)
{
     qWarning("mime: %s", qPrintable(event->mimeData()->html()));
     if (1)
     {
         // if I am a file, don't drop on me
         qWarning("no drop!");
         event->ignore();
     }
     else
     {
         // if I am a folder, accept drops
         qWarning("drop!");
         event->accept();
     }
}



What happens? A complete mess. Items dropped simply disappear, and I get 
this on the console:

QAbstractItemModel::endRemoveRows:  Invalid index ( -1 , 0 ) in model 
QTreeModel(0x104e95d60)



Can someone help me? I think this is a simple task, but I have no idea 
how to do what I'm looking for. Even a simple project that accepts 
drag&drop would be useful...


Thanks & Cheers!












More information about the Interest mailing list