Qt拖拽组件与键盘事件

news2024/9/23 3:25:00

1.相关说明

1.设置widget或view的拖拽和放置模式函数setDragDropMode参数说明,NoDragDrop(无拖拽和放置)、DragOnly(只允许拖拽)、DropOnly(只允许放置)、DragDrop(允许拖拽和放置)、InternalMove(只移动不复制)

2.设置widget或view的放置动作函数setDefaultDropAction参数说明,Qt::CopyAction(复制)、Qt::MoveAction(移动)、Qt::LinkAction(创建链接)、Qt::IgnoreAction(忽略,什么都不做)

2.相关界面

3.相关代码

#include "widget.h"
#include "ui_widget.h"
#include <QKeyEvent>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->listSource->setAcceptDrops(true);
    ui->listSource->setDragEnabled(true);
    ui->listSource->setDefaultDropAction(Qt::CopyAction);
    ui->listSource->setDragDropMode(QAbstractItemView::DragDrop);

    ui->listWidget->setAcceptDrops(true);
    ui->listWidget->setDragEnabled(true);
    ui->listWidget->setDefaultDropAction(Qt::CopyAction);
    ui->listWidget->setDragDropMode(QAbstractItemView::DragDrop);

    ui->treeWidget->setAcceptDrops(true);
    ui->treeWidget->setDragEnabled(true);
    ui->treeWidget->setDefaultDropAction(Qt::CopyAction);
    ui->treeWidget->setDragDropMode(QAbstractItemView::DragDrop);

    ui->tableWidget->setAcceptDrops(true);
    ui->tableWidget->setDragEnabled(true);
    ui->tableWidget->setDefaultDropAction(Qt::MoveAction);
    ui->tableWidget->setDragDropMode(QAbstractItemView::DragDrop);

    m_itemView = ui->listSource;
    refreshToUI(ui->groupBox_listSource);

    ui->listSource->installEventFilter(this);
    ui->listWidget->installEventFilter(this);
    ui->treeWidget->installEventFilter(this);
    ui->tableWidget->installEventFilter(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::refreshToUI(QGroupBox *curGroupBox)
{
    ui->chkAcceptDrops->setChecked(m_itemView->acceptDrops());
    ui->chkDragEnabled->setChecked(m_itemView->dragEnabled());
    ui->comboDragDropMode->setCurrentIndex((int)m_itemView->dragDropMode());
    int index = getDropActionIndex(m_itemView->defaultDropAction());
    ui->comboDefaultDropAction->setCurrentIndex(index);

    QFont font = ui->groupBox_listSource->font();
    font.setBold(false);
    ui->groupBox_listSource->setFont(font);
    ui->groupBox_listWidget->setFont(font);
    ui->groupBox_treeWidget->setFont(font);
    ui->groupBox_tableWidget->setFont(font);

    font.setBold(true);
    curGroupBox->setFont(font);
}

int Widget::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 Widget::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;
    }
}

// 设置listWidget对象
void Widget::on_radioListWidget_clicked()
{
    m_itemView = ui->listWidget;
    this->refreshToUI(ui->groupBox_listWidget);
}
// 设置listSource对象
void Widget::on_radioListSource_clicked()
{
    m_itemView = ui->listSource;
    this->refreshToUI(ui->groupBox_listSource);
}
// 设置treeWidget对象
void Widget::on_radioTreeWidget_clicked()
{
    m_itemView = ui->treeWidget;
    this->refreshToUI(ui->groupBox_treeWidget);
}
// 设置tableWidget对象
void Widget::on_radioTableWidget_clicked()
{
    m_itemView = ui->tableWidget;
    this->refreshToUI(ui->groupBox_tableWidget);
}
// 设置 acceptDrops
void Widget::on_chkAcceptDrops_clicked(bool checked)
{
    m_itemView->setAcceptDrops(checked);
}
// 设置 dragEnabled
void Widget::on_chkDragEnabled_clicked(bool checked)
{
    m_itemView->setDragEnabled(checked);
}
// dragDropMode选择
void Widget::on_comboDragDropMode_currentIndexChanged(int index)
{
    m_itemView->setDragDropMode((QAbstractItemView::DragDropMode)index);
}
// defaultDropAction
void Widget::on_comboDefaultDropAction_currentIndexChanged(int index)
{
    m_itemView->setDefaultDropAction(getDropActionType(index));
}

// 事件捕获与过滤
bool Widget::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){
        return QWidget::eventFilter(watched, event);
    }
    if(watched == ui->listSource){
        delete ui->listSource->takeItem(ui->listSource->currentRow());
    }else if(watched == ui->listWidget){
        delete ui->listWidget->takeItem(ui->listWidget->currentRow());
    }else if(watched == ui->treeWidget){
        QTreeWidgetItem *curItem = ui->treeWidget->currentItem();
        if(curItem->parent() != NULL){
            curItem->parent()->removeChild(curItem);
        } else {
            int index = ui->treeWidget->indexOfTopLevelItem(curItem);
            ui->treeWidget->takeTopLevelItem(index);
        }
        delete curItem;
    } else if(watched == ui->tableWidget){
        delete ui->tableWidget->takeItem(ui->tableWidget->currentRow(), ui->tableWidget->currentColumn());
    }
    return true;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1398066.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

小周带你读论文-2之“草履虫都能看懂的Transformer老活儿新整“Attention is all you need(2)

书接前文&#xff1a;小周带你读论文-2之"草履虫都能看懂的Transformer老活儿新整"Attention is all you need(1) (qq.com) 上文书说到为什么我们要用casual-decoder架构&#xff0c;把Transformer的左边给省略了&#xff0c;于是得到下图这样的架构 上图是GPT-1的模…

leetcode2312卖木头块

其实不难&#xff0c;主要是你得一眼看出来这个问题缩小规模然后就可以用DP来做了 using ll long long; class Solution { public:long long sellingWood(int m, int n, vector<vector<int>>& prices) {ll f[m10][n10];ll p[m10][n10];memset(p,0,sizeof p)…

AI时代—ChatGPT-4.5的正确打开方式

前言 前些天发现了一个巨牛的人工智能学习网站&#xff0c;通俗易懂&#xff0c;风趣幽默&#xff0c;忍不住分享一下给大家&#xff1a;https://www.captainbed.cn/z ChatGPT体验地址 文章目录 前言4.5key价格泄漏ChatGPT4.0使用地址ChatGPT正确打开方式最新功能语音助手存档…

数据结构——Java实现栈和队列

一、栈 Stack 1.特点 &#xff08;1&#xff09;栈是一种线性数据结构 &#xff08;2&#xff09;规定只能从栈顶添加元素&#xff0c;从栈顶取出元素 &#xff08;3&#xff09;是一种先进后出的数据结构&#xff08;Last First Out&#xff09;LIFO 2.具体实现 Java中可…

win系统环境搭建(十二)——Windows系统下使用docker安装redis

windows环境搭建专栏&#x1f517;点击跳转 win系统环境搭建&#xff08;十二&#xff09;——Windows系统下使用docker安装redis 文章目录 win系统环境搭建&#xff08;十二&#xff09;——Windows系统下使用docker安装redis1.创建文件夹2.docker-compose.yaml配置文件3.red…

《WebKit 技术内幕》之四(1): 资源加载和网络栈

第四章 资源加载和网络栈 使用网络栈来下载网页和网页资源是渲染引擎工作的第一步 1.WebKit 资源加载机制 1.1 资源 网页本身就是一种资源、网页还需要依赖很多其他的资源(图片、视频) &#xff08;1&#xff09;HTML 支持的资源主要包括以下几种类型&#xff1a; HTML 页…

【动态规划】【C++算法】741摘樱桃

作者推荐 【动态规划】【数学】【C算法】18赛车 涉及知识点 动态规划 LeetCode741 摘樱桃 给你一个 n x n 的网格 grid &#xff0c;代表一块樱桃地&#xff0c;每个格子由以下三种数字的一种来表示&#xff1a; 0 表示这个格子是空的&#xff0c;所以你可以穿过它。 1 表…

Golang 搭建 WebSocket 应用(六) - 监控

我在上一篇文章中&#xff0c;提到了目前的认证方式存在一些问题&#xff0c;需要替换为一种更简单的认证方式。 但是最后发现&#xff0c;认证这个实在是没有办法简单化&#xff0c;认证本身又是另外一个不小的话题了&#xff0c;因此关于这一点先留个坑。 本文先讨论一下另外…

前端开发必备 HTML的常用标签(二)

目录 一、HTML语言 二、水平线标签 三、字体样式标签 四、注释和特殊符号 一、HTML语言 HTML&#xff08;Hypertext Markup Language&#xff09;是一种标记语言&#xff0c;用于创建网页的结构和内容。它由一系列的标签组成&#xff0c;这些标签定义了网页中各个元素的结…

商铺工厂119消防火灾SOS声光一键报警器平台联网

