Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. how to send self-defined event to eventfilter installed at son widget instead of mainwindow

how to send self-defined event to eventfilter installed at son widget instead of mainwindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
eventeventfilterevent handlingui and heritageboardview
4 Posts 3 Posters 673 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Z Offline
    Z Offline
    Zonghan Gan
    wrote on 27 Feb 2020, 11:35 last edited by VRonin
    #1

    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

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 27 Feb 2020, 12:01 last edited by
      #2

      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

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      Z 1 Reply Last reply 27 Feb 2020, 13:31
      2
      • V VRonin
        27 Feb 2020, 12:01

        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

        Z Offline
        Z Offline
        Zonghan Gan
        wrote on 27 Feb 2020, 13:31 last edited by
        #3

        @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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on 27 Feb 2020, 19:50 last edited by
          #4

          Hi,

          Why not generate key press events matching the correct key when getting your network packets ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3

          1/4

          27 Feb 2020, 11:35

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved