所以,我有一个mysql数据库patients,其中是一个客户机表。我需要在表中的GUI窗口中显示有关客户机的信息。此外,我还需要一个按主字段筛选,以获得更详细的信息(分别是姓氏、姓名、一系列文档、一系列策略等),以便在多个字段中同时搜索。我从下面的代码开始。
问题1是:在这个任务中什么是容易使用的-QSqlTableModel还是QTableWidget?
第二个问题是:如何按主字段过滤?什么意思?
我很乐意对这个问题有任何想法。在from PyQt4.QtGui import *
from PyQt4.QtSql import *
import sys
def main_():
app = QApplication(sys.argv)
table = QTableWidget()
db = QSqlDatabase.addDatabase("QMYSQL")
db.setHostName("127.0.0.1")
db.setDatabaseName("patients")
db.setUserName("root")
db.setPassword("")
table_name = "client"
if (db.open()==False):
QMessageBox.critical(None, "Database Error",
db.lastError().text())
query = QSqlQuery ("select * from "+ table_name + " limit 10")
table.setColumnCount(query.record().count())
table.setRowCount(query.size())
index=0
while (query.next()):
table.setItem(index,0,QTableWidgetItem(query.value(0).toString()))
table.setItem(index,1,QTableWidgetItem(query.value(1).toString()))
index += 1
table.show()
return app.exec_()
if __name__ == '__main__':
main_()