Home > front end >  Qt::nativeEvent doesnt get called
Qt::nativeEvent doesnt get called

Time:10-11

Why does the nativeEvent function never get called?

    // .h
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
      
        bool nativeEvent(const QByteArray& eventType, void* message, long*);
    
    //private slots:
      //  virtual bool nativeEvent(const QByteArray & eventType, void * message, long * result) override;
    
    private:
        Ui::MainWindowClass ui;
    
    //protected:
      //  bool nativeEvent(const QByteArray& eventType, void* message, long* result);
    };

   
    //.cpp
    bool MainWindow::nativeEvent(const QByteArray& eventType, void* message, long* result)
    {
       //...
    }

I found some similar question where people suggest to define it as override but when i try:

bool nativeEvent(const QByteArray& eventType, void* message, long*) override;

It doesn't compile with the error: member function declared with 'override' does not override a base class member.

Commented lines was other ways I tried to define it, but I got the same error above.

CodePudding user response:

The signature of QWidget::nativeEvent() is:

bool QWidget::nativeEvent(
  const QByteArray &eventType, void *message, qintptr *result)

Thereby, qintptr is an

Integral type for representing pointers in a signed integer (useful for hashing, etc.).

Typedef for either qint32 or qint64. This type is guaranteed to be the same size as a pointer on all platforms supported by Qt. On a system with 32-bit pointers, qintptr is a typedef for qint32; on a system with 64-bit pointers, qintptr is a typedef for qint64.

(Emphasize mine.)

According to the reported error,
member function declared with 'override' does not override a base class member
on OPs platform sizeof (long) != sizeof (qintptr).

E.g. on my platform (Visual Studio 2019 with target x64)

  • sizeof (long): 4
  • sizeof (qintptr): 8.

If the signature of a virtual member function doesn't match the signature of the overridden virtual base class member function the latter is just not overridden. To make such cases detectable by the compiler, the override identifier was introduced to make the intention clear.

Hence, without override it just doesn't work. (I would've expected a warning of the compiler but that might depend on the compiler settings.) However, with override the compiler is forced to emit an error in this case.

The fix is obvious: OP should use qintptr* instead of long* for the last parameter result.

If it's expected to be in fact a long in the specific case of OP, it can be explicitly converted in the body of the function.


Please, note that sizeof (long) is platform dependent as well: Fundamental types and usually varies between 32 and 64 bit. Thereby, it's always 32 bit in Visual Studio (with x86 as well as with x64) but 64 bit in Linux with g and target x64. (I forgot how it is when g emits 32 bit code on Linux…)

  •  Tags:  
  • c qt
  • Related