Add support for Qt6 (#51)

CMake:
The 'QT_MAJOR' CMake variable controls which major version
of Qt is used for building. Defaults to '5', so nothing
changes when using the default 'cmake' invocation, as it
will default to search/build for Qt5.

qmake:
Just build in the usual way with 'qmake6 && make' or
'qmake-qt5 && make'.

When building with Qt6 on GNU/Linux and similar UNIXes,
the minimium required Qt version is 6.2.0, as Qt 6.1
and 6.0 lacks support for getting the X11 display.
This commit is contained in:
Daniel Bermond
2021-09-30 20:46:35 +03:00
committed by GitHub
parent eb7ddaba57
commit 25f21849b3
6 changed files with 112 additions and 25 deletions
+24 -8
View File
@@ -7,15 +7,27 @@ option(QHOTKEY_INSTALL "Enable install rule" ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
set(QT_MAJOR 5 CACHE STRING "Qt major version to use")
qt5_wrap_cpp(MOC_HEADERS
QHotkey/qhotkey.h
QHotkey/qhotkey_p.h)
if(QT_MAJOR EQUAL 6)
find_package(Qt${QT_MAJOR} 6.2.0 COMPONENTS Core Widgets REQUIRED)
else()
find_package(Qt${QT_MAJOR} COMPONENTS Core Widgets REQUIRED)
endif()
if(QT_MAJOR GREATER_EQUAL 6)
qt_wrap_cpp(MOC_HEADERS
QHotkey/qhotkey.h
QHotkey/qhotkey_p.h)
else()
qt5_wrap_cpp(MOC_HEADERS
QHotkey/qhotkey.h
QHotkey/qhotkey_p.h)
endif()
set(LIBS
Qt5::Core
Qt5::Widgets)
Qt${QT_MAJOR}::Core
Qt${QT_MAJOR}::Widgets)
set(SRC_FILES
QHotkey/qhotkey.cpp)
@@ -30,10 +42,14 @@ elseif(WIN32)
set(SRC_FILES ${SRC_FILES} QHotkey/qhotkey_win.cpp)
else()
find_package(X11 REQUIRED)
find_package(Qt5X11Extras REQUIRED)
if(QT_MAJOR GREATER_EQUAL 6)
set(LIBS ${LIBS} ${X11_LIBRARIES})
else()
find_package(Qt${QT_MAJOR}X11Extras REQUIRED)
set(LIBS ${LIBS} ${X11_LIBRARIES} Qt${QT_MAJOR}::X11Extras)
endif()
include_directories(${X11_INCLUDE_DIR})
set(LIBS ${LIBS} ${X11_LIBRARIES} Qt5::X11Extras)
set(SRC_FILES ${SRC_FILES} QHotkey/qhotkey_x11.cpp)
endif()
+8 -3
View File
@@ -1,12 +1,17 @@
set(CMAKE_INCLUDE_CURRENT_DIR ON)
qt5_wrap_ui(test_UI_HEADERS hottestwidget.ui)
qt5_wrap_cpp(test_MOC_HEADERS hottestwidget.h)
if(QT_MAJOR GREATER_EQUAL 6)
qt_wrap_ui(test_UI_HEADERS hottestwidget.ui)
qt_wrap_cpp(test_MOC_HEADERS hottestwidget.h)
else()
qt5_wrap_ui(test_UI_HEADERS hottestwidget.ui)
qt5_wrap_cpp(test_MOC_HEADERS hottestwidget.h)
endif()
add_executable(HotkeyTest
main.cpp
hottestwidget.cpp
${test_UI_HEADERS}
${test_MOC_HEADERS})
target_link_libraries(HotkeyTest Qt5::Widgets qhotkey)
target_link_libraries(HotkeyTest Qt${QT_MAJOR}::Widgets qhotkey)
target_include_directories(HotkeyTest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
+22 -5
View File
@@ -10,9 +10,15 @@ Q_LOGGING_CATEGORY(logQHotkey, "QHotkey")
void QHotkey::addGlobalMapping(const QKeySequence &shortcut, QHotkey::NativeShortcut nativeShortcut)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
const int key = shortcut[0].toCombined();
#else
const int key = shortcut[0];
#endif
QMetaObject::invokeMethod(QHotkeyPrivate::instance(), "addMappingInvoked", Qt::QueuedConnection,
Q_ARG(Qt::Key, Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask)),
Q_ARG(Qt::KeyboardModifiers, Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask)),
Q_ARG(Qt::Key, Qt::Key(key & ~Qt::KeyboardModifierMask)),
Q_ARG(Qt::KeyboardModifiers, Qt::KeyboardModifiers(key & Qt::KeyboardModifierMask)),
Q_ARG(QHotkey::NativeShortcut, nativeShortcut));
}
@@ -56,7 +62,12 @@ QKeySequence QHotkey::shortcut() const
{
if(_keyCode == Qt::Key_unknown)
return QKeySequence();
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
return QKeySequence((_keyCode | _modifiers).toCombined());
#else
return QKeySequence(static_cast<int>(_keyCode | _modifiers));
#endif
}
Qt::Key QHotkey::keyCode() const
@@ -88,8 +99,14 @@ bool QHotkey::setShortcut(const QKeySequence &shortcut, bool autoRegister)
"Only the first shortcut will be used!");
}
return setShortcut(Qt::Key(shortcut[0] & ~Qt::KeyboardModifierMask),
Qt::KeyboardModifiers(shortcut[0] & Qt::KeyboardModifierMask),
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
const int key = shortcut[0].toCombined();
#else
const int key = shortcut[0];
#endif
return setShortcut(Qt::Key(key & ~Qt::KeyboardModifierMask),
Qt::KeyboardModifiers(key & Qt::KeyboardModifierMask),
autoRegister);
}
@@ -354,7 +371,7 @@ uint qHash(QHotkey::NativeShortcut key)
return qHash(key.key) ^ qHash(key.modifier);
}
uint qHash(QHotkey::NativeShortcut key, uint seed)
QHOTKEY_HASH_SEED qHash(QHotkey::NativeShortcut key, QHOTKEY_HASH_SEED seed)
{
return qHash(key.key, seed) ^ qHash(key.modifier, seed);
}
+7 -1
View File
@@ -16,6 +16,12 @@
#define QHOTKEY_SHARED_EXPORT
#endif
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
#define QHOTKEY_HASH_SEED size_t
#else
#define QHOTKEY_HASH_SEED uint
#endif
//! A class to define global, systemwide Hotkeys
class QHOTKEY_SHARED_EXPORT QHotkey : public QObject
{
@@ -115,7 +121,7 @@ private:
};
uint QHOTKEY_SHARED_EXPORT qHash(QHotkey::NativeShortcut key);
uint QHOTKEY_SHARED_EXPORT qHash(QHotkey::NativeShortcut key, uint seed);
QHOTKEY_HASH_SEED QHOTKEY_SHARED_EXPORT qHash(QHotkey::NativeShortcut key, QHOTKEY_HASH_SEED seed);
QHOTKEY_SHARED_EXPORT Q_DECLARE_LOGGING_CATEGORY(logQHotkey)
+41 -7
View File
@@ -1,7 +1,17 @@
#include "qhotkey.h"
#include "qhotkey_p.h"
#include <QDebug>
#include <QX11Info>
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
#include <QGuiApplication>
#define _NATIVE_EVENT_RESULT qintptr
#else
#include <QDebug>
#include <QX11Info>
#define _NATIVE_EVENT_RESULT long
#endif
#include <QThreadStorage>
#include <QTimer>
#include <X11/Xlib.h>
@@ -16,7 +26,7 @@ class QHotkeyPrivateX11 : public QHotkeyPrivate
{
public:
// QAbstractNativeEventFilter interface
bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) Q_DECL_OVERRIDE;
bool nativeEventFilter(const QByteArray &eventType, void *message, _NATIVE_EVENT_RESULT *result) override;
protected:
// QHotkeyPrivate interface
@@ -52,13 +62,17 @@ NATIVE_INSTANCE(QHotkeyPrivateX11)
bool QHotkeyPrivate::isPlatformSupported()
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
return qGuiApp->nativeInterface<QNativeInterface::QX11Application>();
#else
return QX11Info::isPlatformX11();
#endif
}
const QVector<quint32> QHotkeyPrivateX11::specialModifiers = {0, Mod2Mask, LockMask, (Mod2Mask | LockMask)};
const quint32 QHotkeyPrivateX11::validModsMask = ShiftMask | ControlMask | Mod1Mask | Mod4Mask;
bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
bool QHotkeyPrivateX11::nativeEventFilter(const QByteArray &eventType, void *message, _NATIVE_EVENT_RESULT *result)
{
Q_UNUSED(eventType)
Q_UNUSED(result)
@@ -120,8 +134,16 @@ quint32 QHotkeyPrivateX11::nativeKeycode(Qt::Key keycode, bool &ok)
return 0;
}
if(QX11Info::isPlatformX11()) {
auto res = XKeysymToKeycode(QX11Info::display(), keysym);
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
const QNativeInterface::QX11Application *x11Interface = qGuiApp->nativeInterface<QNativeInterface::QX11Application>();
Display *display = x11Interface->display();
#else
const bool x11Interface = QX11Info::isPlatformX11();
Display *display = QX11Info::display();
#endif
if(x11Interface) {
auto res = XKeysymToKeycode(display, keysym);
if(res != 0)
ok = true;
return res;
@@ -146,8 +168,15 @@ quint32 QHotkeyPrivateX11::nativeModifiers(Qt::KeyboardModifiers modifiers, bool
bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
const QNativeInterface::QX11Application *x11Interface = qGuiApp->nativeInterface<QNativeInterface::QX11Application>();
Display *display = x11Interface->display();
#else
const bool x11Interface = QX11Info::isPlatformX11();
Display *display = QX11Info::display();
if(!display || !QX11Info::isPlatformX11())
#endif
if(!display || !x11Interface)
return false;
HotkeyErrorHandler errorHandler;
@@ -172,7 +201,12 @@ bool QHotkeyPrivateX11::registerShortcut(QHotkey::NativeShortcut shortcut)
bool QHotkeyPrivateX11::unregisterShortcut(QHotkey::NativeShortcut shortcut)
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0)
Display *display = qGuiApp->nativeInterface<QNativeInterface::QX11Application>()->display();
#else
Display *display = QX11Info::display();
#endif
if(!display)
return false;
+10 -1
View File
@@ -1,6 +1,15 @@
mac: LIBS += -framework Carbon
else:win32: LIBS += -luser32
else:unix {
QT += x11extras
equals(QT_MAJOR_VERSION, 6) {
lessThan(QT_VERSION, 6.2.0) {
error("Qt 6.2.0 or greater is required when using Qt6")
}
}
else {
lessThan(QT_MAJOR_VERSION, 6) {
QT += x11extras
}
}
LIBS += -lX11
}