像 QStringListModel 一样使用 QStandardItemModel
我正在尝试使用 QStandardItemModel 做与 QStringListModel 相同的事情(仅供练习):
http:// programmingexamples.net/wiki/Qt/ModelView/StandardItemModel
但是,显示了一个单元格,但它是空的,而不是像我期望的那样包含“文本”。谁能解释一下吗?这是使用 QStandardItemModel 的正确方法吗(即构建 QStandardItems 并将它们提供给模型?)
I am trying to use QStandardItemModel to do the same thing as QStringListModel (just for practice):
http://programmingexamples.net/wiki/Qt/ModelView/StandardItemModel
However, one cell shows up, but it is empty, as opposed to containing "text" as I would expect. Can anyone explain this? Is this the right way to go about using the QStandardItemModel (i.e. constructing QStandardItems and feeding them to the model?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实上,我很惊讶你没有发生车祸。您正在堆栈上创建
item0
,然后将指向它的指针传递给QList
。当该方法离开作用域时,item0
将被删除,并且您的列表包含一个指向用于保存QStandardItem
的内存腐烂区域的指针。通常,您只需创建新项目,然后使用 QStandardItemModel::setItem 之类的内容添加它,如下所示:
Actually, I'm surprised you aren't getting a crash. You are creating
item0
on the stack, then passing a pointer to it to theQList
. When that method leaves scope,item0
is deleted, and your list contains a pointer to the rotting area of memory that used to hold aQStandardItem
.Typically you would just create the new item, then add it using something like
QStandardItemModel::setItem
, like this: