How to insert my database into my QTableWidget tab

2019-08-31 07:05发布

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?

2条回答
forever°为你锁心
2楼-- · 2019-08-31 07:17

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.

查看更多
Lonely孤独者°
3楼-- · 2019-08-31 07:23

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)
查看更多
登录 后发表回答