PyQt (Python+Qt) Learning Notes: Two standard models in the model/view architecture, QStandardItemModel and QFileSystemModel

1. Standard Model in PyQt

PyQt and Qt provide two standard models, QStandardItemModel and QFileSystemModel.QStandardItemModel is a multipurpose model that can be used to represent various data structures required for list, table, and tree type views, and can hold data items.QFileSystemModel is a model that maintains information about the contents of file directories. It does not contain any data items, but simply represents files and directories on the local file system.

QFileSystemModel provides a ready-to-use model for experimentation and can easily be configured to use existing data.Using this model, we can demonstrate how to set up models for ready-made views and explore how to manipulate data using model indexes.QListView and QTreeView are the best views to use with QFileSystemModel.

2. QFileSystemModel Use Cases

An instance of QFileSystemModel is created in the application, QListView and QTreeView are designed and created in the graphical interface (view names are listView and treeView respectively), and the corresponding model of the view is set to the QFileSystemModel instance to display the contents of the catalog.

1. GUI Design

2. Define models in interface derived classes and associate models with views

class w_mainWin(ui_fileView.Ui_fileViewMainWin,QtWidgets.QWidget):
    def __init__(self):
        super(w_mainWin, self).__init__()
        self.model = QtWidgets.QFileSystemModel(self)
        self.model.setRootPath('c:\\') #QtCore.QDir.currentPath())
        self.listView.setRootIndex(self.model.index('c:\\')) #QtCore.QDir.currentPath()))
        self.treeView.setRootIndex(self.model.index('c:\\')) #(QtCore.QDir.currentPath()))

The code first initializes the interface class, then defines the model, points the model to the corresponding directory, binds the view to the model, and sets the current display directory to match the model monitoring directory.

3. Apply main program code

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    W = w_mainWin()

4. Screenshot of interface operation

3. QStandardItemModel Use Cases

1. Case Description

The application displays the file names and icons of the icon files in the specified directory, and the interface is a simple window with a list view, listView, displayed in the view.

2. Define models in interface derived classes and associate models with views

class mainWindow( QtWidgets.QWidget,ui_listView.Ui_mainWin):
    def  __init__(self):
        super(mainWindow, self).__init__()
        self.model = QStandardItemModel()

        ICon1 = QStandardItem(QIcon(r"F:\Study\python\Resources\image file\add.png"),'add.png')
        ICon2 = QStandardItem(QIcon(r"F:\Study\python\Resources\image file\application_windows_add.png"), 'application_windows_add.png')
        ICon3 = QStandardItem(QIcon(r"F:\Study\python\Resources\image file\save.png"), 'save.png')
        ICon4 = QStandardItem(QIcon(r"F:\Study\python\Resources\image file\search.png"), 'search.png')
        ICon5 = QStandardItem(QIcon(r"F:\Study\python\Resources\image file\stop.gif"), 'stop.gif')


3. Screenshot of running interface

Ape Python, learn Python from Ape!

574 original articles published, 3397 awarded, 300,000 visits+
His message board follow

Keywords: Python Qt

Added by RobOgden on Sun, 19 Jan 2020 04:00:42 +0200