拖拽是編程中常常要用到的,我這裏主要是實習了Treewidget之間直接拖拽Item,按下Ctrl鍵的話是copy,不按Ctrl則是Move。如下是實現代碼編程
- class TreeItemMimeData:public QMimeData
- {
- Q_OBJECT
- public:
- TreeItemMimeData():QMimeData()
- {
- m_pDragItem = NULL;
- }
-
- ~TreeItemMimeData()
- {
-
- }
-
- void SetDragData(QString mimeType , QTreeWidgetItem *pItem)
- {
- m_format<<mimeType;
- m_pDragItem = pItem;
- }
-
-
-
- QStringList formats() const
- {
- return m_format;
- }
-
- const QTreeWidgetItem* DragItemData() const
- {
- return m_pDragItem;
- }
-
-
- protected:
-
- QVariant retrieveData(const QString &mimetype, QVariant::Type preferredType) const
- {
- if (mimetype == "ItemMimeData")
- {
- return m_pDragItem;
- }
- else
- {
- return QMimeData::retrieveData(mimetype, preferredType);
- }
- }
- private:
- const QTreeWidgetItem *m_pDragItem;
- QStringList m_format;
- };
-
- class MyTreeWidget:public QTreeWidget
- {
- Q_OBJECT
- public:
- MyTreeWidget(QWidget *parent = NULL);
- ~MyTreeWidget();
-
- protected:
- void mouseMoveEvent(QMouseEvent *event);
- void mousePressEvent(QMouseEvent *event);
- void mouseReleaseEvent(QMouseEvent *event);
-
- void dragEnterEvent(QDragEnterEvent *event);
- void dragMoveEvent(QDragMoveEvent *event);
- void dropEvent(QDropEvent *event);
- void keyPressEvent(QKeyEvent *event);
- void keyReleaseEvent(QKeyEvent *event);
- private:
- QPoint m_startDragPoint;
- void performDrag();
- bool m_CtrlPressed;
-
- };
如下是我演示的截圖this
http://blog.csdn.net/hai200501019/article/details/9322329spa