gpt4 book ai didi

qt - 如何在 QTimer 中检查 QtDesigner 生成的小部件点的有效性?

转载 作者:行者123 更新时间:2023-12-03 15:58:39 25 4
gpt4 key购买 nike

在我的 QDialog 代码中,我有以下内容:

QTimer::singleShot(2 * 1000, [&] {
if (theme_download_label) {
theme_download_label->hide();
theme_download_label->setText("text");
}
});

问题是如果我在计时器触发之前关闭对话框, theme_download_label->setText崩溃。如何处理?

我已经尝试设置 theme_download_labelQ_NULLPRclose() 之前方法无效。

最佳答案

  • 小部件的来源或是否是小部件都无关紧要。任何 QObject 的解决方案都是相同的。 .
  • 提供 QObject连接的上下文。当上下文对象死亡时,它会断开连接,因此不会调用仿函数。
  • 通过值而不是引用传递指向标签的指针,以防计时器超时 this .这是坠机的可能原因。
  • 无需检查 theme_download_label为空:不可能。这是与上下文对象连接的不变量:设计保证上下文对象是事件的。
    QTimer::singleShot(2 * 1000, theme_download_label, [label = theme_download_label] {
    label->hide();
    label->setText("text");
    });
  • 关于qt - 如何在 QTimer 中检查 QtDesigner 生成的小部件点的有效性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44444105/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com