Note that there are some explanatory texts on larger screens.

plurals
  1. POIntermixed parent and child nodes using QTreeWidget (Qt4) and drag'n'drop
    primarykey
    data
    text
    <p>I am using a QTreeWidget to display a tree of parent nodes with their leave nodes. Each parent can have various leaf nodes but leaf nodes should have no childs. The user should be able to move leaves between parents by dragging them to the new position. To avoid leaves from being dropped on other leaves, I have only set <code>ItemIsDragEnabled</code> on leaves while having <code>ItemIsDropEnabled</code> on parent nodes. This works fine if the QTreeWidget is set to "SingleSelection". However, if the SelectionMode is set to <code>ExtendedSelection</code> you are able to select a leave and a parent node together and drop them both on a leaf: <a href="http://i.stack.imgur.com/Kil3y.jpg" rel="nofollow">http://i.stack.imgur.com/Kil3y.jpg</a> (Screenshot)</p> <p>Here is the sample code:</p> <pre><code>QTreeWidget *tree = this-&gt;ui-&gt;treeWidget; QTreeWidgetItem *item; QTreeWidgetItem *child; tree-&gt;setSelectionMode(QAbstractItemView::ExtendedSelection); tree-&gt;setDefaultDropAction(Qt::MoveAction); tree-&gt;setDragEnabled(true); tree-&gt;setAcceptDrops(true); tree-&gt;setDropIndicatorShown(true); // disable dropping of leaves as top level items tree-&gt;invisibleRootItem()-&gt;setFlags( Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled ); for (int i = 0; i &lt; 2; i++) { // create top level item item = new QTreeWidgetItem(); item-&gt;setText(0, "parent"); item-&gt;setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsDropEnabled | Qt::ItemIsEnabled ); // add 3 child items for (int j = 0; j &lt; 3; j++) { child = new QTreeWidgetItem(); child-&gt;setText(0, "child"); child-&gt;setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsDragEnabled | Qt::ItemIsEnabled ); item-&gt;addChild(child); } // add item to tree tree-&gt;addTopLevelItem(item); } </code></pre> <p>I googled quite a lot but could not come up with a solution. How can I keep child and parent nodes on their respective levels while using <code>ExtendedSelection</code>?</p> <p>Do I have to subclass QTreeWidget and override insertRows()? Is there any way to intercept drag'n'drop actions on QTreeWidget so I could check if the action is ok? (If there is a way to get this to work with QStandardItemModel/QTreeView I would be happy too)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload