我有一个使用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;
}
...........
}
感谢。