c++ - QtContainer 中的 QStackedWidget 中的双重删除

标签 c++ qt

我的表单中有一个 QStackedWidget,我正在动态添加某些小部件。

    m_lineEdit = new QLineEdit();
    m_label = new QLabel();
    m_lineEdit->setText(name());
    m_label->setText(name());
    innerUi->stackedWidget->addWidget(m_lineEdit);
    innerUi->stackedWidget->addWidget(m_label);
    connect(m_lineEdit, &QLineEdit::editingFinished, [this]() {
        m_label->setText(m_lineEdit->text());
        innerUi->stackedWidget->setCurrentWidget(m_label);
    });
    innerUi->stackedWidget->setCurrentWidget(m_label);

当我使用它时,效果很好。但是当我关闭应用程序时它崩溃了。

我尝试调查该问题并观察到以下内容。 stackedwidget 的父级存储在 QVector 中。有时我会动态地从 QVector 中删除元素。

m_tasks.removeOne(task); 
mainUi->tasksLayout->removeWidget(task);
task->setParent(nullptr); 
mainUi->statusLabel->setText(getCompleteStatus());
delete task;

因此,这会导致双重删除。有人可以建议我如何解决这个问题吗?

最佳答案

尝试使用 QObject::deleteLater而不是 delete。您正确地将 parent 设置为 null,这意味着默认 Qt 机制不应删除您的对象,但它仍可能被未删除的信号/插槽连接调用。

关于c++ - QtContainer 中的 QStackedWidget 中的双重删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48038982/

相关文章:

c++ - 在 x86 中增加一个整数原子吗?

C++ 缓存设计建议

c++ - VLC-Qt 在 Windows 错误上从源构建

linux - qml 从控制台工作,但在 qt creator 下不工作

c++ - 如何在 Windows 上安装 MySQL C++ 驱动程序

c++ - 我在这里滥用继承吗?什么是最佳实践替代方案/模式?

c++ - 如何使用QT获取计算机的本地IP地址

c++ - stringstream 的模糊重载

c++ - 连接 QSqlDatabase 有什么区别?

qt - 当我设置边框半径时,角消失了