So I have looked further and found that the event() function in QWidget is not virtual, which I'm not sure why that's that way. As far as code, I set up a gui project, and my MainWindow class creates and populates a ChildListWidget object which is derived from QListWidget. I found out that I had to implement mousePressEvent(), mouseReleaseEvent(), and mouseMoveEvent() in order to get QListWidget to no longer process these events. They were not being passed through my reimplementation of event(). At the very top of ChildListWidget::event(), I am printing the enum for the event type that I get. The enum for mouseButtonPress is 2 and for mouseButtonRelease is 3, but I see neither of these when I look at the output. My code is as follows. If I'm doing something wrong here I'd love to know about it:
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class ChildListWidget;
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
ChildListWidget *m_listWidget;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class ChildListWidget;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
ChildListWidget *m_listWidget;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp:
#include "mainwindow.h"
#include "childlistwidget.h"
#include "ui_mainwindow.h"
#include <QListWidget>
#include <QSize>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSize winsize
= this
->window
()->size
();
m_listWidget = new ChildListWidget(this);
m_listWidget->resize(winsize.width(), winsize.height() - 35);
m_listWidget->addItem(item);
m_listWidget->addItem(item);
m_listWidget->addItem(item);
m_listWidget->addItem(item);
m_listWidget->addItem(item);
m_listWidget->addItem(item);
m_listWidget->addItem(item);
this->setCentralWidget(m_listWidget);
}
#include "mainwindow.h"
#include "childlistwidget.h"
#include "ui_mainwindow.h"
#include <QListWidget>
#include <QSize>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QSize winsize = this->window()->size();
m_listWidget = new ChildListWidget(this);
m_listWidget->resize(winsize.width(), winsize.height() - 35);
QListWidgetItem* item;
item = new QListWidgetItem("Item 1");
m_listWidget->addItem(item);
item = new QListWidgetItem("Item 2");
m_listWidget->addItem(item);
item = new QListWidgetItem("Item 3");
m_listWidget->addItem(item);
item = new QListWidgetItem("Item 4");
m_listWidget->addItem(item);
item = new QListWidgetItem("Item 5");
m_listWidget->addItem(item);
item = new QListWidgetItem("Item 6");
m_listWidget->addItem(item);
item = new QListWidgetItem("Item 7");
m_listWidget->addItem(item);
this->setCentralWidget(m_listWidget);
}
To copy to clipboard, switch view to plain text mode
childlistwidget.h:
#ifndef CHILDLISTWIDGET_H
#define CHILDLISTWIDGET_H
#include <QListWidget>
class QGestureEvent;
class QSwipeGesture;
class QPanGesture;
{
Q_OBJECT
public:
explicit ChildListWidget
(QWidget *parent
= 0);
virtual bool event
(QEvent *e
);
bool gestureEvent(QGestureEvent *event);
void swipeTriggered(QSwipeGesture* gesture);
void panTriggered(QPanGesture* gesture);
};
#endif // CHILDLISTWIDGET_H
#ifndef CHILDLISTWIDGET_H
#define CHILDLISTWIDGET_H
#include <QListWidget>
class QGestureEvent;
class QSwipeGesture;
class QPanGesture;
class QEvent;
class ChildListWidget : public QListWidget
{
Q_OBJECT
public:
explicit ChildListWidget(QWidget *parent = 0);
virtual bool event(QEvent *e);
virtual void mousePressEvent(QMouseEvent *event);
virtual void mouseReleaseEvent(QMouseEvent *event);
virtual void mouseMoveEvent(QMouseEvent *event);
bool gestureEvent(QGestureEvent *event);
void swipeTriggered(QSwipeGesture* gesture);
void panTriggered(QPanGesture* gesture);
bool eventFilter(QObject *obj, QEvent *event);
};
#endif // CHILDLISTWIDGET_H
To copy to clipboard, switch view to plain text mode
childlistwidget.cpp:
#include "childlistwidget.h"
#include <QEvent>
#include <QGesture>
#include <QGestureEvent>
#include <QPainter>
#include <QEvent>
#include <QList>
#include <QDebug>
ChildListWidget
::ChildListWidget(QWidget *parent
) :{
grabGesture(Qt::PanGesture);
grabGesture(Qt::SwipeGesture);
grabGesture(Qt::PinchGesture);
installEventFilter(this);
}
bool ChildListWidget
::event(QEvent *e
) {
qDebug() << "ChildListWidget::event(): type(): " << e->type();
if (e
->type
() == QEvent::Gesture) { qDebug() << "QEvent::Gesture";
return gestureEvent(static_cast<QGestureEvent*>(e));
}
if (e
->type
() == QEvent::MouseButtonPress) { qDebug() << "QEvent::MouseButtonPress";
return true;
// do nothing
}
if (e
->type
() == QEvent::MouseButtonRelease) { qDebug() << "QEvent::MouseButtonRelease";
// do nothing
return true;
}
}
void ChildListWidget
::mousePressEvent(QMouseEvent *event
) {
Q_UNUSED(event);
event->accept();
}
void ChildListWidget
::mouseReleaseEvent(QMouseEvent *event
) {
Q_UNUSED(event);
event->accept();
}
void ChildListWidget
::mouseMoveEvent(QMouseEvent *event
) {
Q_UNUSED(event);
event->accept();
}
bool ChildListWidget::gestureEvent(QGestureEvent *event)
{
Q_UNUSED(event);
qDebug() << "ChildListWidget::gestureEvent()";
qDebug() << "Gesture type: " << event->gestures().at(0)->gestureType();
if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) {
//swipeTriggered(static_cast<QSwipeGesture *>(swipe));
} else if (QGesture *pan = event->gesture(Qt::PanGesture)) {
//panTriggered(static_cast<QPanGesture *>(pan));
}
return true;
}
void ChildListWidget::swipeTriggered(QSwipeGesture* gesture)
{
Q_UNUSED(gesture);
qDebug() << "ChildListWidget::swipeTriggered()";
}
void ChildListWidget::panTriggered(QPanGesture* gesture)
{
Q_UNUSED(gesture);
qDebug() << "ChildListWidget::panTriggered()";
}
{
Q_UNUSED(obj);
qDebug() << "ChildListWidget::eventFilter()";
if (event
->type
() == QEvent::KeyPress) { QKeyEvent *keyEvent
= static_cast<QKeyEvent
*>
(event
);
qDebug() << "Ate key press" << keyEvent->key();
return true;
} else {
return false;
}
}
#include "childlistwidget.h"
#include <QEvent>
#include <QGesture>
#include <QGestureEvent>
#include <QPainter>
#include <QEvent>
#include <QList>
#include <QDebug>
ChildListWidget::ChildListWidget(QWidget *parent) :
QListWidget(parent)
{
grabGesture(Qt::PanGesture);
grabGesture(Qt::SwipeGesture);
grabGesture(Qt::PinchGesture);
installEventFilter(this);
}
bool ChildListWidget::event(QEvent *e)
{
qDebug() << "ChildListWidget::event(): type(): " << e->type();
if (e->type() == QEvent::Gesture) {
qDebug() << "QEvent::Gesture";
return gestureEvent(static_cast<QGestureEvent*>(e));
}
if (e->type() == QEvent::MouseButtonPress) {
qDebug() << "QEvent::MouseButtonPress";
return true;
// do nothing
}
if (e->type() == QEvent::MouseButtonRelease) {
qDebug() << "QEvent::MouseButtonRelease";
// do nothing
return true;
}
return QListWidget::event(e);
}
void ChildListWidget::mousePressEvent(QMouseEvent *event)
{
Q_UNUSED(event);
event->accept();
}
void ChildListWidget::mouseReleaseEvent(QMouseEvent *event)
{
Q_UNUSED(event);
event->accept();
}
void ChildListWidget::mouseMoveEvent(QMouseEvent *event)
{
Q_UNUSED(event);
event->accept();
}
bool ChildListWidget::gestureEvent(QGestureEvent *event)
{
Q_UNUSED(event);
qDebug() << "ChildListWidget::gestureEvent()";
qDebug() << "Gesture type: " << event->gestures().at(0)->gestureType();
if (QGesture *swipe = event->gesture(Qt::SwipeGesture)) {
//swipeTriggered(static_cast<QSwipeGesture *>(swipe));
} else if (QGesture *pan = event->gesture(Qt::PanGesture)) {
//panTriggered(static_cast<QPanGesture *>(pan));
}
return true;
}
void ChildListWidget::swipeTriggered(QSwipeGesture* gesture)
{
Q_UNUSED(gesture);
qDebug() << "ChildListWidget::swipeTriggered()";
}
void ChildListWidget::panTriggered(QPanGesture* gesture)
{
Q_UNUSED(gesture);
qDebug() << "ChildListWidget::panTriggered()";
}
bool ChildListWidget::eventFilter(QObject *obj, QEvent *event)
{
Q_UNUSED(obj);
qDebug() << "ChildListWidget::eventFilter()";
if (event->type() == QEvent::KeyPress) {
QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
qDebug() << "Ate key press" << keyEvent->key();
return true;
} else {
return false;
}
}
To copy to clipboard, switch view to plain text mode
main.cpp:
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include <QtGui/QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks