sqlite3 表转换成 QTableWidget、sqlite3、PyQt5

2022-01-12 00:00:00 python-3.x pyqt5 sqlite

我已经创建了一个管理数据库中表的程序,现在我正在尝试为它制作一个 gui,我认为最简单的方法是使用 Qt Designer 来完成,然后使用 PyQt5 将其转换为 python,所以我做到了,我知道如何将函数绑定到按钮,但我不知道更复杂的事情,比如将表列和行显示到 Qtablewidget 中,假设我有下面的数据库:

I have created a program that manages tables in a database, now i'm trying to make a gui for it, and i figured the easiest way would be to do it with Qt Designer and then convert it to python with PyQt5, and so i did it, i know how to bind functions to buttons, but i don't know more complicated things like displaying table columns and rows into a Qtablewidget, suppose i have the database bellow:

import sqlite3
conn = sqlite3.connect('realscheduler.db')
c = conn.cursor()
c.execute('pragma foreign_keys = ON;')
conn.commit()

有一个名为professors的表,其中有6列,我该怎么做才能在下面的QtableWidget上显示这个表、它的列和它的行?

With a table named professors, with 6 columns in it, what do i do to display this table, its columns and its rows on the QtableWidget bellow?

# -*- 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"))

推荐答案

在我回答这个问题之前,我强烈建议你在 Qt Designer 中打开你的 UI 文件并从 tableWidget_2 更改你的对象名称和 pushButton 到更合适的东西,因为你会让自己发疯.

Before I answer this question, I highly recommend you open up your UI file in the Qt Designer and change your object names from tableWidget_2 and pushButton to something more appropriate as you are going to drive yourself insane.

其次,PyQt 提供了一个完整的 API 来处理名为 QtSql 的数据库.您可以像这样访问 api:

Second of all, PyQt provides an entire API to work with databases called QtSql. You can access the api like so:

from PyQt5.QtSql import (QSqlDatabase, QSqlQuery) #and so on.

大吵大闹.我现在就回答你的问题.

Rant over. I will answer your question now.

QTableWidgets 使用起来非常不稳定,需要做一些事情:

QTableWidgets are quite precarious to work with and require a couple of things:

  1. 反旗
  2. 行数
  3. 列数
  4. 数据(显然)

要将数据插入表中,您可以执行以下操作.

To insert data into the table you could do something like this.

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 中,您可以执行以下操作并在设置中调用它:

However, if you just want to load the data into the QTableWidget you could do something like this and call it in the setup:

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]))

相关文章