简体   繁体   English

如何将我的数据库插入我的QTableWidget表?

[英]How to insert my database into my QTableWidget table?

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. 此代码生成一个包含20行和3列的表,每个表中的数据都会通知我它的位置。 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? 这将使用sqlite 3.如何在此处插入数据库并正确连接?

Well a better solution to solve this problem would be by using Model View Programming . 那么解决这个问题的更好的解决方案是使用模型视图编程。 Here is a solution using model view. 这是使用模型视图的解决方案。

class MyTableModel(QtCore.QAbstractTableModel):
    def __init__(self,dbDir,parent=None):
        QtCore.QabstractTableModel.__init__(self,parent)
       dbConnection = sqlite3.connect(dbDir,isolation_level=None)
        cursor=dbConnection.cursor()
        dbConnection.execute("""SELECT * FROM tablename""")
        data=cursor.fetchall()
        data =[[]]
        count1=0
            for i in data:
            count2 = 0
            for x in i:
                data[count1][count2] =x
                count2 +=1

        self.__data=data
        self.__header=[" First "," Second "," Thirdt " ]

    def rowCount( self, parent ):
        return len(self.__data)

    def columnCount( self , parent ):
        return len(self.__data[0])

    def flags( self,index):
        return QtCore.Qt.ItemIsEnabled |QtCore.Qt.ItemIsSelectable

    def data ( self , index , role ):
        if role == QtCore.Qt.DisplayRole:
            row = index.row()
            column = index.column()
            value = self.__data[row][column]
            return value.name()

    def headerData(self , section , orientation , role):
        if role == QtCore.Qt.DisplayRole:
            if orientation == QtCore.Qt.Horizontal:
                return self.__header[section]

class Table(QtGui.QDialog):
 def __init__(self, parent=None):
    super(Table, self).__init__(parent)
    layout = QtGui.QGridLayout() 
    self.MyTableMModel = MyTableModel(table_directory) # pass directory in which table exists
    self.table = QtGui.QTableView()
    self.table.setModel(self.MyTableModel)
    layout.addWidget(self.table)
    self.setLayout(layout)

Qt has ready-to-use tablemodels that connect to a database. Qt具有连接到数据库的现成的表模型。

Check 校验

http://pyqt.sourceforge.net/Docs/PyQt4/qsqltablemodel.html 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. 和PyQt安装中的site-packages\\PyQt4\\examples\\sql (它在我的机器上安装的位置)文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM