如何在 QWidget 中的所有组件上安装事件过滤器
How to install an event filter on all components inside a QWidget
我的软件有很多组件(文本框、复选框、滑块...)。我需要做的是安装一个过滤器来记录用户完成的所有事件(点击复选框、滑动滑块……)保存它们并稍后重播。
我首先尝试在所有组件上安装过滤器,例如:
MainGui::MainGui( QWidget* parent ) :
QMainWindow( parent ),
ui( new Ui::MainWindow ),
m_console( new Console( this ) ),
m_serial( &port_qt::get() ),
m_status( new QLabel( "Bienvenue sur STEG.", this ) ),
m_mainLayout( new QHBoxLayout() ){
ui->setupUi( this );
QWidget* centralWidget = new QWidget();
centralWidget->setLayout( m_mainLayout );
setCentralWidget( centralWidget );
addAction( m_console->action() );
statusBar()->addWidget( m_status );
statusBar()->layout()->setMargin( 9 );
QCheckBox *checkbox_1 = new QCheckBox("check 1");
checkbox_1->setChecked (true);
addWidgetToColumn(1,checkbox_1);
m_recorder.setObj(checkbox_1); // Install filter here
.......;}
这段代码工作正常,我可以成功地记录和重放点击这个复选框。不幸的是,我需要安装一个过滤器,它可以在“centralWidget”内的所有组件上工作,当我把过滤器放在上面时,我没有得到任何与复选框相关的事件。
所以我正在寻找安装通用过滤器的解决方案。谢谢。
编辑:
使用qApp,我可以成功获取所有事件,但我现在的问题是重放这些事件。我正在使用 qApp->postEvent(m_App, clonedEvent); (m_App 是 qApp)。事件已发送,但未按应有的方式更改 Checkbox 检查状态。
我已经测试了这段代码,它应该可以满足您的要求
file mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
bool eventFilter(QObject *obj, QEvent *event);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
file mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QEvent>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
qApp->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
const QObjectList& list = ui->centralwidget->children(); // or centralwidget->children();
if(list.contains(obj))
{
if(event->type() == QEvent::MouseButtonPress)
{
qDebug() << "QEvent::MouseButtonPress";
if(obj == ui->pushButton)
qDebug() << "pushButton";
else if(obj == ui->checkBox)
qDebug() << "checkBox";
}
}
return QObject::eventFilter(obj, event);
}
如果您有任何问题,请告诉我。
我的软件有很多组件(文本框、复选框、滑块...)。我需要做的是安装一个过滤器来记录用户完成的所有事件(点击复选框、滑动滑块……)保存它们并稍后重播。
我首先尝试在所有组件上安装过滤器,例如:
MainGui::MainGui( QWidget* parent ) :
QMainWindow( parent ),
ui( new Ui::MainWindow ),
m_console( new Console( this ) ),
m_serial( &port_qt::get() ),
m_status( new QLabel( "Bienvenue sur STEG.", this ) ),
m_mainLayout( new QHBoxLayout() ){
ui->setupUi( this );
QWidget* centralWidget = new QWidget();
centralWidget->setLayout( m_mainLayout );
setCentralWidget( centralWidget );
addAction( m_console->action() );
statusBar()->addWidget( m_status );
statusBar()->layout()->setMargin( 9 );
QCheckBox *checkbox_1 = new QCheckBox("check 1");
checkbox_1->setChecked (true);
addWidgetToColumn(1,checkbox_1);
m_recorder.setObj(checkbox_1); // Install filter here
.......;}
这段代码工作正常,我可以成功地记录和重放点击这个复选框。不幸的是,我需要安装一个过滤器,它可以在“centralWidget”内的所有组件上工作,当我把过滤器放在上面时,我没有得到任何与复选框相关的事件。
所以我正在寻找安装通用过滤器的解决方案。谢谢。
编辑:
使用qApp,我可以成功获取所有事件,但我现在的问题是重放这些事件。我正在使用 qApp->postEvent(m_App, clonedEvent); (m_App 是 qApp)。事件已发送,但未按应有的方式更改 Checkbox 检查状态。
我已经测试了这段代码,它应该可以满足您的要求
file mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
protected:
bool eventFilter(QObject *obj, QEvent *event);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
file mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QApplication>
#include <QEvent>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
qApp->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{
const QObjectList& list = ui->centralwidget->children(); // or centralwidget->children();
if(list.contains(obj))
{
if(event->type() == QEvent::MouseButtonPress)
{
qDebug() << "QEvent::MouseButtonPress";
if(obj == ui->pushButton)
qDebug() << "pushButton";
else if(obj == ui->checkBox)
qDebug() << "checkBox";
}
}
return QObject::eventFilter(obj, event);
}
如果您有任何问题,请告诉我。