从 QListView 中选定的项目获取数据
我用 QSqlTableModel 填充 QListView。
projectModel = QSqlTableModel()
projectModel.setTable("project")
projectModel.select()
projectView = QListView()
projectView.setModel(projectModel)
projectView.setModelColumn(1)
- 稍微编辑一下我的措辞。 -
假设我的 QSqlTableModel 有 3 列。例如,(0) 代表 ID,(1) 代表名称,(2) 代表颜色。
在我的 QListView 中,我显示第 1 列,因此仅显示名称。然后我选择了 1 项。我怎样才能检索该项目的 ID 和颜色?
抱歉,我在这方面是个初学者。太初学者了,我无法通过阅读文档来弄清楚:(
I populate a QListView with QSqlTableModel.
projectModel = QSqlTableModel()
projectModel.setTable("project")
projectModel.select()
projectView = QListView()
projectView.setModel(projectModel)
projectView.setModelColumn(1)
- edit my wording a bit. -
Say my QSqlTableModel has 3 columns. (0) for id, (1) for name, (2) for color, for example.
In my QListView I'm displaying column 1, so only the names. Then I have 1 item selected. How can I retrieve that item's id and color?
Sorry I'm a very beginner at this. Too beginner I can't figure things out reading documentations :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
虽然我不完全理解你想要做什么,但我正在向你展示你的两个问题的答案。
例如我们有一个 sqlite3 数据库“sqlitedb.rdb”
和一个表名“info”,
并有一些数据,
现在,
为此,
您可以设置要使用查询显示的字段/列。
和,
您可以浏览查询结果并获取数据,例如,
您可以循环查询并访问数据。
Though i didn't fully understand what you are trying to do, I'm showing you the answer of your two question.
For example we have an sqlite3 database "sqlitedb.rdb"
And a table name "info",
and have some data,
Now,
To do this you can,
You can set which field/column you want to show using the query.
and,
You can navigate through the result of the query and have the data, example,
you can loop through the query and get access to the data.
请检查以下示例。您可以向 QListView 的“clicked”槽添加一个函数。然后获取选择了哪一行。
Please check the following example. You can add a function to the "clicked" slot of QListView. Then get the which row is selected.
您可以使用模型的
selectedIndexes()
方法来获取所选项目的索引,然后使用获取所需的值
idx 循环,您可以使用
x
您想要检索的列号val
现在是一个 QVariant,您可以进一步详细说明它。You can use the
selectedIndexes()
method of the model to get the indexes of the selected items using something likethen looping on idx, you can get the values you need with
with
x
beeing the number of the column you want to retriveval
is now a QVariant and you can further elaborate it.