zoukankan      html  css  js  c++  java
  • Qt实现不同Treewidget之间拖拽

    拖拽是编程中经常要用到的,我这里主要是实习了Treewidget之间直接拖拽Item,按下Ctrl键的话是copy,不按Ctrl则是Move。以下是实现代码

    [cpp] view plain copy
     
    1. class TreeItemMimeData:public QMimeData  
    2. {  
    3.     Q_OBJECT  
    4. public:  
    5.     TreeItemMimeData():QMimeData()  
    6.     {  
    7.         m_pDragItem = NULL;  
    8.     }  
    9.   
    10.     ~TreeItemMimeData()  
    11.     {  
    12.   
    13.     }  
    14.   
    15.     void SetDragData(QString mimeType , QTreeWidgetItem *pItem)  
    16.     {  
    17.         m_format<<mimeType;  
    18.         m_pDragItem = pItem;  
    19.     }  
    20.       
    21.   
    22.   
    23.     QStringList formats() const  
    24.     {  
    25.         return m_format;  
    26.     }  
    27.   
    28.     const QTreeWidgetItem* DragItemData() const  
    29.     {  
    30.         return m_pDragItem;  
    31.     }  
    32.   
    33.   
    34. protected:  
    35.   
    36.     QVariant retrieveData(const QString &mimetype, QVariant::Type preferredType) const  
    37.     {  
    38.         if (mimetype == "ItemMimeData")  
    39.         {  
    40.             return m_pDragItem;  
    41.         }  
    42.         else  
    43.         {  
    44.              return QMimeData::retrieveData(mimetype, preferredType);    
    45.         }  
    46.     }  
    47. private:  
    48.     const QTreeWidgetItem   *m_pDragItem;  
    49.     QStringList              m_format;  
    50. };  
    51.   
    52. class MyTreeWidget:public QTreeWidget  
    53. {  
    54.     Q_OBJECT  
    55. public:  
    56.     MyTreeWidget(QWidget *parent = NULL);  
    57.     ~MyTreeWidget();  
    58.   
    59. protected:  
    60.     void mouseMoveEvent(QMouseEvent *event);  
    61.     void mousePressEvent(QMouseEvent *event);  
    62.     void mouseReleaseEvent(QMouseEvent *event);  
    63.   
    64.     void dragEnterEvent(QDragEnterEvent *event);  
    65.     void dragMoveEvent(QDragMoveEvent *event);  
    66.     void dropEvent(QDropEvent *event);  
    67.     void keyPressEvent(QKeyEvent *event);  
    68.     void keyReleaseEvent(QKeyEvent *event);  
    69. private:  
    70.     QPoint     m_startDragPoint;  
    71.     void       performDrag();  
    72.     bool       m_CtrlPressed;  
    73.   
    74. };  
    [cpp] view plain copy
     
    1. MyTreeWidget::MyTreeWidget(QWidget *parent /*= NULL*/):QTreeWidget(parent)  
    2. {  
    3.     m_CtrlPressed = false;  
    4.     this->setSelectionMode(QAbstractItemView::ExtendedSelection);  
    5.     this->setAcceptDrops(true);  
    6.     this->setDragEnabled(true);  
    7.     this->setStyleSheet("QTreeWidget::item:selected{ background-color: rgb(150, 0, 0)}");  
    8. }  
    9.   
    10. MyTreeWidget::~MyTreeWidget()  
    11. {  
    12. }  
    13.   
    14. void MyTreeWidget::keyPressEvent(QKeyEvent *event)  
    15. {  
    16.     if (event->key() == Qt::Key_Control)  
    17.     {  
    18.         m_CtrlPressed = true;  
    19.     }  
    20. }  
    21.   
    22. void MyTreeWidget::keyReleaseEvent(QKeyEvent *event)  
    23. {  
    24.     if (event->key() == Qt::Key_Control)  
    25.     {  
    26.         m_CtrlPressed = false;  
    27.     }  
    28. }  
    29.   
    30. void MyTreeWidget::mousePressEvent(QMouseEvent *event)  
    31. {  
    32.     if (event->button() == Qt::LeftButton)  
    33.     {  
    34.         m_startDragPoint = event->pos();  
    35.     }  
    36.     QTreeWidget::mousePressEvent(event);  
    37. }  
    38.   
    39. void MyTreeWidget::mouseMoveEvent(QMouseEvent *event)  
    40. {  
    41.     if (event->buttons() & Qt::LeftButton)  
    42.     {  
    43.         int dragDistance = (event->pos() - m_startDragPoint).manhattanLength();  
    44.         if (dragDistance > QApplication::startDragDistance())  
    45.         {  
    46.             performDrag();  
    47.         }  
    48.     }  
    49.     QTreeWidget::mouseMoveEvent(event);  
    50.   
    51. }  
    52.   
    53. void MyTreeWidget::mouseReleaseEvent(QMouseEvent *event)  
    54. {  
    55.     QTreeWidget::mouseReleaseEvent(event);  
    56. }  
    57.   
    58. void MyTreeWidget::dragEnterEvent(QDragEnterEvent *event)  
    59. {  
    60.     QWidget *source =  qobject_cast<MyTreeWidget *>(event->source());    
    61.     if (source /*&& source != this*/)   
    62.     {    
    63.         if (m_CtrlPressed)  
    64.         {  
    65.             event->setDropAction(Qt::CopyAction);    
    66.         }   
    67.         else  
    68.         {  
    69.             event->setDropAction(Qt::MoveAction);    
    70.         }  
    71.         /*event->setDropAction(Qt::MoveAction);  */  
    72.         event->accept();    
    73.     }    
    74. }  
    75.   
    76. void MyTreeWidget::dragMoveEvent(QDragMoveEvent *event)  
    77. {  
    78.   
    79.     QWidget *source =  qobject_cast<MyTreeWidget *>(event->source());    
    80.     if (source /*&& source != this*/)  
    81.     {    
    82.         const TreeItemMimeData *pMimeData = (const TreeItemMimeData *)(event->mimeData());   
    83.         const QTreeWidgetItem *item = pMimeData->DragItemData();  
    84.         QTreeWidgetItem *currentItem = this->itemAt(event->pos());  
    85.         if (currentItem == item)           //不允许拖回到原来的item  
    86.         {  
    87.             event->ignore();  
    88.         }   
    89.         else  
    90.         {  
    91.             setCurrentItem(currentItem);  
    92.             if (m_CtrlPressed)  
    93.             {  
    94.                 event->setDropAction(Qt::CopyAction);    
    95.             }   
    96.             else  
    97.             {  
    98.                 event->setDropAction(Qt::MoveAction);    
    99.             }  
    100.             //event->setDropAction(Qt::MoveAction);    
    101.             event->accept();    
    102.         }  
    103.           
    104.     }    
    105. }  
    106.   
    107. void MyTreeWidget::dropEvent(QDropEvent *event)  
    108. {  
    109.     QWidget *source =  qobject_cast<MyTreeWidget *>(event->source());   
    110.     const TreeItemMimeData *pMimeData = (const TreeItemMimeData *)(event->mimeData());   
    111.     if (source /*&& source != this*/)   
    112.     {    
    113.         const QTreeWidgetItem *item = pMimeData->DragItemData();  
    114.         QTreeWidgetItem *pItem = item->clone();  
    115.         QTreeWidgetItem *currentItem = this->itemAt(event->pos());  
    116.           
    117.         if (currentItem && (currentItem != item))  
    118.         {  
    119.             currentItem->addChild(pItem);  
    120.         }   
    121.         else  
    122.         {  
    123.             this->addTopLevelItem(pItem);  
    124.         }  
    125.         if (m_CtrlPressed)  
    126.         {  
    127.             event->setDropAction(Qt::CopyAction);    
    128.         }   
    129.         else  
    130.         {  
    131.             event->setDropAction(Qt::MoveAction);    
    132.         }  
    133.         //event->setDropAction(Qt::MoveAction);    
    134.         event->accept();    
    135.     }   
    136. }  
    137.   
    138. void MyTreeWidget::performDrag()  
    139. {  
    140.     QTreeWidgetItem *item = currentItem();    
    141.     if (item)   
    142.     {    
    143.   
    144.         TreeItemMimeData *mimeData = new TreeItemMimeData;  
    145.         mimeData->SetDragData("ItemMimeData",item);  
    146.   
    147.         QDrag *drag = new QDrag(this);    
    148.         drag->setMimeData(mimeData);    
    149.         drag->setPixmap(QPixmap(":/DragDropDemo/Resources/Mail.png"));    
    150.         if (m_CtrlPressed)  
    151.         {  
    152.             drag->exec(Qt::CopyAction);  
    153.         }   
    154.         else  
    155.         {  
    156.             drag->exec(Qt::MoveAction);  
    157.             delete item;  
    158.         }  
    159.           
    160.     }   
    161. }  

    以下是我演示的截图

    http://blog.csdn.net/hai200501019/article/details/9322329

  • 相关阅读:
    渚漪Day18——JavaWeb 09【JSP】
    渚漪Day17——JavaWeb 08【Session】
    渚漪Day16——JavaWeb 07【Cookie】
    渚漪Day15——JavaWeb 06【HTTPServletRequest】
    渚漪Day14——JavaWeb 05【HTTPServletResponse】
    Typora编写markdown 常用入门
    Vue 笔记
    ABCNN 学习笔记
    DSSM 学习笔记
    支持向量机 SVM 学习笔记
  • 原文地址:https://www.cnblogs.com/findumars/p/5176057.html
Copyright © 2011-2022 走看看