PyQt

Table of Contents

1 Modules

  • QtCore
  • QtWidgets
  • QtGui

2 Dialogs

2.1 Dumb Dialogs

We define a “dumb” dialog to be a dialog whose widgets are set to their initial values by the dialog’s caller, and whose final values are obtained directly from the widgets, again by the dialog’s caller.

Dumb dialogs are normally modal dialogs with an “accept” button (e.g., OK ) and a “reject” button (e.g., Cancel ).

dialog = FakeDlg(self.values_to_set, self)
if dialog.exec_():
    self.values = dialog.someWidget.value()

2.2 Standard Dialogs

We consider a dialog to be a “standard” dialog if it initializes its widgets in accordance with the values set through its initializer or through its methods, and whose final values are obtained by method calls or from instance variables—not directly from the dialog’s widgets.

dialog = FakeDlg(self.values_to_set, self)
if dialog.exec_():
    self.values = dialog.get_values()

2.3 Smart Dialogs

We define a “smart” dialog to be one that initializes its widgets in accordance with data references or data structures that are passed to its initializer, and which is capable of updating the data directly in response to user interaction.

dialog = FakeDlg(self.values_to_set, self)
self.connect(dialog, SIGNAL("changed"), self.refreshSomeWidgets)
dialog.show()

2.3.1 Live

if self.dialog is None:
    self.dialog = FakeDlg(
	self.values_to_set, self.refreshSomeWidgets, self)
self.dialog.show()
self.dialog.raise_()
self.dialog.activateWindow()

3 Model

3.1 QtCore.QModelIndex

Every data item in the model can be identified by a unique QModelIndex. Each model index has three important attributes: a row, a column, and a parent.

3.2 Model Classification

  • List(1D): Lists are just tables with a single column.
  • Table(2D)
  • Trees(Hierarchical)

3.2.1 QAbstractListModel

  • APIs for read-only
    • data(index: QModelIndex, role: int)
    • rowCount(parent: QModelIndex)
  • APIs for editable
    • setData
    • flags

3.2.2 QAbstractTableModel

  • APIs for read-only
    • data(index: QModelIndex, role: int)
    • rowCount(parent: QModelIndex)
    • columnCount(parent: QModelIndex)
    • headerData(section: int, orientation: Qt.Orientation, role: int) (recommended)
  • APIs for editable
    • flags(index: QModelIndex) -> QtCore.Qt.ItemFlags

      # example
      def flags(self, index):
          return QtCore.Qt.ItemFlags(super.flags(index) | QtCore.Qt.ItemIsEditable)
      
    • setData(index: QtCore.QModelIndex, value, role: int = Qt.EditRole) -> bool: is called when the user completes an edit
    • insertRow(row: int, parent: QModelIndex)
    • insertRows(row: int, count: int, parent: QModelIndex)
      • beginInsertRows(parent, first, last), endInsertRows(parent, first, last): reference
    • removeRow(row: int, parent: QModelIndex)
    • removeRows(row: int, count: int, parent: QModelIndex)
      • beginRemoveRows, endRemoveRows
  • Signals
    • dataChanged[topleft_index, bottomright_index]
    • modelReset()

3.2.3 QAbstractTreeModel

3.2.4 QListWidget, QTableWidget, and QTreeWidget

are views with models and delegates aggregated inside them.

3.2.5 Pure Views

QListView, QTableView, and QTreeView

3.3 Built-in Models

  • QStringListModel
  • QDirModel
  • QSqlTableModel

4 Delegate

4.1 APIs for read-only

  • paint(painter, option, index)

4.2 APIs for editable

  • createEditor()
  • setEditorData()
  • setModelData()
  • commitAndCloseEditor() (recommended if we use QLineEdit or QTextEdit for editing)
  • sizeHint() (sometimes)