QListWidget、QTreeWidget、QTableWidget的拖放实验
QAbstractItemView::DragDropMode 的枚举值
QAbstractItemView::NoDragDrop | 0 | 组件不支持拖放操作 |
QAbstractItemView::DragOnly | 1 | 组件只支持拖动操作 |
QAbstractItemView::DropOnly | 2 | 组件只支持放置操作 |
QAbstractItemView::DragDrop | 3 | 组件支持拖放操作 |
QAbstractItemView::InternalMove | 4 | 组件只支持内部项的移动操作,例如目录树内节点的移动操作 |
Qt::DropAction 的枚举值
Qt::CopyAction | 1 | 将数据复制到放置点组件处 |
Qt::MoveAction | 2 | 将数据从拖动点组件处移动到放置点组件处 |
Qt::LinkAction | 4 | 在拖动点组件和放置点组件之间建立数据连接 |
Qt::IgnoreAction | 0 | 对数据不进行任何操作 |
效果
DragDropItemExample.h
#ifndef DRAGDROPITEMEXAMPLE_H
#define DRAGDROPITEMEXAMPLE_H#include <QWidget>
#include <QAbstractItemView>
#include <QGroupBox>
#include <QEvent>namespace Ui {
class DragDropItemExample;
}class DragDropItemExample : public QWidget
{Q_OBJECT
private://将枚举值 转换为indexint getDropActionIndex(Qt::DropAction actionType);//将index转换为枚举值Qt::DropAction getDropActionType(int index);//当前设置属性的item widgetQAbstractItemView * m_ItemView = nullptr;//m_ItemView的属性显示到界面上void refreshToUI(QGroupBox *curGroupBox);
protected://重写事件过滤器bool eventFilter(QObject *watched, QEvent *event);public:explicit DragDropItemExample(QWidget *parent = nullptr);~DragDropItemExample();private slots:void on_radioSource_clicked();void on_radioList_clicked();void on_radioTree_clicked();void on_radioTable_clicked();void on_chkAccetDrops_clicked(bool checked);void on_chkDragEnabled_clicked(bool checked);void on_comboMode_currentIndexChanged(int index);void on_comboDefaultAction_currentIndexChanged(int index);private:Ui::DragDropItemExample *ui;
};#endif // DRAGDROPITEMEXAMPLE_H
DragDropItemExample.cpp
#include "dragdropitemexample.h"
#include "ui_dragdropitemexample.h"#include <QMessageBox>
#include <QKeyEvent>int DragDropItemExample::getDropActionIndex(Qt::DropAction actionType)
{switch(actionType){case Qt::CopyAction:return 0;case Qt::MoveAction:return 1;case Qt::LinkAction:return 2;case Qt::IgnoreAction:return 3;default:return 0;}
}Qt::DropAction DragDropItemExample::getDropActionType(int index)
{switch(index){case 0:return Qt::CopyAction;case 1:return Qt::MoveAction;case 2:return Qt::LinkAction;case 3:return Qt::IgnoreAction;default:return Qt::CopyAction;}
}void DragDropItemExample::refreshToUI(QGroupBox *curGroupBox)
{ui->chkAccetDrops->setChecked(m_ItemView->acceptDrops());ui->chkDragEnabled->setChecked(m_ItemView->dragEnabled());ui->comboMode->setCurrentIndex((int)m_ItemView->dragDropMode());int index = getDropActionIndex(m_ItemView->defaultDropAction());ui->comboDefaultAction->setCurrentIndex(index);QFont font = ui->groupBox->font();font.setBold(false);ui->groupBox_source->setFont(font);ui->groupBox_list->setFont(font);ui->groupBox_tree->setFont(font);ui->groupBox_table->setFont(font);font.setBold(true);curGroupBox->setFont(font);
}bool DragDropItemExample::eventFilter(QObject *watched, QEvent *event)
{if(event->type()!= QEvent::KeyPress)//如果不是按键事件,退出+return QWidget::eventFilter(watched,event);QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);if(keyEvent->key()!= Qt::Key_Delete){//如果按下的不是delete键 ,退出return QWidget::eventFilter(watched,event);}if(watched == ui->listSource){QListWidgetItem *item = ui->listSource->takeItem(ui->listSource->currentRow());delete item;//最后释放item}else if(watched == ui->listWidget){QListWidgetItem *item = ui->listWidget->takeItem(ui->listWidget->currentRow());delete item;//最后释放item}else if(watched== ui->treeWidget){QTreeWidgetItem *item = ui->treeWidget->currentItem();if(item->parent()!=nullptr){QTreeWidgetItem *parent =item->parent();parent->removeChild(item);}else{//顶级节点的删除takeTopLevelItem(index)int index = ui->treeWidget->indexOfTopLevelItem(item);ui->treeWidget->takeTopLevelItem(index);}delete item;//最后释放item}else if(watched == ui->tableWidget){//删除对应的单元格QTableWidgetItem *item =ui->tableWidget->takeItem(ui->tableWidget->currentRow(),ui->tableWidget->currentColumn());delete item;}return true;// eventFilter 返回true表示事件已经处理
}DragDropItemExample::DragDropItemExample(QWidget *parent): QWidget(parent), ui(new Ui::DragDropItemExample)
{ui->setupUi(this);//evenFilter 需要给控件安装事件过滤器ui->listSource->installEventFilter(this);ui->listWidget->installEventFilter(this);ui->treeWidget->installEventFilter(this);ui->tableWidget->installEventFilter(this);//设置四个组件的 拖放操作属性ui->listSource->setAcceptDrops(true);ui->listSource->setDragDropMode(QAbstractItemView::DragDrop);ui->listSource->setDragEnabled(true);ui->listSource->setDefaultDropAction(Qt::CopyAction);ui->listWidget->setAcceptDrops(true);ui->listWidget->setDragDropMode(QAbstractItemView::DragDrop);ui->listWidget->setDragEnabled(true);ui->listWidget->setDefaultDropAction(Qt::CopyAction);ui->treeWidget->setAcceptDrops(true);ui->treeWidget->setDragDropMode(QAbstractItemView::DragDrop);ui->treeWidget->setDragEnabled(true);ui->treeWidget->setDefaultDropAction(Qt::CopyAction);ui->tableWidget->setAcceptDrops(true);ui->tableWidget->setDragDropMode(QAbstractItemView::DragDrop);ui->tableWidget->setDragEnabled(true);ui->tableWidget->setDefaultDropAction(Qt::CopyAction);}DragDropItemExample::~DragDropItemExample()
{delete ui;
}void DragDropItemExample::on_radioSource_clicked()
{m_ItemView= ui->listSource;refreshToUI(ui->groupBox_source);
}void DragDropItemExample::on_radioList_clicked()
{m_ItemView= ui->listWidget;refreshToUI(ui->groupBox_list);
}void DragDropItemExample::on_radioTree_clicked()
{m_ItemView= ui->treeWidget;refreshToUI(ui->groupBox_tree);
}void DragDropItemExample::on_radioTable_clicked()
{m_ItemView= ui->tableWidget;refreshToUI(ui->groupBox_table);
}void DragDropItemExample::on_chkAccetDrops_clicked(bool checked)
{m_ItemView->setAcceptDrops(checked);
}void DragDropItemExample::on_chkDragEnabled_clicked(bool checked)
{m_ItemView->setDragEnabled(checked);
}void DragDropItemExample::on_comboMode_currentIndexChanged(int index)
{QAbstractItemView::DragDropMode mode=(QAbstractItemView::DragDropMode)index;//将index强制转换为 dragDropModem_ItemView->setDragDropMode(mode);qDebug()<<QString("DragDropMode:%1").arg(index);
}void DragDropItemExample::on_comboDefaultAction_currentIndexChanged(int index)
{Qt::DropAction actionType=getDropActionType(index);m_ItemView->setDefaultDropAction(actionType);}