• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

QT6实现按钮双击事件

武飞扬头像
虚空道人
帮助1

实现思路:利用事件过滤器实现按钮双击功能,主要是通过调用installEventFilter函数和重写事件eventFilter函数实现。

1、创建项目

学新通

启动Qt Creator,在主菜单的[文件]下,打开[新建文件或项目]菜单,弹出新建文件或项目对话框,创建Qt Widgets Application 项目,在Class Information步骤中Base class 选择Qwidget,其他基本默认即可。

学新通

在界面增加1个列表控件和1个按钮控件,为按钮添加单机事件,界面设计如上所示。

2、代码

widget.ui

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <ui version="4.0">
  3.  
    <class>Widget</class>
  4.  
    <widget class="QWidget" name="Widget">
  5.  
    <property name="geometry">
  6.  
    <rect>
  7.  
    <x>0</x>
  8.  
    <y>0</y>
  9.  
    <width>565</width>
  10.  
    <height>424</height>
  11.  
    </rect>
  12.  
    </property>
  13.  
    <property name="windowTitle">
  14.  
    <string>Widget</string>
  15.  
    </property>
  16.  
    <widget class="QPushButton" name="btnDoubleTest">
  17.  
    <property name="geometry">
  18.  
    <rect>
  19.  
    <x>430</x>
  20.  
    <y>20</y>
  21.  
    <width>101</width>
  22.  
    <height>31</height>
  23.  
    </rect>
  24.  
    </property>
  25.  
    <property name="text">
  26.  
    <string>测试按钮</string>
  27.  
    </property>
  28.  
    </widget>
  29.  
    <widget class="QListWidget" name="listWidget">
  30.  
    <property name="geometry">
  31.  
    <rect>
  32.  
    <x>10</x>
  33.  
    <y>10</y>
  34.  
    <width>401</width>
  35.  
    <height>401</height>
  36.  
    </rect>
  37.  
    </property>
  38.  
    </widget>
  39.  
    </widget>
  40.  
    <resources/>
  41.  
    <connections/>
  42.  
    </ui>
学新通

widget.h

  1.  
    #ifndef WIDGET_H
  2.  
    #define WIDGET_H
  3.  
     
  4.  
    #include <QWidget>
  5.  
     
  6.  
    QT_BEGIN_NAMESPACE
  7.  
    namespace Ui { class Widget; }
  8.  
    QT_END_NAMESPACE
  9.  
     
  10.  
    class Widget : public QWidget
  11.  
    {
  12.  
    Q_OBJECT
  13.  
     
  14.  
    public:
  15.  
    Widget(QWidget *parent = nullptr);
  16.  
    ~Widget();
  17.  
     
  18.  
    bool eventFilter(QObject *watched, QEvent *event);
  19.  
     
  20.  
    private slots:
  21.  
    void on_btnDoubleTest_clicked();
  22.  
     
  23.  
    private:
  24.  
    Ui::Widget *ui;
  25.  
    };
  26.  
    #endif // WIDGET_H
学新通

widget.cpp

  1.  
    #include "widget.h"
  2.  
    #include "./ui_widget.h"
  3.  
     
  4.  
    #include <QMouseEvent>
  5.  
     
  6.  
    Widget::Widget(QWidget *parent)
  7.  
    : QWidget(parent)
  8.  
    , ui(new Ui::Widget)
  9.  
    {
  10.  
    ui->setupUi(this);
  11.  
     
  12.  
    ui->btnDoubleTest->installEventFilter(this);
  13.  
    }
  14.  
     
  15.  
    Widget::~Widget()
  16.  
    {
  17.  
    delete ui;
  18.  
    }
  19.  
     
  20.  
    bool Widget::eventFilter(QObject *watched, QEvent * event)
  21.  
    {
  22.  
    if(event->type()==QEvent::MouseButtonDblClick)
  23.  
    {
  24.  
    QMouseEvent * e = static_cast<QMouseEvent *>(event);
  25.  
     
  26.  
    if(e->button() == Qt::LeftButton)
  27.  
    {
  28.  
    if(watched==ui->btnDoubleTest)
  29.  
    ui->listWidget->addItem(QString("双击事件"));
  30.  
     
  31.  
    return true;
  32.  
    }
  33.  
    }
  34.  
     
  35.  
    return QWidget::eventFilter(watched, event);
  36.  
    }
  37.  
     
  38.  
    void Widget::on_btnDoubleTest_clicked()
  39.  
    {
  40.  
    ui->listWidget->addItem(QString("单击事件"));
  41.  
    }
学新通

3、运行效果

学新通

运行效果如上所示,单击按钮时触发单击事件,双击按钮时触发双击事件。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgfhicf
系列文章
更多 icon
同类精品
更多 icon
继续加载