当前位置:网站首页>QT control adds double click event

QT control adds double click event

2022-06-22 08:04:00 Licht powder

One 、 overloaded function mouseDoubleClickEvent


class SpinBoxAndCombox: public QWidget
{
Q_OBJECT
protected:
    //  Then re implement the mouse double click event 
    void mouseDoubleClickEvent(QMouseEvent* e);
public:
    SpinBoxAndCombox(QWidget* parent = nullptr) : SpinBoxAndCombox(parent){ };
};
 
void SpinBoxAndCombox::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (event->buttons() == Qt::LeftButton)
    {
        qDebug() << "12345";
    }
}

Two 、 Event filter

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
    
protected:
    virtual bool eventFilter(QObject * obj,QEvent *event) override;
 
private:
    Ui::MainWindow *ui;
};
 
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->widget->installEventFilter(this);//widget Control to install the event filter 
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
 
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
    if (obj == ui->widget) //  The object you want to filter 
            if (event->type() == QEvent::MouseButtonDblClick) 
            { //  The event type of the object you want to filter 
              //  What you want to achieve , My implementation here is to create a new tab and a new text edit bar 
                
                return true; //  Note that you must return here true, Indicates that you want to filter the original implementation of the event 
            }
        return false; //  return false Indicates no filtering , Also by default 
}

原网站

版权声明
本文为[Licht powder]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220800101319.html