QT C++ 解决调试运行时报 The inferior stopped 错误

(1)报错信息和报错时调用堆栈

SignaI Received - Qt Creator

The inferior stopped because it received a signal from the operating system.
Signal name: SIGSEGV
Signal meaning:Segmentation fault报错时调用堆栈停留在 void MyTableView::setModel() 函数的该行:

复制代码
报错时调用堆栈停留在 void MyTableView::setModel() 函数的该行:
    // 设置共享选择模型
    tableView->setSelectionModel(sharedSelectionModel);

// 函数代码
void MyTableView::setModel(QStandardItemModel *model) {
    tableView_model = model;
    tableView->setModel(model);
    listView_model = tableView_model;

    listView->setModel(listView_model);
    iconView->setModel(listView_model);

    // 创建共享选择模型。创建时需要使用 model变量,所以必须放在这里
    if (!sharedSelectionModel) {  // 避免重复创建
        sharedSelectionModel = new QItemSelectionModel(model, this);
    }

    // 设置共享选择模型
    tableView->setSelectionModel(sharedSelectionModel);
    tableView->resizeColumnsToContents();  // 自动调整列宽
    listView->setSelectionModel(sharedSelectionModel);
    iconView->setSelectionModel(sharedSelectionModel);
}
复制代码

 

  

(2)报错原因分析

      原因是sharedSelectionModel变量非法,因为下面的判断导致sharedSelectionModel变量没有被正常创建。
      因为sharedSelectionModel在 mytableview.h 定义为:
      QItemSelectionModel *sharedSelectionModel;
      在 MyTableView 构造函数中也未初始化。默认情况下,sharedSelectionModel 的值为随机数,if (!sharedSelectionModel) 判断时 sharedSelectionModel 为随机数并不为空,所以导致误判断 sharedSelectionModel 已经创建,导致sharedSelectionModel变量没有被正常创建。所以,下面的调用报 The inferior stopped 错误

// 创建共享选择模型。创建时需要使用 model变量,所以必须放在这里
if (!sharedSelectionModel) { // 避免重复创建
      sharedSelectionModel = new QItemSelectionModel(model, this);
}

(3)解决办法。
      清楚了报错原因,解决办法就很简单。在 MyTableView 构造函数中初始化 sharedSelectionModel 为 nullptr 即可解决。

MyTableView::MyTableView(QWidget *parent)
    : QWidget(parent),
      ...
      lastMouseEvent(nullptr),
      ...

 

  

 

posted on   patton88  阅读(465)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 推荐几款开源且免费的 .NET MAUI 组件库
· 实操Deepseek接入个人知识库
· 易语言 —— 开山篇
· Trae初体验

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示