I have an application which using QDBus
connects to a Bluetooth printer. After pairing the printer, I call Connect
on org.bluez.Serial
interface. I realised this is actally freezing the UI until the call returns. I created a worker object and then moved to a separate QThread
which didn't solve the problem either.
Worker* w = new Worker();
QThread* thread = new QThread();
w->moveToThread(thread);
connect(thread, &QThread::started, w, &BluetoothPrinter::Worker::print);
connect(w, &BluetoothPrinter::Worker::finished, thread, &QThread::quit);
connect(w, &BluetoothPrinter::Worker::finished, w, &BluetoothPrinter::Worker::deleteLater);
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
connect(w, &BluetoothPrinter::Worker::destroyed, []() { qDebug() << "worker destroyed"; });
connect(thread, &QThread::destroyed, []() { qDebug() << "thread destroyed"; });
thread->start();
This is how I call Connect
in BluetoothPrinter::Worker::print
function.
QDBusInterface dbusSerialInterface("org.bluez", objPath, "org.bluez.Serial", QDBusConnection::systemBus(), this);
if (dbusSerialInterface.isValid()) {
QDBusMessage reply = dbusSerialInterface.call("Connect", "spp"); // freezes UI here
if (reply.type() == QDBusMessage::ErrorMessage || reply.arguments().isEmpty()) {
emit finished();
return;
}
...........
}
Thanks.