调用QDBus冻结UI

时间:2016-01-18 10:56:11

标签: qt qthread qdbus

我有一个使用QDBus连接到蓝牙打印机的应用程序。配对打印机后,我在Connect界面上拨打org.bluez.Serial。我意识到这会在调用返回之前冻结UI。我创建了一个工作对象,然后移动到一个单独的QThread,它也没有解决问题。

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

这就是我在Connect函数中调用BluetoothPrinter::Worker::print的方式。

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;
    }
    ...........
}

感谢。

0 个答案:

没有答案