gpt4 book ai didi

c++ - QItemSelectionModel::selectedIndexes() 崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:28:49 26 4
gpt4 key购买 nike

我一直在四处寻找这个问题的解决方案,我开始相信这可能是 Qt 函数本身的一个错误。问题是,在您调用 QItemSelectionModel::selectedIndexes() 之后,当程序试图破坏此函数返回的 QModelIndexList 时,程序将崩溃。在崩溃之前,您会收到以下调试消息:

Debug Assertion Failed!
(…)
File: f:\dd\vctools\crt_bld\self_x86\crt\src\dbgheap.c
Line:1419
Expression: _pFirstBlock == pHead
(…)

下面是最简单的会导致问题的代码,大家可以自行测试:

#include <QApplication>
#include <QStringList>
#include <QStringListModel>
#include <QItemSelectionModel>
#include <QModelIndex>
#include <QModelIndexList>
#include <QListView>
#include <QItemSelectionModel>

void doSomethingWithSelection(QItemSelectionModel* selectionmodel);

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QStringList list;
list.push_back("1");
list.push_back("2");
list.push_back("3");
list.push_back("4");
QStringListModel model(list);
QListView view;
view.setModel(&model);
view.setSelectionMode(QAbstractItemView::ExtendedSelection);

QItemSelectionModel *selectionmodel = view.selectionModel();
QModelIndex first = model.index(0);
QModelIndex last = model.index(2);
QItemSelection selection(first, last);
selectionmodel->select(selection,QItemSelectionModel::Select);

doSomethingWithSelection(selectionmodel);

view.show();
return a.exec();
}

void doSomethingWithSelection(QItemSelectionModel* selectionmodel)
{
QModelIndexList indexlist = selectionmodel->selectedIndexes();
// this is what causes the error. I put this inside a function
// so the error will happen when exiting the function,
// when the program try to destroy the list nodes.
}

最佳答案

我有完全相同的问题,只是我使用的是选定的行而不是选定的索引。当 QModelIndexList 中的内部 QList 尝试从堆中删除索引时,我将其缩小为函数退出。不确定如何修复它,但我阅读了这篇有趣的帖子 here并想出了这个功能:

QModelIndexList Class::getViewSelection(QAbstractItemView *view ,  int columnNumber) const
{
QModelIndexList list;
for (int row = 0; row < view->model()->rowCount(view->rootIndex()); ++row)
{
QModelIndex index = view->model()->index(row, columnNumber, view->rootIndex());

if (view->selectionModel()->isSelected(index))
list.push_back(index);
}
return list;
}

我编写此函数是为了获取选定的行,因此列参数是您想要索引的列。

关于c++ - QItemSelectionModel::selectedIndexes() 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15123109/

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