QListWidget、QTreeWidget、QTableWidget的拖放

 QListWidget、QTreeWidget、QTableWidget的拖放实验

QAbstractItemView::DragDropMode 的枚举值

QAbstractItemView::NoDragDrop0组件不支持拖放操作
QAbstractItemView::DragOnly1组件只支持拖动操作
QAbstractItemView::DropOnly 2组件只支持放置操作
QAbstractItemView::DragDrop3组件支持拖放操作
QAbstractItemView::InternalMove4组件只支持内部项的移动操作,例如目录树内节点的移动操作

 Qt::DropAction 的枚举值

Qt::CopyAction 1将数据复制到放置点组件处
Qt::MoveAction2将数据从拖动点组件处移动到放置点组件处
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:
    //将枚举值 转换为index
    int getDropActionIndex(Qt::DropAction actionType);
    //将index转换为枚举值
    Qt::DropAction getDropActionType(int index);
    //当前设置属性的item widget
    QAbstractItemView * 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强制转换为 dragDropMode
    m_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);

}

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

castlooo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值