1
0
mirror of https://github.com/mpv-player/mpv synced 2025-02-27 19:01:07 +00:00

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.
This commit is contained in:
wm4 2014-12-30 22:37:56 +01:00
parent 10f51a8c57
commit 38976703ca
3 changed files with 19 additions and 20 deletions

View File

@ -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()

View File

@ -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;

View File

@ -6,7 +6,7 @@ TARGET = qtexample
TEMPLATE = app
CONFIG += link_pkgconfig debug
PKGCONFIG = mpv
PKGCONFIG += mpv
SOURCES += qtexample.cpp
HEADERS += qtexample.h