From 511920bd13f55a1fd8aeb815a13955f953f4a074 Mon Sep 17 00:00:00 2001 From: JesseQu Date: Fri, 29 Aug 2025 11:18:13 +0800 Subject: [PATCH] added shortcut to re-read the messages from queue TG-54 --- CMakeLists.txt | 5 +++-- main.cpp | 49 +++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 102f53b..e1a4153 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -37,7 +37,8 @@ add_compile_definitions(GIT_VERSION="${GIT_VERSION}") option(BUILD_SHARED_LIBS "Build shared libraries" ON) -find_package(Qt6 COMPONENTS Core Network REQUIRED) +find_package(Qt6 COMPONENTS Core Network Widgets REQUIRED) +find_package(qxtglobalshortcut REQUIRED) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) @@ -55,7 +56,7 @@ add_executable(events main.cpp ) -target_link_libraries(events Qt6::Core Qt6::Network qamqp) +target_link_libraries(events Qt6::Core Qt6::Network Qt6::Widgets qamqp qxtglobalshortcut) set_target_properties(events PROPERTIES AUTOMOC ON diff --git a/main.cpp b/main.cpp index 08eb156..a6ea3a9 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,9 @@ -#include +#include +#include #include +#include + #include "qamqpclient.h" #include "qamqpexchange.h" #include "qamqpqueue.h" @@ -10,7 +13,7 @@ class Receiver : public QObject Q_OBJECT public: Receiver(QObject *parent = 0) : QObject(parent) { - m_client.setAutoReconnect(true); + m_client.setAutoReconnect(true); } public Q_SLOTS: @@ -19,6 +22,10 @@ public Q_SLOTS: m_client.connectToHost(); } + void OneShot(void) { + m_client.connectToHost(); + } + private Q_SLOTS: void clientConnected() { QAmqpQueue *queue = m_client.createQueue("hello"); @@ -42,13 +49,13 @@ private Q_SLOTS: queue->get(false); } - qDebug() << " [*] Waiting for messages. To exit press CTRL+C"; + qDebug() << Qt::endl << " [*] Waiting for messages. To exit press CTRL+C"; - queue->ack(3, false); // Acknowledgement the 3rd message. + // queue->ack(3, false); // Acknowledgement the 3rd message. - queue->reopen(); + // queue->reopen(); - // m_client.disconnectFromHost(); + m_client.disconnectFromHost(); } void messageReceived() { @@ -71,10 +78,36 @@ private: int main(int argc, char **argv) { - qDebug() << " Recieve starts ... "; - QCoreApplication app(argc, argv); + qDebug() << " " << Qt::endl; + QApplication app(argc, argv); + + QTextStream out(stdout); + QTextStream err(stderr); + + const QKeySequence shortcut("Ctrl+Shift+Z"); + const QxtGlobalShortcut globalShortcut(shortcut); + + if ( !globalShortcut.isValid() ) { + err << QString("Error: Failed to set shortcut %1") + .arg(shortcut.toString()) << Qt::endl; + return 1; + } + + out << QString("Press shortcut %1 (or CTRL+C to exit)").arg(shortcut.toString()) << Qt::endl; + Receiver receiver; + + QObject::connect( + &globalShortcut, &QxtGlobalShortcut::activated, &globalShortcut, + [&]{ + // out << QLatin1String("Shortcut pressed!") << Qt::endl; + receiver.OneShot(); + // QApplication::quit(); + }); + + receiver.start(); + return app.exec(); }