如何将数据库插入QTableWidget表中? [英] How to insert my database into my QTableWidget table?
问题描述
class Table(QtGui.QDialog):
def __init __(self,parent = None):
super(Table,self).__ init __(parent)
布局= QtGui.QGridLayout()
self.table = QtGui.QTableWidget()
self.table.setRowCount(20)
self.table.setColumnCount(3 )
layout.addWidget(self.table)
self.enterDataInTable()
self.setLayout(layout)
def enterDataInTable (self):
用于范围(0,20)中的行:
用于范围在(0,3)中的列:
self.table.setItem(row,column,QtGui.QTableWidgetItem( cell%s-%s%(row + 1,column + 1)))
此代码产生一个包含20行3列的表,每个表中的数据告诉我它的位置。我想改用数据库的列标题和行标题,包括其中的信息。这将使用sqlite3。我如何能够在此处插入数据库并正确连接它?
Qt已经准备就绪可以使用的连接数据库的表模型。
检查
http://pyqt.sourceforge.net/Docs/PyQt4/qsqltablemodel.html
PyQt安装中的
和 site-packages\PyQt4\examples\sql
(在我的计算机上安装的位置)文件夹。 / p>
class Table(QtGui.QDialog):
def __init__(self, parent=None):
super(Table, self).__init__(parent)
layout = QtGui.QGridLayout()
self.table = QtGui.QTableWidget()
self.table.setRowCount(20)
self.table.setColumnCount(3)
layout.addWidget(self.table)
self.enterDataInTable()
self.setLayout(layout)
def enterDataInTable(self):
for row in range(0,20):
for column in range(0,3):
self.table.setItem(row, column, QtGui.QTableWidgetItem("cell %s-%s"%(row+1,column+1)))
This code produces a table with 20 rows and 3 columns, the data within each one informs me of its location. I want to instead have my database column and row titles, including the information inside them. This will be using sqlite 3. How would I be able to insert the database here and connect it appropriately?
Qt has ready-to-use tablemodels that connect to a database.
Check
http://pyqt.sourceforge.net/Docs/PyQt4/qsqltablemodel.html
and the site-packages\PyQt4\examples\sql
(that's where it's installed on my machine) folder in your PyQt installation.
这篇关于如何将数据库插入QTableWidget表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!