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(MainWindow, self).__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

Create GUI Applications with Python & Qt5 by Martin Fitzpatrick — (PyQt5 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

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


The complete guide to packaging Python GUI applications with PyInstaller.
[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]
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.