Display TABLE with QTableWidget

Heads up! You've already completed this tutorial.

provopoulos3720 | 2020-05-22 00:27:12 UTC | #1

Greetings here, I'm learning QT for Python to use for a University project as well as some personal endeavors. I'm using a cursor to query data from various tables and here's what I've got:

python
cursor = cnx.cursor()
query = ("SELECT * FROM Flight")
cursor.execute(query)
records = cursor.fetchall()

If I try to print(records) I receive the following list: [('GR001', 'ZTH', datetime.datetime(2020, 8, 9, 9, 25), 'PRG', datetime.datetime(2020, 8, 9, 10, 55), 'SX-DNH'), ('GR002', 'PRG', datetime.datetime(2020, 8, 9, 18, 30), 'CFU', datetime.datetime(2020, 8, 9, 21, 40), 'SX-DNH'), ('GR003', 'CFU', datetime.datetime(2020, 7, 12, 9, 0), 'EFL', datetime.datetime(2020, 7, 12, 9, 20), 'SX-DGA'), ('GR004', 'CFU', datetime.datetime(2020, 8, 15, 11, 30), 'ZTH', datetime.datetime(2020, 8, 15, 12, 0), 'SX-DGF'), ('GR005', 'EFL', datetime.datetime(2020, 8, 20, 18, 5), 'ZTH', datetime.datetime(2020, 8, 20, 18, 30), 'SX-DVI'), ('GR006', 'EFL', datetime.datetime(2020, 9, 2, 14, 30), 'CFU', datetime.datetime(2020, 9, 2, 14, 50), 'SX-DNF'), ('GR007', 'ZTH', datetime.datetime(2020, 10, 7, 13, 40), 'EFL', datetime.datetime(2020, 10, 7, 14, 5), 'SX-DGA'), ('GR008', 'ZTH', datetime.datetime(2020, 4, 21, 15, 5), 'CFU', datetime.datetime(2020, 4, 21, 15, 35), 'SX-DVI'), ('GR009', 'EFL', datetime.datetime(2020, 7, 22, 14, 30), 'CFU', datetime.datetime(2020, 7, 22, 14, 50), 'SX-DNF')]

I want to display those results with QTableWidget/QTableView or whichever widget would work best for my case. I'll have to do this multiple times for different tables each time. Closest solution I've found for this problem is this SO question: https://stackoverflow.com/questions/54691624/load-qtablewidget-with-a-list

What I want to achieve is preferably a button inside MainWindow that once pushed, the queried data will be displayed. Here's how my PyQT5 code is structured:

python
class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowIcon(QIcon('ico.png'))
        self.setFixedSize(800, 600)

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    login = Login()
    if login.exec_() == QtWidgets.QDialog.Accepted:
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())

Any help would be greatly appreciated!


Luca | 2020-05-26 00:53:50 UTC | #2

Read these two tutorials: https://www.pythonguis.com/courses/model-views/modelview-architecture/ https://www.pythonguis.com/courses/model-views/qtableview-modelviews-numpy-pandas/


Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book

Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

Display TABLE with QTableWidget was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.