QTreeWidget design solves the problem of no drag items

Source: Internet
Author: User

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:

 
  1. Tw = new TreeWidget (this, 10, 2); // customized treewidget class
  2. Tw-> setDragDropMode (q1_actitemview: InternalMove); // drag drop
  3. Connect (tw, SIGNAL (currentItemChanged (QTreeWidgetItem *, QTreeWidgetItem *), this, SLOT (doCurrentItemChanged (QTreeWidgetItem *, QTreeWidgetItem *)));
  4.  
  5.  
  6. Void TreeWidget: dropEvent (QDropEvent * event)
  7. {
  8. QWarning () <"begin drop event..."; // set the flag here
  9. QTreeWidget: dropEvent (event );
  10. QWarning () <"end drop event..."; // reset the flag here
  11. }
  12.  
  13.  
  14. Void MainWin: doCurrentItemChanged (QTreeWidgetItem * cur, QTreeWidgetItem * prev)
  15. {
  16. If (prev) // judge the flag here, And prev is the item to be dragged.
  17. QWarning () <"previous:" <prev-> text (0 );
  18. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.