how to send self-defined event to eventfilter installed at son widget instead of mainwindow
-
Recently I need to substitute the event type of keypress of a Pong Gui with a self defined event type MyEvent(to Use UDP signal instead <- -> to control the game). However the problem is that seems for self-defined event I need to manually send or post them, while the event filter can't receive the self-defined event (the inbuilt keypress or mouseclick seems need no manually send). The event filter was installed as in mainwindow.cpp: ui->boardView->installEventFilter(gameloop iLoop); the eventfilter is realized in gameplay.cpp:
bool Gameplay::eventFilter(QObject *target, QEvent *e) { Q_UNUSED(target); bool handled = false; if(e->type() == MyEventType) { MyEvent *myevent = (MyEvent *)e; if ( myevent->sg >0) { //pong paddle move left iP1Direction = (iP1Direction == 0 ? -5 : 0); handled = true; } else if ( myevent->sg <0 ) { //pong paddle move right iP1Direction = (iP1Direction == 0 ? 5 : 0); handled = true; } } if(e->type() == MyEventType) { qDebug()<<"abc"; handled = true; } return handled; }
The main.cpp
#include <QtWidgets/QApplication> #include "mainwindow.h" #include "MyEvent.h" #include <iostream> #include <cmath> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); for(int i=0;i<1000;i++){ MyEvent myEvent1(MyEventType); myEvent1.set_ch(1); myEvent1.load_sg(1); QCoreApplication::postEvent(&w, &myEvent1); } return a.exec(); }
the mainwindow.cpp is
//cited from https://github.com/ynonp/Pong #include <QPen> #include <QResizeEvent> #include <QDebug> #include <QtWidgets/QApplication> #include "mainwindow.h" #include "MyEvent.h" #include <QCoreApplication> #include <QEvent> #include <QObject> #include <QDebug> #include <iostream> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow), iScore ( 0 ) { ui->setupUi(this); QGraphicsScene *scene = new QGraphicsScene(this); QGraphicsRectItem *p1 = new QGraphicsRectItem(0, 0, 80, 20); p1->setBrush(QBrush(Qt::blue)); QGraphicsRectItem *p2 = new QGraphicsRectItem(0, 0, 80, 20); p2->setBrush(QBrush(Qt::green)); QGraphicsEllipseItem *ball = new QGraphicsEllipseItem(0, 0, 15, 15); ball->setBrush(QBrush(Qt::magenta)); ui->boardView->setScene(scene); iLoop = new Gameplay(*scene, p1, p2, ball, this); QSize m(scene->sceneRect().size().width() + 10, scene->sceneRect().size().height() + 10); ui->boardView->setMinimumSize(m); resize(minimumSize()); ui->boardView->installEventFilter(iLoop); QObject::connect(iLoop, SIGNAL(goal(int)), this, SLOT(addScore(int))); } MainWindow::~MainWindow() { delete ui; } void MainWindow::addScore(int count) { iScore += count; ui->lcdNumber->display(iScore); }
So how can I feed my self defined event into the GUI event filter?
Thanks
-
From https://doc.qt.io/qt-5/qcoreapplication.html#postEvent
The event must be allocated on the heap since the post event queue will take ownership of the event and delete it once it has been posted.
In any case I think your logic is just messed up.
It should be:
key pressed event determines if you pressed left/right → call moveLeft()/moveRight() → repaint the bar, etc.This way, if you need to receive inputs from UDP rather than the keyboard you just plug in at the second stage of the chain, you don't have to add another step where you send a custom hybrid event
-
@VRonin Yes but the problem is the time. It's a crouse work which allowed to use opensource GUI and we can't write the whole gui before the deadline (we're generally qt fresher). The most possible way we found is to package a event when the signal over threshold and substitute the original keypress event of existed gui.
And sorry for the mistakes of the code as well. I correct the declare of the event. but it can still just be sent to the mainwindow w, not the ui->boardView. The latest code is on https://github.com/Zonghan-Barry-Gan/RTEP-EMG/tree/master/Pong_gui_test0227_latest_v -
Hi,
Why not generate key press events matching the correct key when getting your network packets ?