Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QWidget::winEvent equivalent for qtbindings? #175

Open
casper opened this issue Dec 16, 2021 · 1 comment
Open

QWidget::winEvent equivalent for qtbindings? #175

casper opened this issue Dec 16, 2021 · 1 comment

Comments

@casper
Copy link

casper commented Dec 16, 2021

Does anyone know if QWidget::winEvent() or QApplication::winEventFilter() is somehow available on qtbindings?

I would like to do my own event processing in order to capture WM_COPYDATA for some simpe Windows IPC. I found some sample code for Qt C++ here:

https://stackoverflow.com/questions/1755196/receive-wm-copydata-messages-in-a-qt-app

I tried searching for winEvent and winEventFilter in the qtbindings source, but could not find anything. Are these methods missing or how to access them through Ruby?

@top-master
Copy link

https://stackoverflow.com/q/14610426/8740349

Qt5 replaced winEvent with nativeEvent.

Example

Imagine you need to be Qt 4 compatible, and your Window has some transparent parts, which you need to be click-through:

Header:

#ifndef MY_CLASS_H
#define MY_CLASS_H

#include <QtCore/QByteArray>
#include <QWidget>

class MyClass : public QWidget {
    typedef QWidget super;
public:

    // ...

protected:

#if defined(_WIN32)
#  if QT_VERSION_MAJOR >= 5
    bool nativeEvent(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
#  else
    bool winEvent(MSG *message, long *result) Q_DECL_OVERRIDE;
#  endif
#endif

private:
    Q_DISABLE_COPY(MyClass)
};

#endif // MY_CLASS_H

Source:

#include "my-class.h"

#include <QPoint>

#ifdef _WIN32
#   include <Windows.h>
#   include <windowsx.h>
#endif


#if defined(_WIN32)
#  if QT_VERSION_MAJOR >= 5
bool MyClass::nativeEvent(const QByteArray &eventType, void *messageVoid, long *result)
{
    if (eventType == "windows_generic_MSG")
    {
        MSG *message = reinterpret_cast<MSG *>(messageVoid);

#  else

bool MyClass::winEvent(MSG *message, long *result)
{
    {
#  endif // QT_VERSION_MAJOR

        if (message->message == WM_NCHITTEST) {
            QPoint pos = this->mapFromGlobal(QPoint(GET_X_LPARAM(message->lParam), GET_Y_LPARAM(message->lParam)));

            if ( ! contentsRect().contains(pos)) {
                *result = HTTRANSPARENT;
                return true; // Stops further processing.
            }
        }

    }
#  if QT_VERSION_MAJOR >= 5
    return super::nativeEvent(eventType, messageVoid, result);
#  else
    return super::winEvent(message, result);
#  endif // QT_VERSION_MAJOR

}
#endif // _WIN32

Note that Qt5 defines Q_OS_WIN and Qt4 defines Q_WS_WIN hence above uses _WIN32.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants