From 38976703ca900cd07f10415637b95fa7bf99e738 Mon Sep 17 00:00:00 2001 From: wm4 Date: Tue, 30 Dec 2014 22:37:56 +0100 Subject: DOCS/client_api_examples: qtexample: use queued signals Use queued signals instead of QEvent for the wakeup notification. This is slightly nicer, and reduces the chance that the event (QEvent::User) could clash with other code using the same event. Also switch to modern connect() syntax. --- DOCS/client_api_examples/qtexample.cpp | 30 ++++++++++++++---------------- DOCS/client_api_examples/qtexample.h | 7 ++++--- DOCS/client_api_examples/qtexample.pro | 2 +- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/DOCS/client_api_examples/qtexample.cpp b/DOCS/client_api_examples/qtexample.cpp index bcda45ef9b..b527575dce 100644 --- a/DOCS/client_api_examples/qtexample.cpp +++ b/DOCS/client_api_examples/qtexample.cpp @@ -29,7 +29,7 @@ static void wakeup(void *ctx) // the Qt GUI thread to wake up (so that it can process events with // mpv_wait_event()), and return as quickly as possible. MainWindow *mainwindow = (MainWindow *)ctx; - QCoreApplication::postEvent(mainwindow, new QEvent(QEvent::User)); + emit mainwindow->mpv_events(); } MainWindow::MainWindow(QWidget *parent) : @@ -42,7 +42,7 @@ MainWindow::MainWindow(QWidget *parent) : QAction *on_open = new QAction(tr("&Open"), this); on_open->setShortcuts(QKeySequence::Open); on_open->setStatusTip(tr("Open a file")); - connect(on_open, SIGNAL(triggered()), this, SLOT(on_file_open())); + connect(on_open, &QAction::triggered, this, &MainWindow::on_file_open); menu->addAction(on_open); statusBar(); @@ -89,8 +89,10 @@ MainWindow::MainWindow(QWidget *parent) : mpv_request_log_messages(mpv, "info"); // From this point on, the wakeup function will be called. The callback - // can come from any thread, so we use the Qt QEvent mechanism to relay - // the wakeup in a thread-safe way. + // can come from any thread, so we use the QueuedConnection mechanism to + // relay the wakeup in a thread-safe way. + connect(this, &MainWindow::mpv_events, this, &MainWindow::on_mpv_events, + Qt::QueuedConnection); mpv_set_wakeup_callback(mpv, wakeup, this); if (mpv_initialize(mpv) < 0) @@ -164,20 +166,16 @@ void MainWindow::handle_mpv_event(mpv_event *event) } } -bool MainWindow::event(QEvent *event) +// This slot is invoked by wakeup() (through the mpv_events signal). +void MainWindow::on_mpv_events() { - // QEvent::User is sent by wakeup(). - if (event->type() == QEvent::User) { - // Process all events, until the event queue is empty. - while (mpv) { - mpv_event *event = mpv_wait_event(mpv, 0); - if (event->event_id == MPV_EVENT_NONE) - break; - handle_mpv_event(event); - } - return true; + // Process all events, until the event queue is empty. + while (mpv) { + mpv_event *event = mpv_wait_event(mpv, 0); + if (event->event_id == MPV_EVENT_NONE) + break; + handle_mpv_event(event); } - return QMainWindow::event(event); } void MainWindow::on_file_open() diff --git a/DOCS/client_api_examples/qtexample.h b/DOCS/client_api_examples/qtexample.h index 672a818844..f59738cfbd 100644 --- a/DOCS/client_api_examples/qtexample.h +++ b/DOCS/client_api_examples/qtexample.h @@ -15,11 +15,12 @@ public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); -protected: - virtual bool event(QEvent *event); - private slots: void on_file_open(); + void on_mpv_events(); + +signals: + void mpv_events(); private: QWidget *mpv_container; diff --git a/DOCS/client_api_examples/qtexample.pro b/DOCS/client_api_examples/qtexample.pro index 84c34031d3..40a4d964e2 100644 --- a/DOCS/client_api_examples/qtexample.pro +++ b/DOCS/client_api_examples/qtexample.pro @@ -6,7 +6,7 @@ TARGET = qtexample TEMPLATE = app CONFIG += link_pkgconfig debug -PKGCONFIG = mpv +PKGCONFIG += mpv SOURCES += qtexample.cpp HEADERS += qtexample.h -- cgit v1.2.3