WiseOwl Training - Established 1992 Wise Owl Training

Established May 1992
30 years in business
Wise Owl Training
30 years in business
See 529 reviews for our classroom and online training
Should you be using QT for Python as a GUI?
Part eight of an eight-part series of blogs

If you need to build a GUI system in Python, PyQt5 is probably the best choice, but don't take our word for it - read this blog and see what you think.

  1. Should you be using PyQt?
  2. QT Designer
  3. Widgets
  4. Layouts
  5. Forms and code
  6. Slots and Signals
  7. CSS and Formatting
  8. Models and Data (this blog)

Posted by Andy Brown on 21 June 2022

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Models and Data

To attach data to a widget, create a model and then attach the model to the widget.  For our example, we'll do this with a combo box:

Combo box

When we run our form, the combo box will list out the Wise Owls.

 

Assuming that the combo box above is called cmbOwl, here's a way to populate it:

from PyQt5.QtWidgets import *

from PyQt5 import uic, QtSql # type: ignore

class MainWindow(QMainWindow):

# called when create new instance of our class based on a QT main window

def __init__(self):

# calls constructor for base class

super().__init__()

# get Python for form from QT Designer generated UI file

uic.loadUi("owl_form.ui", self)

# set this combo box

self.cmbOwl: QComboBox = self.findChild(QComboBox,"cmbOwl")

# open a database

self.db = QtSql.QSqlDatabase.addDatabase("QODBC3")

self.db.setDatabaseName(

"DRIVER={SQL Server};SERVER=.<<Server name here>>;DATABASE=<<Database name here>>;Trusted_Connection=yes;")

self.db.open()

# create a new model upon which to base combo box

self.model1 = QtSql.QSqlQueryModel()

# set the query to run against the database

sql_query = "SELECT PersonId, PersonName FROM vwTempOwl"

# apply this query

self.model1.setQuery(sql_query, self.db)

self.cmbOwl.setModel(self.model1)

self.cmbOwl.setModelColumn(1)

# set width to longest entry

width = self.cmbOwl.minimumSizeHint().width()

self.cmbOwl.view().setMinimumWidth(width)

# create a QT5 app, add a visible window to it and start event loop

app = QApplication([])

window = MainWindow()

window.show()

What this shows is the following instructions:

  1. Open a database, setting the connection string.
  2. Create a new data model.
  3. Tell the data model which SQL query it should run, and against which database.
  4. Tell the combo box which data model it should use.
  5. Tell the combo box to display the second column from the query.
  6. Set the combo box's minimum width to be the width of the longest entry.

When you run this program you should see a list of the Wise Owls appearing:

List of owls

The combo box is linked to the given query in the given database.

 

There's obviously a lot more to working with SQL Server data in QT, but this gives a flavour of how it works.

This blog has 0 threads Add post