QTreeView insertRows通过方法挤压/直接调用工作
QTreeView insertRows via method crushs / direct call works
我有以下奇怪的问题
我已经实现了一个QAbstractItemModel,我可以插入子节点到树视图,但当我试图通过insertRows()方法添加节点时,会发生一些奇怪的事情。
首先调用all:
QApplication a(argc, argv);
QResource::registerResource("Qt5Tutorial.rcc");
QTreeView *treeView = new QTreeView();
treeView->show();
Node rootNode("rootNode");
CameraNode childNode0("childNode0", &rootNode);
CameraNode childNode1("childNode1", &rootNode);
LightNode childNode2("childNode2", &rootNode);
CameraNode childNode3("childNode3", &childNode0);
TransformNode childNode4("childNode4", &childNode2);
TransformNode tryNode("potato");
// setup model
ObjectTreeModel model(&rootNode);
treeView->setModel(&model);
// insert directly via the insert child method
// this works!
childNode0.insertChild(1, &tryNode);
// get the QModelIndex of childNode1
// must be passed in the insertRows() method
QModelIndex index(model.index(1, 0, QModelIndex()));
// the output is "childNode1" what is totally right
qDebug() << "index: "<<static_cast<Node*>(index.internalPointer())->getName();
// output see posted beneath
qDebug() << rootNode.log();
// should insert in "childNode1" -> at 0th position and just 1 Node object
// see the method beneath
model.insertRows(0, 1, index);
// if i try to call the method rootNode.log(); now again, it crashes
return a.exec();
这是rootNode.log()调用的输出:
---rootNode
---childNode0
---childNode3
---potato
---childNode1
---childNode2
---childNode4
可以看到,"Potato"节点被正确插入。
查看图片http://www10.pic-upload.de/04.01.13/m65huuqq4ruu.png
但是一旦我尝试扩展childNode1节点,它就崩溃了。但是看看上面代码中的最后一条注释。正如我所提到的->如果我现在尝试输出树视图(它遍历所有节点),它会崩溃。
当方法被调用时,一切似乎都很好-就在我试图展开树视图时,它崩溃了->调试输出让我认为一切都应该是好的
实际的错误消息是读取位置…时的访问冲突。(翻译自德语-不知道在英语中是否叫相同)
bool ObjectTreeModel::insertRows(int position, int row, const QModelIndex &parent)
{
beginInsertRows(parent, position, position + row - 1);
Node *parentNode = getNode(parent);
qDebug() << "parentName: " << parentNode->getName();
bool success = false;
for(int i = position; i < row; i++)
{
qDebug() << "inside loop";
qDebug() << "position: " << position << "row: " << row;
TransformNode childNode("insertedNode");
success = parentNode->insertChild(i, &childNode);
qDebug() << "success: " << success;
}
endInsertRows();
return success;
}
上述方法的调试输出:
getNode: successful
parentName: "childNode1"
inside loop
position: 0 row: 1
called inserchild
success: true
我不知道为什么会发生这种情况,因为调试输出似乎是正确的,它应该基本上与通过insertChild方法直接插入节点相同。
我希望有人知道为什么它不起作用。
致以最诚挚的问候,Michael
几乎一切都是正确的。只有这两行不行:
TransformNode *childNode = new TransformNode("insertedNode");
success = parentNode->insertChild(i, childNode);
相关文章:
- 即使没有调用这个递归函数,它是如何工作的?
- 这个C++编译器优化(在自身的实例上调用对象自己的构造函数)的名称是什么,它是如何工作的?
- 推理类型如何工作"auto"和按引用调用?
- 以下代码如何工作以每次为唯一调用堆栈唯一实例化模板函数?
- 递归函数调用在后台工作
- std::async 如何工作:为什么它会调用这么多次复制/移动?
- QLibrary 函数在第一次调用时工作缓慢
- 工作线程一直在等待,condition_variable甚至调用了notify_all
- 虚拟基类在内部如何工作?编译器如何解析对基方法的调用?
- 在新作用域中使用unique_lock是否等效于在使用共享资源的工作结束时解锁调用
- 从Excel VBA调用C++DLL只能从Visual Studio中的调试实例工作
- 异步操作的 Asio 处理程序在其同步对应项正常工作时不会调用
- 如何使功能"run"工作。程序在调用运行时退出
- 当在函数调用中递增值时,程序正常工作,但是如果我们在单独的行中增加值而不是呼叫函数,则会出现错误.为什么
- 在线程中调用线程会给我带来错误,如果我只调用该函数,则可以正常工作
- boost::asio 使用 post() 时没有调用处理程序,当直接调用函数时有效(io_context有工作)
- 为什么用未申报的变量工作调用函子
- 引用调用在 c++ 中如何工作?
- C++:"such strings"类型。使像 fun( "str" ) 一样的函数调用工作
- QTreeView insertRows通过方法挤压/直接调用工作