0

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.

ramtheconqueror
  • 1,907
  • 1
  • 22
  • 35
  • There is reimplemented version of `call` method which takes `QDBus::CallMode mode` as first parameter. Try to play with this param. Also you may try to use `asyncCall` method. – Evgeny Jan 18 '16 at 15:54
  • Check if the QThread::currentThreadId is the right one where you call call() (and not the main thread). Alternatives would be using asynchronous API from within the main thread, e.g. callWithCallback(). – Frank Osterfeld Jan 18 '16 at 16:50
  • May be it freezes because you create the `dbusSerialInterface` object with the parent `this` which lives in the gui thread ? – Vladimir Bershov Jan 25 '16 at 07:03
  • @VladimirBershov, `dbusSerialInterface` is created in `print` function which is executed when thread starts (i.e., connected to `started` signal). will it be still created in GUI? – ramtheconqueror Jan 25 '16 at 09:14
  • @ramtheconqueror, I tell about `dbusSerialInterface` constructor last parameter - `this`. Does `this` point to a GUI object or no? Can you replace it to 0 (zero) and try to run? – Vladimir Bershov Jan 25 '16 at 09:21

0 Answers0