QTableView knocks Tkinter off the ‘table’

Capture

Hallelujah!

Have I got a deal for you! How about a GUI widget with over double the functionality, with less than half the code? Here it is:

import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtSql import *

# My function for assembling SQL queries for my 'transactions' table. I'll share the code another time.
from dq_sql import *

app = QApplication(sys.argv)

class DqTableForm(QWidget):
    def __init__(self, parent = None):
        super(DqTableForm,self).__init__(parent)

        self.setWindowTitle('Transactions')
        self.setMinimumWidth(950)
        self.setMinimumHeight(300)

        db = QSqlDatabase.addDatabase("QSQLITE")
        db.setDatabaseName("dataq.db")
        db.open()

        self.DqModel = QSqlQueryModel()

# My TransQry function takes parameters and returns a SQL query using the parameters as criteria for filtering my 'transactions' table.      

self.DqModel.setQuery(TransQry("2014-03-01","2014-03-31","DESC","","","202",""), db)

        self.DqTable = QTableView()
        self.DqTable.setModel(self.DqModel)
        self.DqTable.hideColumn(0)
        self.DqTable.resizeColumnsToContents()
        self.DqTable.resizeRowsToContents()

        grid = QGridLayout()
        grid.addWidget(self.DqTable,0,0)
        self.setLayout(grid)

form = DqTableForm()
form.show()
app.exec_()

See? Less than 45 lines. Sure beats the nearly 100-line, marginally useful alternative, which I have been sweating and struggling under for months. Lesson learned: there’s a very fuzzy boundary between challenging myself toward productive learning, and finding the most efficient solution to a need. In this instance, given the complex data filtering and sorting I’m trying to accomplish, Qt’s QTableView soundly beats anything Tkinter is even remotely capable of without astronomically more code. Shout out to Bo’s Qt GUI tutorials for introducing me to Qt, the documentation for the PySide Python Qt bindings for helping me figure out QTableView, and to my friend and constant companion Google, for helping me evolve.