[Interest] Tree widget: rearrange elements

Tibold Kandrai dom at dotzteam.com
Mon Aug 27 10:59:12 CEST 2012


Hi,

As I understood the task is this:
You want Qt to handle the drag and drop, but you want to control whether
you can or cannot drop the item.

For this you need to override one of the drag events not the drop event.
I think you need to use this:
http://doc.qt.nokia.com/4.7-snapshot/qtreeview.html#dragMoveEvent and
you have to accept or ignore the events here.

Cheers,
Tibold

On 2012-08-27 10:47, Sensei wrote:
> 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!
>
>
>
>
>
>
>
>
>
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest


-- 
Tibold Kandrai
CEO @ Dotz Team




More information about the Interest mailing list