从 QListView 中的选定项获取数据 [英] Getting data from selected item in QListView
问题描述
我用 QSqlTableModel 填充了一个 QListView.
I populate a QListView with QSqlTableModel.
projectModel = QSqlTableModel()
projectModel.setTable("project")
projectModel.select()
projectView = QListView()
projectView.setModel(projectModel)
projectView.setModelColumn(1)
- 稍微修改一下我的措辞.-
假设我的 QSqlTableModel 有 3 列.例如,(0) 为 id,(1) 为名称,(2) 为颜色.
Say my QSqlTableModel has 3 columns. (0) for id, (1) for name, (2) for color, for example.
在我的 QListView 中,我显示的是第 1 列,所以只有名称.然后我选择了 1 个项目.如何检索该项目的 ID 和颜色?
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 :(
推荐答案
虽然我没有完全理解你想要做什么,但我向你展示了你的两个问题的答案.
Though i didn't fully understand what you are trying to do, I'm showing you the answer of your two question.
例如我们有一个 sqlite3 数据库sqlitedb.rdb"还有一个表名info",
For example we have an sqlite3 database "sqlitedb.rdb" And a table name "info",
CREATE TABLE info (
id integer PRIMARY KEY AUTOINCREMENT DEFAULT NULL,
name TEXT(20) DEFAULT NULL,
country TEXT(20) DEFAULT NULL,
age integer)
还有一些数据,
INSERT INTO info (name,country,age) VALUES ('Leroy Hale','San Marino','46');
INSERT INTO info (name,country,age) VALUES ('William Coleman','Namibia','50');
INSERT INTO info (name,country,age) VALUES ('Phelan Waller','Belgium','43');
INSERT INTO info (name,country,age) VALUES ('Kato Martin','Virgin Islands, British','21');
INSERT INTO info (name,country,age) VALUES ('Jameson Mccoy','United Arab Emirates','45');
INSERT INTO info (name,country,age) VALUES ('Fulton Reeves','Belarus','34');
INSERT INTO info (name,country,age) VALUES ('Calvin Love','Morocco','28');
INSERT INTO info (name,country,age) VALUES ('Peter Solis','Bhutan','31');
INSERT INTO info (name,country,age) VALUES ('Connor Stephenson','Dominican Republic','26');
INSERT INTO info (name,country,age) VALUES ('Aristotle Smith','Chad','45');
现在
我只想显示 ID 和名称(第 0 列和第 1 列)我该怎么做?
I only want to display the id and name (column 0 and 1) how do I do that?
要做到这一点,您可以
from PyQt4.QtSql import QSqlQueryModel,QSqlDatabase,QSqlQuery
from PyQt4.QtGui import QTableView,QApplication
import sys
app = QApplication(sys.argv)
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("sqlitedb.rdb")
db.open()
projectModel = QSqlQueryModel()
projectModel.setQuery("select name,age from info",db)
projectView = QTableView()
projectView.setModel(projectModel)
projectView.show()
app.exec_()
您可以使用查询设置要显示的字段/列.
You can set which field/column you want to show using the query.
和,
如何从我选择的项目的不同列中检索信息?
how can I retrieve the information from different columns of the item I have selected?
您可以浏览查询结果并获取数据,例如,
You can navigate through the result of the query and have the data, example,
from PyQt4.QtSql import QSqlDatabase,QSqlQuery
from PyQt4.QtCore import QCoreApplication
import sys
app = QCoreApplication(sys.argv)
db = QSqlDatabase.addDatabase("QSQLITE")
db.setDatabaseName("sqlitedb.rdb")
db.open()
query = QSqlQuery()
query.exec_("select * from info")
while (query.next()):
id = query.value(0).toInt()
name = query.value(1).toString()
country = query.value(2).toString()
age = query.value(3).toInt()
print "name = %s \ncountry = %s" % (name,country)
app.exec_()
您可以遍历查询并访问数据.
you can loop through the query and get access to the data.
这篇关于从 QListView 中的选定项获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!