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. Custom QT Events
Forum Updated to NodeBB v4.3 + New Features

Custom QT Events

Scheduled Pinned Locked Moved Solved General and Desktop
qeventcustom eventqcustomevent
4 Posts 2 Posters 1.3k 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.
  • R Offline
    R Offline
    rtavakko
    wrote on 9 Jun 2020, 01:16 last edited by
    #1

    Hi guys,

    I have a very simple subclass of QEvent:

    #ifndef FFTEVENT_H
    #define FFTEVENT_H
    
    #include <eventdefs.h>
    
    #include <QDebug>
    
    class FFTEvent : public QEvent
    {
    public:
        FFTEvent(float val = 0.0f, float thresh = 50.0f);
    
        virtual float getValue();
        virtual float getThreshold();
    
    protected:
    
        float value;
        float threshold;
    };
    
    #endif // FFTEVENT_H
    

    My question is what is the best C++ approach to register this custom event class in a header so that I could pass the registered type to the constructor of the class?

    #include "fftevent.h"
    
    FFTEvent::FFTEvent(float val, float thresh) :
        QEvent(FFTEventType),
        value(val),
        threshold(thresh)
    {
        qDebug() << "Request to create event object of type " << FFTEventType << " created event object of type " << type();
    }
    

    Using a static const variable results in different values being registered every time the header below is included:

    #ifndef EVENTDEFS_H
    #define EVENTDEFS_H
    
    #include <QEvent>
    
    static const QEvent::Type FFTEventType = static_cast<QEvent::Type>(QEvent::registerEventType());
    
    #endif // EVENTDEFS_H
    

    I would use the QCustomEvent class but it doesn't seem to exist in QT 5.11 or 5.12.

    Cheers!

    B 1 Reply Last reply 9 Jun 2020, 02:00
    0
    • R rtavakko
      9 Jun 2020, 01:16

      Hi guys,

      I have a very simple subclass of QEvent:

      #ifndef FFTEVENT_H
      #define FFTEVENT_H
      
      #include <eventdefs.h>
      
      #include <QDebug>
      
      class FFTEvent : public QEvent
      {
      public:
          FFTEvent(float val = 0.0f, float thresh = 50.0f);
      
          virtual float getValue();
          virtual float getThreshold();
      
      protected:
      
          float value;
          float threshold;
      };
      
      #endif // FFTEVENT_H
      

      My question is what is the best C++ approach to register this custom event class in a header so that I could pass the registered type to the constructor of the class?

      #include "fftevent.h"
      
      FFTEvent::FFTEvent(float val, float thresh) :
          QEvent(FFTEventType),
          value(val),
          threshold(thresh)
      {
          qDebug() << "Request to create event object of type " << FFTEventType << " created event object of type " << type();
      }
      

      Using a static const variable results in different values being registered every time the header below is included:

      #ifndef EVENTDEFS_H
      #define EVENTDEFS_H
      
      #include <QEvent>
      
      static const QEvent::Type FFTEventType = static_cast<QEvent::Type>(QEvent::registerEventType());
      
      #endif // EVENTDEFS_H
      

      I would use the QCustomEvent class but it doesn't seem to exist in QT 5.11 or 5.12.

      Cheers!

      B Offline
      B Offline
      Bonnie
      wrote on 9 Jun 2020, 02:00 last edited by Bonnie 6 Sept 2020, 03:01
      #2

      @rtavakko
      What is the point to set a global variable as static in the header file?
      The static in front of a global variable is different from the one in front of a class member variable.
      If you only want it to be used from inside FFTEvent, then put it in the cpp file.
      If you may use it in other places:
      option1. make it a public static member variable of FFTEvent in the header file and init it in the cpp file.
      option2. declare it as extern, not static, in the header, and init it in the cpp file.
      option3. put it in the cpp file, and create a public static member function of FFTEvent to return the value of it.

      R 1 Reply Last reply 10 Jun 2020, 23:58
      1
      • B Bonnie
        9 Jun 2020, 02:00

        @rtavakko
        What is the point to set a global variable as static in the header file?
        The static in front of a global variable is different from the one in front of a class member variable.
        If you only want it to be used from inside FFTEvent, then put it in the cpp file.
        If you may use it in other places:
        option1. make it a public static member variable of FFTEvent in the header file and init it in the cpp file.
        option2. declare it as extern, not static, in the header, and init it in the cpp file.
        option3. put it in the cpp file, and create a public static member function of FFTEvent to return the value of it.

        R Offline
        R Offline
        rtavakko
        wrote on 10 Jun 2020, 23:58 last edited by
        #3

        @Bonnie Thanks for replying. My thought process was since QEvent takes a QEvent::Type as argument, then this means that it should be registered before in a global header which is what I'm trying to do. Having it in the class itself would work but is there a way to register events elsewhere?

        1 Reply Last reply
        0
        • R Offline
          R Offline
          rtavakko
          wrote on 5 Jul 2020, 20:25 last edited by
          #4

          Option 2 suggested above works best I think and is the cleanest solution. To sum it up:

          Header file for event definition:

          #ifndef EVENTDEFINITIONS_H
          #define EVENTDEFINITIONS_H
          
          #include <QEvent>
          
          extern const QEvent::Type TYPE_AUDIO;
          
          #endif // EVENTDEFINITIONS_H
          

          Cpp file for the event definitions where it is initialized:

          #include <eventdefinitions.h>
          
          const QEvent::Type TYPE_AUDIO = static_cast<QEvent::Type>(QEvent::registerEventType());
          

          Event subclass:

          #ifndef AUDIOEVENT_H
          #define AUDIOEVENT_H
          
          #include <eventdefinitions.h>
          
          #include <QDebug>
          
          class AudioEvent : public QEvent
          {
          
          public:
              explicit AudioEvent();
          
          };
          
          #endif // AUDIOEVENT_H
          
          #include "audioevent.h"
          
          AudioEvent::AudioEvent() : QEvent(TYPE_AUDIO)
          {
              qDebug()<<QString("Creating custom event(%1)").arg(static_cast<int>(type()));
          }
          
          1 Reply Last reply
          0

          • Login

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