商铺工厂119消防火灾SOS声光一键报警器平台联网 1.设有火灾自动报警系统的建筑&#xff0c;宜选择符合相关现行国家技术标准的消防应急广播、火灾声光警报类产品&#xff0c;由火灾报警或消防联动控制器进行控制&#xff0c;在消防控制室应能一键启动全楼火灾声光警报或向全楼进…

阿里云腾讯七牛内容安全配置

一&#xff0c;阿里云 1&#xff0c;配置RAM角色权限 向RAM用户授权系统策略权限&#xff1a;AliyunYundunGreenWebFullAccess 2&#xff0c;内容安全控制台——授权访问OSS 不授权——会报错——no permission(not authorized about role AliyunCIPScanOSSRole)

Pytest插件“pytest-selenium” - 让自动化测试更简洁

在现代Web应用的开发中,自动化测试成为确保网站质量的重要手段之一。而Pytest插件 pytest-selenium 则为开发者提供了简单而强大的工具,以便于使用Python进行Web应用的自动化测试。本文将深入介绍 pytest-selenium 插件的基本用法和实际案例,助你轻松进入无忧的Web应用测试之…

LeetCode 9.回文数(python版)

需求 给你一个整数 x &#xff0c;如果 x 是一个回文整数&#xff0c;返回 true &#xff1b;否则&#xff0c;返回 false 。回文数是指正序&#xff08;从左向右&#xff09;和倒序&#xff08;从右向左&#xff09;读都是一样的整数。 例如&#xff0c;121 是回文&#xff0…

【惠友小课堂】正确认识「股骨头坏死」,必读!

在上一篇门诊故事中讲到的孙女士&#xff0c;一开始觉得自己腿疼的原因可能是股骨头坏死&#xff0c;结果在王教授的诊断下发现其实是髋关节发育不良&#xff0c;给出的专业建议是需要进行髋关节置换手术治疗。这恰恰反映了一个情况&#xff0c;很多病人会把“腿疼”的原因归咎…

【GitHub项目推荐--微软开源的可视化工具】【转载】

说到数据可视化&#xff0c;大家都很熟悉了&#xff0c;设计师、数据分析师、数据科学家等&#xff0c;都需要用各种方式各种途径做着数据可视化的工作.....当然许多程序员在工作中有时也需要用到一些数据可视化工具&#xff0c;如果工具用得好&#xff0c;就可以把原本枯燥凌乱…

中国电子学会2021年12月份青少年软件编程Scratch图形化等级考试试卷一级真题(含答案)

一、单选题&#xff08;共25题&#xff0c;每题2分&#xff0c;共50分&#xff09; 1.点击下列哪个按钮&#xff0c;可以让正在运行的程序停下来&#xff1f;&#xff08;&#xff09;(2分) A. B. C. D. 2.小乔完成了一个编程作品后&#xff0c;点击“文件”中的“保…

【Linux】Linux基本操作(二):rm rmdir man cp mv cat echo

承接上文&#xff1a; 【【Linux】Linux基本操作&#xff08;一&#xff09;&#xff1a;初识操作系统、ls、cd、touch、mkdir、pwd 】 目录 1.rmdir指令 && rm 指令&#xff1a; rmdir -p #当子目录被删除后如果父目录也变成空目录的话&#xff0c;就连带父目录一…

【设计模式】文件目录管理是组合模式吗?

组合模式是什么&#xff1f; 组合模式是一种将对象组合成树形结构以表示"部分-整体"的层次结构的设计模式。它使得用户对单个对象和组合对象的使用具有一致性。 组合模式在什么情况下使用&#xff1f; 当你发现你需要在代码中实现树形数据结构&#xff0c;让整体-部…

户外机器人区域覆盖算法仿真测试平台设计与实现(预告)

要求14周完成。一定要熟练掌握人工智能工具的使用。 起伏地形环境多机器人编队运动控制与路径规划研究_2016年中小结-CSDN博客 简要版本 随着机器人技术的快速发展&#xff0c;户外机器人在农业、环境监测、搜索与救援等领域的应用日益广泛。为了实现高效、准确的区域覆盖&…

基于docker创建nginx容器

docker一键安装可以参考我这个博客&#xff1a;一键安装docker 1.创建基础容器 docker run -p280:280 --name nginx -d nginx创建挂载到容器的宿主机文件夹 mkdir -p /home/000nginx-ebrms-ftp/html mkdir -p /home/000nginx-ebrms-ftp/logs mkdir -p /home/000nginx-ebrms-f…