从 QListView 获取文本

发布于 2024-07-28 23:08:34 字数 61 浏览 3 评论 0 原文

我有一个指向第三方 QListView 对象的指针,它只是显示文本行。 获取该文本字符串的最佳方法是什么?

I have a pointer to a third party QListView object, which is simply displaying rows of text. What is the best way of getting a hold of that string of text?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千寻… 2024-08-04 23:08:34

该模型可通过 QListView::model() 访问,保存项目。 您可以执行以下操作:

QListView* view ; // The view of interest

QAbstractItemModel* model = view->model() ;
QStringList strings ;
for ( int i = 0 ; i < model->rowCount() ; ++i )
{
  // Get item at row i, col 0.
  strings << model->index( i, 0 ).data( Qt::DisplayRole ).toString() ;
}

您还提到您希望在写入文本时获取更新的字符串 - 您可以通过将模型的 dataChanged() 信号连接到提取字符串的函数来实现此目的。 请参阅 QAbstractItemModel::dataChanged()

The model, accessible by QListView::model(), holds the items. You can do something like this:

QListView* view ; // The view of interest

QAbstractItemModel* model = view->model() ;
QStringList strings ;
for ( int i = 0 ; i < model->rowCount() ; ++i )
{
  // Get item at row i, col 0.
  strings << model->index( i, 0 ).data( Qt::DisplayRole ).toString() ;
}

You also mention you would like to obtain the updated strings when text is written - you can do this by connecting the model's dataChanged() signal to your function that extracts strings. See QAbstractItemModel::dataChanged().

万人眼中万个我 2024-08-04 23:08:34

您可以向 QListView 对象询问其根 QModelIndex 并使用它来使用同级/子级方法迭代不同的条目。 您可以通过使用指定为 Qt::DisplayRole 的角色调用索引上的数据方法来访问与每个索引关联的文本。

有关更多详细信息,请参阅以下文档:

QAbstractItemView - QListView 的父类

QModelIndex

You can ask the QListView object for its root QModelIndex and use that to iterate over the different entries using the sibling/children methods. You can access the text associated with each index by calling the data method on the index with the role specified as the Qt::DisplayRole.

For more details see the following documentation:

QAbstractItemView - parent class to QListView

QModelIndex

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文