【问题标题】:The program has unexpectedly finished in QDateTime::currentdatetime程序在 QDateTime::currentdatetime 意外结束
【发布时间】:2015-06-30 05:06:38
【问题描述】:

我正在运行一个线程,但有时程序会死掉。我希望你能告诉我为什么退出。错误

glibc 检测到 /home/hsr/CMBU-build-desktop-Qt_4_8_1_in_PATH__System__Release/CMBU:损坏的双链表:0x00000000026192e0

回溯:

00400000-00437000 r-xp 00000000 08:01 2499001 /home/hsr/CMBU-build-desktop-Qt_4_8_1_in_PATH__System__Release/CMBU
00636000-00637000 r--p 00036000 08:01 2499001 /home/hsr/CMBU-build-desktop-Qt_4_8_1_in_PATH__System__Release/CMBU
00637000-00638000 rw-p 00037000 08:01 2499001 /home/hsr/CMBU-build-desktop-Qt_4_8_1_in_PATH__System__Release/CMBU
02410000-02c5f000 rw-p 00000000 00:00 0 [堆]
7fe2ff74d000-7fe2ffb72000 r--p 00000000 08:01 2755027 /usr/share/fonts/truetype/nanum/NanumGothic.ttf
7fe2ffb72000-7fe300000000 rw-s 00000000 00:04 6717449 /SYSV00000000(已删除)
7fe300000000-7fe300022000 rw-p 00000000 00:00 0
7fe300022000-7fe304000000 ---p 00000000 00:00 0
7fe304000000-7fe304132000 rw-p 00000000 00:00 0
7fe304132000-7fe308000000 ---p 00000000 00:00 0
7fe308000000-7fe308022000 rw-p 00000000 00:00 0
7fe308022000-7fe30c000000 ---p 00000000 00:00 0
7fe30c26c000-7fe30c2cc000 rw-s 00000000 00:04 6750219 /SYSV00000000(已删除)
7fe30c2cc000-7fe30c6e4000 r--p 00000000 08:01 2755028 /usr/share/fonts/truetype/nanum/NanumGothicBold.ttf
7fe30c6e4000-7fe30c6e5000 ---p 00000000 00:00 0
7fe30c6e5000-7fe30cee5000 rw-p 00000000 00:00 0

void Thread::run()
{
    while (!stopped) {
        if(messageStr==tr("A")) {
            MainCMBU::ui->dateTimeEdit->setDateTime(QDateTime::currentDateTime());
            msleep(1000);
        }
    }
    //std::cerr << qPrintable(messageStr);
    stopped = false;
}

【问题讨论】:

  • 您无法从另一个线程访问 UI 组件。 stackoverflow.com/questions/10450750/…
  • @Ashif 线程可能是问题所在,但链接的问题是完全不同的语言和平台。
  • 尝试使用QMetaObject::invokeMethod(MainCMBU::ui->dateTimeEdit, "setDateTime", Q_ARG(QDateTime, QDateTime::currentDateTime());
  • @Yaroslav 谢谢,有区别吗?什么意思?

标签: c++ qt


【解决方案1】:
  1. 使用 QTimer 在 UI 线程中更新您的 UI
  2. 如果您打算工作,请使用同一个 QTimer 来工作
  3. 如果 GUI 的工作太多,请使用 QtConcurrent::run() 或类似方法来完成非 GUI 工作。如果您需要知道异步操作何时完成,请使用 QFutureWatcher。

像你这样在线程中睡觉通常是糟糕的设计。

在 Qt 中,您不能从非 GUI 线程访问 GUI 线程元素。 GUI 不是线程安全的。

【讨论】:

  • 怎么改?
  • 就像我告诉你的那样。您需要进行设计大修来修复您的代码。两者,您发布的内容和未发布的内容。
猜你喜欢
  • 1970-01-01
  • 2013-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多