Qt拖拽组件与键盘事件

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.mfbz.cn/a/336256.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

python 实现大语言模型中的概率论:两人轮流出手对决时取胜概率的推导

假设你跟朋友通过打赌投篮来打赌一万块。你们找到一个篮球框&#xff0c;然后约定轮流投篮&#xff0c;谁先投进谁赢。假设你投进的概率是 p&#xff0c;也就是投不进的概率是 1-p&#xff0c;你对手投进的概率是 q,投不进的概率是 1-q&#xff0c;如果由你先投&#xff0c;那么…

反序列化提升刷题(2)

今天的例题&#xff1a; <?phphighlight_file(__FILE__);class ctfshowvip{public $username;public $password;public $code;public function __construct($u,$p){$this->username$u;$this->password$p;}public function __wakeup(){if($this->username! || $thi…

解决 vue 项目开发越久 node_modules包越大的问题

解决 vue 项目开发越久 node_modules包越大的问题 node_modules.cache 文件&#xff08;编译缓存文件 可以删除 &#xff09; compression-webpack-plugin 禁止缓存 const CompressionPlugin require("compression-webpack-plugin");module.exports {plugins: [ne…

面向对象之深度优先和广度优先

面向对象深度优先和广度优先是什么&#xff1f; 二叉树的两种遍历是数据结构的经典考察题目, 广度遍历考察队列结构, 深度遍历考察递归 深度优先 先序遍历(父, 左子, 右子) 0, 1, 3, 7, 8, 4, 9, 2, 5, 6 中序遍历(左子, 父, 右子) 7, 3, 8, 1, 9, 4, 0, 5, 2, 6 后序遍历(左子…

SpringBoot跨域问题解决

前端访问后台接口时&#xff0c;浏览器报错&#xff0c;跨域无法访问。 报错信息如下&#xff1a; Response to preflight request doesnt pass access control check: No Access-Control-Allow-Origin header is present on the requested resource. 经过一番百度之后&#…

【JVM】运行时数据区

文章目录 运行时数据区程序计数器栈栈帧 堆方法区本地方法栈直接内存变量存储位置 面试题本地方法栈有什么用&#xff1f;没有程序计数器会怎么样&#xff1f;说一说Java的内存分布情况类存放在哪里&#xff1f;局部变量存放在哪里&#xff1f; 运行时数据区 java虚拟机在运行…

acwing讲解篇之93. 递归实现组合型枚举

文章目录 题目描述题解思路题解代码 题目描述 题解思路 本题相当于二叉树的深度优先遍历&#xff0c;树的第i层表示第i个数选或不选&#xff0c;当选择了m次左节点后退出 我们记录当前递归的深度deep 然后用state进行状态压缩&#xff0c;state第i位是1表示选第i个数&#xff…

FaFu--练习复盘--2

3、函数练习 3.1、函数表达式&#xff08;1&#xff09; 描述 根据以下公式计算数学表达式的值&#xff0c;并将结果作为函数值返回。在main()函数中输入x&#xff0c;调用函数fun(x)&#xff0c;并输出fun(x)的值。 输入 输入1行&#xff0c;包含1个double类型的浮点数&…

git22端口超时

笔记本换了个主板后&#xff0c;将内容用git上传到GitHub时发现22端口超时。 以为是网络啥的原因&#xff0c;但是用ssh -T gitgithub.com进行多次测试&#xff0c;发现不是网络问题。按照网上操作&#xff0c;在.ssh文件夹内将config文件进行修改&#xff0c;改成&#xff1a;…

python爬取图片(thumbURL和html文件标签分别爬取)

当查看源代码&#xff0c;发现网址在thumbURL之后时&#xff0c;用此代码: # 当查看源代码&#xff0c;发现网址在thumbURL之后时&#xff0c;用此代码:import requestsheaders {User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121…

cs231n assignment1——SVM

整体思路 加载CIFAR-10数据集并展示部分数据数据图像归一化&#xff0c;减去均值&#xff08;也可以再除以方差&#xff09;svm_loss_naive和svm_loss_vectorized计算hinge损失&#xff0c;用拉格朗日法列hinge损失函数利用随机梯度下降法优化SVM在训练集和验证集计算准确率&a…

vue项目执行依赖安装(npm i或npm install )报ls-remote -h -t异常

从git拉取的vue项目执行依赖安装时一直报错&#xff0c; 报错如下图&#xff1a;首先&#xff0c;查看了node版本、npm配置的镜像地址均没找到解决办法。 在命令行中直接输入git发现提示于是从网上搜到了一个博文https://blog.csdn.net/weixin_49159364/article/details/118198…

【LeetCode】141. 环形链表

leetcode题目链接 141. 环形链表 #include <stdio.h> #include <stdbool.h>struct ListNode {int val;struct ListNode* next; }; typedef struct ListNode ListNode;bool hasCycle(ListNode* head) {ListNode* slow head, * fast head;while (fast &&…

2023年上半年网络工程师真题(1/3)

1.固态硬盘的存储介质是&#xff08;B&#xff09;。 A.光盘 B.闪存 C.软盘 D.磁盘 SSD存储介质是FLASH(一块块的存储芯片)&#xff0c;HDD(机械硬盘)存储介质是磁盘(机械臂和盘道)&#xff0c;补充:U盘的存储介质也是FLASH闪存。 2.虚拟存储技术把&#xff08;A&#xf…

最长公共前缀(Leetcode14)

例题&#xff1a; 分析&#xff1a; 我们可以先定义两个变量 i &#xff0c; j&#xff0c; j表示数组中的每一个字符串&#xff0c; i 表示每个字符串中的第几个字符。一列一列地进行比较&#xff0c;先比较第一列的字符&#xff0c;若都相同&#xff0c;则 i &#xff0c;继…

可视化 | 【echarts】渐变条形+折线复合图

文章目录 &#x1f4da;html css&#x1f4da;js&#x1f407;总体框架&#x1f407;option配置项 &#x1f4da;html css html&#xff1a;在这主要是用于整合&#xff0c;将html、css、js连接在一块&#xff0c;虽然单个模板代码量不大&#xff0c;但还是先分开&#xff0…

AI语音合成工具-Lalamu Studio

近期&#xff0c;Lalamu Studio开启了beta版本测试&#xff1a;Lalamu Studio。该工具整合了TTS和lip sync功能&#xff0c;可以让任意视频中的人物开口说话&#xff0c;并精确模拟口型。 例如&#xff0c;选择一段视频素材&#xff0c;添加由Ai合成的语音&#xff0c;即可完成…

MyBatis 系列:MyBatis 源码环境搭建

文章目录 一、环境准备二、下载 MyBatis 源码和 MyBatis-Parent 源码三、创建空项目、导入项目四、编译 mybatis-parent五、编译 mybatis六、测试总结 一、环境准备 jdk&#xff1a;17 maven&#xff1a;3.9.5 二、下载 MyBatis 源码和 MyBatis-Parent 源码 Mybatis&#x…

求职中遇到的性格测试

怎样才能不被刷? 最主要的就是自己的性格特征跟当前应聘的岗位是否相符合&#xff0c;这个符合程度越高&#xff0c;通过率自然也就越高。正规的做法都有一个岗位模型&#xff0c;也叫岗位胜任力模型。 以大五人格测试为例&#xff0c;完整版包含30个性格维度&#xff0c;从…

Pyro —— 简介

目录 Differences between legacy and sparse pyro Getting started with pyro using shelf tools Related pyro nodes Sourcing DOP simulation SOP simulation Post-Processing Rendering Simple FX shelf tools Pyro为Houdini的体积流体&#xff08;volumetric flu…
最新文章