帮助在 Qt 的 rowInserted 信号后获取插入的数据
我有一个 onText 方法,它连接到 QAbstractItemModel 的 rowsInserted SIGNAL,这样我就可以在插入新行时收到通知:
QObject::connect(model, SIGNAL(rowsInserted ( const QModelIndex & , int , int ) ),
client_,SLOT(onText( const QModelIndex & , int , int )) )
该信号工作正常,因为我在插入行时收到通知。 这是 onText 方法:
void FTClientWidget::onText( const QModelIndex & parent, int start, int end )
{
Proxy::write("notified!");
if(!parent.isValid())
Proxy::write("NOT VALID!");
else
Proxy::write("VALID");
QAbstractItemModel* m = parent.model();
}
但我似乎无法从插入的项目中获取字符串。 传递的 QModelIndex“parent”无效,“m”QAbstractItemModel 为 NULL。 我认为这是因为它不是一个实际的项目,而只是一个指向一个项目的指针? 如何获取插入的文本/元素?
I have a onText method that connects to a QAbstractItemModel's rowsInserted SIGNAL so I can be notified when new rows have been inserted:
QObject::connect(model, SIGNAL(rowsInserted ( const QModelIndex & , int , int ) ),
client_,SLOT(onText( const QModelIndex & , int , int )) )
The signal works fine, since I am notified when rows are inserted. Here is the onText method:
void FTClientWidget::onText( const QModelIndex & parent, int start, int end )
{
Proxy::write("notified!");
if(!parent.isValid())
Proxy::write("NOT VALID!");
else
Proxy::write("VALID");
QAbstractItemModel* m = parent.model();
}
But I can't seem to be able to get the string from the inserted items. The QModelIndex "parent" passed is NOT VALID, and "m" QAbstractItemModel is NULL. I think its because it's not a actual item, but just a pointer to one? How do I get a hold of the inserted text/elements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于父项对于顶级项目无效,另一个选择是让 FTClientWidget 访问模型(如果它不违反您的预期设计),然后 FTClientWidget 可以直接在模型本身上使用开始和结束参数:
As the parent will be invalid for top level items, another option would be to give FTClientWidget access to the model (if it doesn't violate your intended design), and then FTClientWidget can use the start and end arguments directly on the model itself:
父级对于顶级项目始终无效,因此您可以预期它是无效的。 Qt 文档有一个很好的解释父母是如何工作的。
start
是插入子项的第一行,end
是插入子项的最后一行。因此,您可以通过如下方式访问它:
或者,如果您愿意,您可以使用索引来检索可以从中访问它的模型。
The parent will always be invalid for the top level items, so you can expect it to be invalid. The Qt documentation has a good explanation of exactly how the parent works.
start
is the first row at which a child has been inserted, andend
is the last row at which a child was inserted.Thus, you can access it with something like the following:
Or, if you want, you can use the index to retrieve the model from which you can access it.