2

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

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

1 Answers1

3

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.

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 are quite precarious to work with and require a couple of things:

  1. A Counter Flag
  2. Row Count
  3. Column Count
  4. Data (Obviously)

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

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

enter image description here

ham-sandwich
  • 3,975
  • 10
  • 34
  • 46
  • Been trying to make this work for an hour but haven't been able to, it doesn't display the gui and throws no error, could you give futher details? I'm really new to this so my level of knowledge is pretty low.. –  May 03 '15 at 20:50
  • @RichardD'Freitas. Sure. No problem. It may take me 10 minutes to provide an example though ~ I will update code now. – ham-sandwich May 03 '15 at 20:54
  • Ok, I made an example and then ran it using your generated UI code. It didn't work because you have not created any columns or rows. Go back to the Qt Designer and and create columns by double clicking on the tableWidget. You code says `self.tableWidget_2.setColumnCount(0)` which means you have no columns to display in the table! – ham-sandwich May 03 '15 at 21:11
  • Ohh, i didn't know i have to do that! I thought it would just take up all of the data in the table, columns, and rows and everything, will do that right now and get back at you! –  May 03 '15 at 21:14
  • I'm signing off for the night. however, I have played about and did some experimentation. See this gist for completed code: https://gist.github.com/pjhampton/9d3bbf95eca47d219145. Also see image. – ham-sandwich May 03 '15 at 21:19
  • However, I recommend you follow my implementation from an earlier question as this one file spaghetti architecture is doomed for failure and to confuse you further. – ham-sandwich May 03 '15 at 21:21
  • alright mate, have a goodnight! thank you so much for the help, i'll leave the details of how it went later on.. Sleep well! thanks again –  May 03 '15 at 21:21