【问题标题】:Running a QT MainWindow method on a separate thread在单独的线程上运行 QT MainWindow 方法
【发布时间】:2017-07-17 20:53:05
【问题描述】:

我正在编写一个 C++ QT5 Widget 桌面应用程序,当按下开始/停止按钮时,我需要在单独的线程上运行耗时的操作 MainWindow::performLengthyOperation(bool)。

这个耗时的操作在我的 MainWindow.h/cpp 中是一个相当冗长的方法。停止后台 IO 活动的操作大约需要 6 秒,启动大约需要 2 秒。在按下开始/停止按钮期间,UI 没有响应。本质上,在附加到我的按钮单击事件的插槽中,我需要执行以下逻辑。

void
MainWindow::on_pushButtonStart_clicked(bool checked)
{
    // temporarily disable the pushbutton 
    // until the lengthy operation completes
    mUI->pushButtonStart->setEnabled(false);

    if (checked) {
        // Start the timer tick callback
        mTickTimer.start(APP_TICK, this);
        mUI->pushButtonStart->setText("starting...");

        // This method needs to somehow run in its own QT thread
        // and when finished, call a slot in this MainWindow to
        // re-enable the pushButtonStart and change the statusBar
        // to indicate "runing..."
        performLengthyOperation(true);
        //mUI->pushButtonStart->setText("Stop")
        //mUI->statusBar->setStyleSheet("color: blue");
        //mUI->statusBar->showMessage("runing...");
    } else { // Stop the protocol threads
        // Stop the subsystem protocol tick timer
        mTickTimer.stop();
        mUI->pushButtonStart->setText("stopping...");
        // This method needs to somehow run in its own QT thread
        // and when finished, call a slot in this MainWindow to
        // re-enable the pushButtonStart and change the statusBar
        // to indicate "ready..."
        performLengthyOperation(false);
        // finally toggle the UI controls
        //mUI->pushButtonStart->setText("Start")
        //mUI->statusBar->setStyleSheet("color: blue");
        //mUI->statusBar->showMessage("ready...");
    }
}

当我在寻找有关如何执行此操作的示例时,我遇到了 following article,但我无法使其适应我的场景,因为我需要以某种方式将 MainWindow 放入 worker 中,以便它可以访问其 UI 小部件等这似乎有点矫枉过正。

理想情况下,我正在寻找一种简单的方法来异步运行 lambda 函数,我可以在其中放置这些耗时的操作(作为参数传入主窗口)。这比使用 QThreads 和将对象移动到线程等更可取。但是我对 QT 框架的了解还不够,无法知道这是否安全或可行。

【问题讨论】:

  • 即使您传入您的窗口,也无法从其他线程对其进行操作。您需要将这些操作编组回主线程(尽管我认为 Qt 可以通过信号/插槽做到这一点)。

标签: c++11 qt5 qtwidgets


【解决方案1】:

使用 std::async 和 lambdas:

将 std::future<void> future; 添加为 MainWindow 类的成员。

使用代码添加插槽OnLengthyOperationPerformed:

mUI->pushButtonStart->setText("Stop")
mUI->statusBar->setStyleSheet("color: blue");
mUI->statusBar->showMessage("runing...");
mUI->pushButtonStart->setEnabled(true);

添加信号 void LengthOperationPerformed(); 并将其与 MainWindow 构造函数中的插槽连接。将Qt::QueuedConnection 作为连接的第五个参数传递,因为您想从主线程调用您的插槽。

然后在on_pushButtonStart_clicked方法中你可以写:

future = std::async(std::launch::async, [this] {
  performLengthyOperation(true);
  emit LengthOperationPerformed();
});

与另一个呼叫相同。只需添加另一个插槽并发出信号或将标志传递给插槽。

长动作将在另一个线程中运行,当它完成时 MainWindow 接收信号并重新启用按钮。

【讨论】:

  • 没人。您不需要将来存储的值。
  • 谢谢,在构造函数中使用以下连接语法 connect(this, SIGNAL(LengthyMethodPerformed(const bool)), this, SLOT(updateStartStopButton(const QString&)), Qt::QueuedConnection);但是之后进行连接...我收到 qt_message_fatal 错误:QCoreApplication::sendEvent 中的 ASSERT 失败:“无法将事件发送到由不同线程拥有的对象。当前线程 34e6b60。接收器''('QSerialPort' 类型)创建于螺纹 40c460"。
  • 你不能用不同的参数类型连接信号和槽。我创建了一个示例应用程序,其中异步 lambda 向 mainWindow pastebin.com/nrzrxJVj 发出信号,您可以检查一下,看看我的意思。
  • 我怀疑上述错误的原因是我在 lambda 中的代码包括关闭一个串行设备-它在关闭时又会执行一些信号/插槽-错误源于 QSerialPort ::~QSerialPort.
  • 阅读此doc.qt.io/qt-4.8/threads-qobject.html 它解释了如何在多线程应用程序中使用以及如何不使用 Qt 对象。你的串口是在主线程还是在 lambda 中构造的?
猜你喜欢
  • 1970-01-01
  • 2015-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-19
相关资源
最近更新 更多