我创建了一个管理数据库中表格的程序,现在我正试图为它做一个gui,我认为最简单的方法是用Qt Designer来做,然后将它转换为python with PyQt5,所以我做了,我知道如何将函数绑定到按钮,但我不知道更复杂的事情,比如在Qtablewidget中显示表列和行,假设我有数据库:
import sqlite3
conn = sqlite3.connect('realscheduler.db')
c = conn.cursor()
c.execute('pragma foreign_keys = ON;')
conn.commit()
有一个名为教授的表,其中有6列,我该如何在QtableWidget上显示这个表,它的列和行?
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'testui.ui'
#
# Created by: PyQt5 UI code generator 5.4.1
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.verticalLayout = QtWidgets.QVBoxLayout()
self.verticalLayout.setObjectName("verticalLayout")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setObjectName("tabWidget")
self.tab = QtWidgets.QWidget()
self.tab.setObjectName("tab")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.tab)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.tableWidget = QtWidgets.QTableWidget(self.tab)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.setColumnCount(0)
self.tableWidget.setRowCount(0)
self.verticalLayout_2.addWidget(self.tableWidget)
self.tabWidget.addTab(self.tab, "")
self.tab_2 = QtWidgets.QWidget()
self.tab_2.setObjectName("tab_2")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.tab_2)
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.tableWidget_2 = QtWidgets.QTableWidget(self.tab_2)
self.tableWidget_2.setObjectName("tableWidget_2")
self.tableWidget_2.setColumnCount(0)
self.tableWidget_2.setRowCount(0)
self.horizontalLayout_2.addWidget(self.tableWidget_2)
self.tabWidget.addTab(self.tab_2, "")
self.verticalLayout.addWidget(self.tabWidget)
self.horizontalLayout.addLayout(self.verticalLayout)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), _translate("MainWindow", "Tab 1"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), _translate("MainWindow", "Tab 2"))
答案 0 :(得分:2)
在回答这个问题之前,我强烈建议您在Qt Designer中打开您的UI文件,并将您的对象名称从tableWidget_2
和pushButton
更改为更合适的内容,因为您要自己开车疯。
其次,PyQt提供了一个完整的API来处理名为QtSql的数据库。你可以这样访问api:
from PyQt5.QtSql import (QSqlDatabase, QSqlQuery) #and so on.
Rant over。我现在回答你的问题。
QTableWidgets非常不稳定,需要处理几件事情:
要将数据插入表中,您可以执行以下操作。
def add_values(self):
self.count = self.count + 1 # this is incrementing counter
self.tableWidget_2.insertRow(self.count)
self.tableWidget_2.setRowCount(self.count)
# these are the items in the database
item = [column1, column2, column3]
# here we are adding 3 columns from the db table to the tablewidget
for i in range(3):
self.tableWidget_2.setItem(self.count - 1, i, QTableWidgetItem(item[i]))
但是,如果您只想将数据加载到QTableWidget中,您可以执行以下操作并在设置中调用它:
def load_initial_data(self):
# where c is the cursor
self.c.execute('''SELECT * FROM table ''')
rows = self.c.fetchall()
for row in rows:
inx = rows.index(row)
self.tableWidget_2.insertRow(inx)
# add more if there is more columns in the database.
self.tableWidget_2.setItem(inx, 0, QTableWidgetItem(row[1]))
self.tableWidget_2.setItem(inx, 1, QTableWidgetItem(row[2]))
self.tableWidget_2.setItem(inx, 2, QTableWidgetItem(row[3]))