QT中事件过滤器

这篇具有很好参考价值的文章主要介绍了QT中事件过滤器。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Qt添加事件过滤器,设置拖放listWidget、TreeWidget、TableWidget控件。
QT中事件过滤器,qt,命令模式,开发语言文章来源地址https://www.toymoban.com/news/detail-831611.html

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);



    //设置控件可以拖放
    ui->listWidgetSourse->setAcceptDrops(true);
    ui->listWidgetSourse->setDragEnabled(true);
    ui->listWidgetSourse->setDefaultDropAction(Qt::CopyAction);
    ui->listWidgetSourse->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);

    //为小部件添加事件过滤器
    ui->listWidgetSourse->installEventFilter(this);
    ui->listWidget->installEventFilter(this);
    ui->treeWidget->installEventFilter(this);
    ui->tableWidget->installEventFilter(this);
}

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

void MainWindow::refreshToUI(QGroupBox *currGroupBox)
{
    ui->checkBoxAcceptDrops->setChecked(m_itemView->acceptDrops());
    ui->checkBoxDropEnabled->setChecked(m_itemView->dragEnabled());

    ui->comboBox->setCurrentIndex((int)m_itemView->dragDropMode());
    int index = getGroupActionIndex(m_itemView->defaultDropAction());
    ui->comboBoxDefaultAction->setCurrentIndex(index);

    QFont fout = ui->groupBoxSourse->font();
    fout.setBold(false);
    ui->groupBox->setFont(fout);
    ui->groupBoxList->setFont(fout);
    ui->groupBoxSourse->setFont(fout);
    ui->groupBoxTree->setFont(fout);

    fout.setBold(true);
    currGroupBox->setFont(fout);
}

int MainWindow::getGroupActionIndex(Qt::DropAction actionType)
{
    switch (actionType) {
        case Qt::CopyAction :
            return 0;
        case Qt::MoveAction :
            return 1;
        case Qt::LinkAction :
            return 2;
        case Qt::ActionMask :
            return 3;
        default:
            return 0;
    }
}

Qt::DropAction MainWindow::getGroupActionTyp(int index)
{
    switch (index) {
        case 0:
            return Qt::CopyAction;
        case 1:
            return Qt::MoveAction;
        case 2:
            return Qt::LinkAction;
        case 3:
            return Qt::ActionMask;
        default:
            return Qt::CopyAction;
    }
}


//设置返回值,如果将返回值设置为eventFilter(watched,event);,程序直接奔溃,目前还没有找到原因
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
    if(event->type() != QEvent::KeyPress){
        return  false;
        // return  eventFilter(watched,event);
    }
    QKeyEvent * keyEvent = static_cast<QKeyEvent*>(event);
    if(keyEvent->key() != Qt::Key_Delete){
         return  false;
      // return   eventFilter(watched,event);
    }

    if(watched == ui->listWidgetSourse){//判断是否是listWidgetSourse委托
        delete ui->listWidgetSourse->takeItem(ui->listWidgetSourse->currentRow());//删除当前行
    }else if(watched == ui->listWidget){
        delete ui->listWidget->takeItem(ui->listWidget->currentRow());//删除当前行
    }else if(watched == ui->treeWidget){
        QTreeWidgetItem * treeItem = ui->treeWidget->currentItem();//获取当前索引项。tree中需要判断这个tree到底有没有父项
        if(treeItem->parent() != nullptr){//判断父索引是否为空
            treeItem->parent()->removeChild(treeItem);
        }else{
            int index = ui->treeWidget->indexOfTopLevelItem(treeItem);
            ui->treeWidget->takeTopLevelItem(index);
        }
        delete treeItem;
    }else if(watched == ui->tableWidget){
        delete ui->tableWidget->takeItem(ui->tableWidget->currentRow(),ui->tableWidget->currentColumn());
    }

    return  eventFilter(watched,event);
     //return true;
}


void MainWindow::on_radioButton_clicked()
{
    m_itemView = ui->listWidgetSourse;
    refreshToUI(ui->groupBoxSourse);
}

void MainWindow::on_radioButtonList_clicked()
{
    m_itemView = ui->listWidget;
    refreshToUI(ui->groupBoxList);
}

void MainWindow::on_radioButtonTree_clicked()
{
    m_itemView = ui->treeWidget;
    refreshToUI(ui->groupBoxTree);
}

void MainWindow::on_radioButtonTable_clicked()
{
    m_itemView = ui->tableWidget;
    refreshToUI(ui->groupBox);
}

到了这里,关于QT中事件过滤器的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Qt 事件过滤器使用QPainter绘制温度

    Qt的 eventFilter 是一个事件过滤器,可以用来捕获和处理Qt对象的事件。事件过滤器可以被安装到一个对象上,以便在该对象上拦截和处理包含特定类型和内容的事件。下面是 eventFilter 的简单使用介绍: 创建一个类,并继承自 QObject 。这个类将作为事件过滤器的实现。 在该类

    2024年02月12日
    浏览(26)
  • Qt之事件过滤器讲解并且实现快捷键切换鼠标焦点

    现在有一个类似于下方图的ui,用户需要在输入前一行内容后,需要摁下指定案件能够跳转到下一行继续进行输入。 一种更为直接的解决方案是子类化 QLineEdit 并且重新实现鼠标事件 keyPressEvent() ,然后调用 focusNextChild() 。 首先需要创建一个子类MyLineEdit继承于QLineEdit类。然后重

    2024年02月12日
    浏览(38)
  • 【VS Code 与 Qt6】运用事件过滤器批量操作子级组件

    如果某个派生自 QObject 的类重写 eventFilter 方法,那它就成了事件过滤器(Event Filter)。该方法的声明如下: watched 参数是监听事件的对象,即事件的接收者;event 参数当然就是待处理的事件了。事件过滤器(也可以翻译为“筛选器”)可在接收者之前拦截事件,处理完毕后还

    2024年02月08日
    浏览(37)
  • 结构型模式-过滤器模式

    允许开发人员使用不同的标准来过滤一组对象,通过逻辑运算以解耦的方式把它们连接起来。这种类型的设计模式属于结构型模式,它结合多个标准来获得单一标准。 结果: Males:  Person : [ Name : Robert, Gender : Male, Marital Status : Single ] Person : [ Name : John, Gender : Male, Marital Status

    2024年02月09日
    浏览(27)
  • 09 过滤器模式

    用来过滤数据,通过一个或多个定义过滤规则,窜起来过滤原始数据。 demo demo 打印

    2024年02月01日
    浏览(26)
  • 设计模式详解-过滤器模式(标准模式)

    在软件开发中,我们常常需要对大量的数据进行筛选或过滤。例如,在一个电子商务网站中,我们可能需要根据用户的喜好、价格区间、商品类型等条件来筛选出符合用户需求的商品列表。为了实现这样的功能,过滤器模式就派上了用场。 过滤器模式是一种结构型设计模式,

    2024年02月12日
    浏览(33)
  • Java设计模式(八)过滤器模式

    一、概要 过滤器模式是一种结构型设计模式,它允许通过一系列条件来筛选对象,并提供一种灵活的方式来组合和操作这些条件。过滤器模式将过滤条件封装成独立的过滤器类,然后使用这些过滤器来过滤对象集合,以满足特定的条件。 二、代码 以下是一个示例代码,说明

    2024年02月03日
    浏览(25)
  • Java设计模式之过滤器模式(Filter Pattern)

    过滤器模式(Filter Pattern)是一种常用的设计模式,它用于通过一系列条件来过滤对象集合,并将满足条件的对象保留下来。该模式可以帮助我们在不修改原始对象集合的情况下,根据特定的条件对集合进行筛选和处理。 在过滤器模式中,通常有三个核心角色: 过滤器接口(

    2024年02月07日
    浏览(34)
  • [后端开发] 过滤器相关注解

    使用Springboot框架开发后端,在鉴权的时候使用到了过滤器。但是在测试的过程发现,跨域的过滤器在过滤链中出现了两次,导致前端访问后端接口时报错:The \\\'Access-Control-Allow-Origin\\\' headers contains multiple values,but only one allowed.错误 在浏览器端比较正常访问接口和报错接口的hea

    2024年04月16日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包