Nokia released Qt 4.7 recently. The new version of Qt includes the brand new QML technology: an advanced descriptive programming language and performance improvement. Someone has raised this question recently: Set the dragDropMode of QTreeWidget to InternalMove, and drag the item to set the dropped item to current. I personally think this is a relatively basic requirement, but after studying it, I found that there is no directly available method or signal in Qt to obtain this information. For further research, the implementation can be implemented, which is a little complicated. It is listed here, hoping to help other Qt developers.
When you drag an item, you first need to select the item, so we can get the dragged item by responding to the currentItemChanged signal. Of course, things are not that simple. Because the signal of currentItemChanged is not only triggered when you drag the mouse, but also triggered when you click the mouse, in addition, the content of a single slave parameter or a simple API cannot distinguish the two cases.
To determine whether this signal occurs during drag and drop, you can rewrite the dropEvent virtual function. When dragging occurs, the currentItemChanged signal is sent during the dropEvent processing. Therefore, we can set a flag when entering the dropEvent, and reset the flag when the function ends. Then, in the currentItemChanged slot, determine whether the drag action is triggered Based on the flag value, and the prev in the parameter is the required item to be dragged.
The relevant Code lists the important parts as follows:
- Tw = new TreeWidget (this, 10, 2); // customized treewidget class
- Tw-> setDragDropMode (q1_actitemview: InternalMove); // drag drop
- Connect (tw, SIGNAL (currentItemChanged (QTreeWidgetItem *, QTreeWidgetItem *), this, SLOT (doCurrentItemChanged (QTreeWidgetItem *, QTreeWidgetItem *)));
-
-
- Void TreeWidget: dropEvent (QDropEvent * event)
- {
- QWarning () <"begin drop event..."; // set the flag here
- QTreeWidget: dropEvent (event );
- QWarning () <"end drop event..."; // reset the flag here
- }
-
-
- Void MainWin: doCurrentItemChanged (QTreeWidgetItem * cur, QTreeWidgetItem * prev)
- {
- If (prev) // judge the flag here, And prev is the item to be dragged.
- QWarning () <"previous:" <prev-> text (0 );
- }