PyQt5 Quick Start (four) PyQt5 advanced window components

PyQt5 Quick Start (four) PyQt5 advanced window components

A, QTableView

1, QTableView Profile

QTableView from the data model may be used to display content defined by the data source setModel binding, the interface defined by the class QAbstractItemView achieved, it is possible to display data provided by the derived class QAbstractItemModel model.

2, the standard model

QStringListModel string list data model
QStandardItemModel standard model data items, any hierarchical structure of data stored
QDirModel file system directory model
QSqlQueryModel SQL query result set data model
QSqlTableModel SQL form data model
QSqlRelationTableModel relational SQL form data model
QSortFilterProxyModel sort filtering proxy model

3, QTableView examples

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTableView, QHBoxLayout
from PyQt5.QtGui import QStandardItemModel,QStandardItem

class MainWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.model = QStandardItemModel(4, 4)
        headers = ["column1", "column2", "column3", "column4"]
        self.model.setHorizontalHeaderLabels(headers)
        self.tableView = QTableView(self)
        # 最后一列拉伸
        self.tableView.horizontalHeader().setStretchLastSection(True)
        self.tableView.setModel(self.model)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.tableView)
        self.setLayout(self.layout)

    def initUI(self):
        # 数据项增加
        for row in range(10):
            for column in range(4):
                item = QStandardItem()
                item.setText(str(column))
                self.model.setItem(row, column, item)
        # 增加一行
        for row in range(5):
            items = []
            for column in range(4):
                items.append(QStandardItem(str(column)))
            self.model.appendRow(items)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.initUI()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

二, QTreeView

1, QTreeView Profile

QTreeView is a view of a tree structure, inherited from QAbstractItemView, is part of the Model / View framework.

2, QTreeView examples

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTreeView, QHBoxLayout
from PyQt5.QtGui import QStandardItemModel,QStandardItem

class MainWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.model = QStandardItemModel()
        self.treeView = QTreeView(self)
        self.treeView.setModel(self.model)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.treeView)
        self.setLayout(self.layout)

    def initUI(self):
        root = self.model.invisibleRootItem()
        for i in range(4):
            item = QStandardItem(str(i))
            for j in range(3):
                chidItem = QStandardItem(str(j))
                item.setChild(j, chidItem)
            root.setChild(i, item)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.initUI()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

Three, QListView

1, QListView Profile

QListView a list view, inherited from QAbstractItemView, headers and box is not displayed, provides a more flexible way for Model / View structure of Qt.

2, QListView examples

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QListView, QHBoxLayout
from PyQt5.QtGui import QStandardItemModel,QStandardItem

class MainWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.model = QStandardItemModel()
        self.listView = QListView(self)
        self.listView.setModel(self.model)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.listView)
        self.setLayout(self.layout)

    def initUI(self):
        root = self.model.invisibleRootItem()
        for i in range(10):
            item = QStandardItem(str(i))
            root.setChild(i, item)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.initUI()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

Four, QTableWidget

1, QTableWidget Profile

 QTableWidget table cell assembly inherited from QTableView, QTableView custom data can be used to display the content model, and you can only use QTableWidget standard data model, and which cell data object is achieved QTableWidgetItem, the table used to represent QTableWidgetItem in a cell, the entire table needs to build up a cell by cell.

2, QTableWidget examples

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QTableWidget, QTableWidgetItem
from PyQt5.QtCore import Qt

class MainWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.tableWidget = QTableWidget(self)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.tableWidget)
        self.setLayout(self.layout)

    def initUI(self):
        self.tableWidget.setRowCount(10)
        self.tableWidget.setColumnCount(5)
        for row in range(10):
            for column in range(5):
                item = QTableWidgetItem()
                # 设置数据项显示数据角色的数据
                item.setData(Qt.DisplayRole, column)
                self.tableWidget.setItem(row, column, item)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.initUI()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

Five, QTreeWidget

1, QTreeWidget Profile

QTreeWidget QTreeView tree inherits from class cell assembly, it can be used to create a simple tree structure list. Achieved through the tree structure and QTreeWidgetItem QTreeWidget classes classes, class implements QTreeWidgetItem added node.

2, QTreeWidget examples

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QTreeWidget, QTreeWidgetItem
from PyQt5.QtCore import Qt

class MainWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.treeWidget = QTreeWidget(self)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.treeWidget)
        self.setLayout(self.layout)

    def initUI(self):
        root = self.treeWidget.invisibleRootItem()
        for row in range(4):
            item = QTreeWidgetItem()
            item.setData(0, Qt.DisplayRole, row)
            root.addChild(item)
            # 设置数据项显示数据角色的数据
            root.addChild(item)
            for column in range(3):
                child = QTreeWidgetItem()
                child.setData(0, Qt.DisplayRole, column)
                item.addChild(child)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.initUI()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

Six, QListWidget

1, QListWidget Profile

QListWidget cell assembly inherited from QListView list, each item may display a list, the list is an example of QListWidgetItem, each item may be operated by QListWidgetItem. Image and text can be set for each item by QListWidgetItem.

2, QListWidget examples

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QListWidget, QListWidgetItem
from PyQt5.QtCore import Qt

class MainWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.listWidget = QListWidget(self)

        self.layout = QHBoxLayout()
        self.layout.addWidget(self.listWidget)
        self.setLayout(self.layout)

    def initUI(self):
        for i in range(10):
            item = QListWidgetItem()
            # 设置数据项显示数据角色的数据
            item.setData(Qt.DisplayRole, i)
            self.listWidget.addItem(item)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.initUI()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

Seven, QStackedWidget

1, QStackedWidget Profile

QStackedWidget container assembly is a stack, developers can use the stack management controls, only the top of the stack QStackedWidget display control function to use raiseWidget any other control stack top of the stack, thereby switching between the controls.

2, QStackedWidget examples

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QStackedWidget, QPushButton

class MainWidget(QWidget):
    currentIndex = 0
    N = 5

    def __init__(self, parent=None):
        super().__init__(parent)
        self.stackWidget = QStackedWidget(self)
        self.layout = QVBoxLayout()

        button = QPushButton("Next")
        button.clicked.connect(self.onNext)
        self.layout.addWidget(self.stackWidget)
        self.layout.addWidget(button)
        self.setLayout(self.layout)

    def initUI(self):
        for i in range(self.N):
            button = QPushButton()
            button.setText("Button {0}".format(i))
            self.stackWidget.addWidget(button)

    def onNext(self):
        self.currentIndex = self.currentIndex + 1
        self.currentIndex = self.currentIndex % self.N
        self.stackWidget.setCurrentIndex(self.currentIndex)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.initUI()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

Eight, QDockWidget

1, QDockWidget Profile

QDockWidget is docked form components can be used as a top-level window floating on the desktop, mainly as a secondary form appears in the interface. QDockWidget comprising a toolbar and a content region for displaying a toolbar window title, a floating button and a close button. QDockWidget child window can be as a package member by setWidget () provided child widgets.
PyQt5 Quick Start (four) PyQt5 advanced window components

2, QDockWidget examples

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDockWidget, QTextEdit
from PyQt5.QtCore import Qt

class MainWindow(QMainWindow):

    def __init__(self, parent=None):
        super().__init__(parent)
        # central widget
        self.centralWidget = QTextEdit()
        self.centralWidget.setText("Main Window")
        self.centralWidget.setAlignment(Qt.AlignCenter)
        self.setCentralWidget(self.centralWidget)
        # left dock
        dock = QDockWidget("Window1", self)
        dock.setFeatures(QDockWidget.DockWidgetMovable)
        dock.setAllowedAreas(Qt.LeftDockWidgetArea | Qt.RightDockWidgetArea)
        textEdit = QTextEdit()
        textEdit.setText("Window1,The dock widget can be moved between docks and users")
        dock.setWidget(textEdit)
        self.addDockWidget(Qt.LeftDockWidgetArea, dock)
        # right dock
        dock =QDockWidget("Window2", self)
        dock.setFeatures(QDockWidget.DockWidgetClosable | QDockWidget.DockWidgetFloatable)
        textEdit =QTextEdit()
        textEdit.setText("Window2,The dock widget can be detac from the main window,""and float as an independent window,and can be closed")
        dock.setWidget(textEdit)
        self.addDockWidget(Qt.RightDockWidgetArea, dock)
        # right dock
        dock =QDockWidget("Window3", self)
        dock.setFeatures(QDockWidget.AllDockWidgetFeatures)
        textEdit = QTextEdit()
        textEdit.setText("Window3,The dock widget can be closed,moved,and float")
        dock.setWidget(textEdit)
        self.addDockWidget(Qt.RightDockWidgetArea, dock)

        self.setWindowTitle("DockWidget Demo")
        self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

PyQt5 Quick Start (four) PyQt5 advanced window components

Nine, QMidArea

1, QMidArea Profile

QMdiArea component is a multi-document interface, MDI namely Multiple Document Interface, mainly applied to the need to use multiple files to complete a task. QMainWindow is SDI (Signal Document Interface, Single Document Interface), open each file occupies a window, not much is mainly applied to file application scenarios.
    QMdiSubWindow class inherits from QWidget, mainly used to create the MDI child form instances.

2, QMidArea examples

import sys
from PyQt5.QtWidgets import QApplication, QMdiArea, QMdiSubWindow

class MainWindow(QMdiArea):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.addSubWindow(QMdiSubWindow())
        self.addSubWindow(QMdiSubWindow())
        self.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

Ten, QScrollArea

1, QScrollArea Profile

QScrollArea scroll area component used to display the contents of the sub-frame control, if the size of a child control over the size of the frame, you can use the scroll bar, easy to view the entire child controls. QScrollArea can give any QWidget add a scroll bar, but generally the custom form to add a scroll bar is not displayed.

2, QScrollArea examples

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QScrollArea, QPushButton, QTableView

class MainWidget(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.layout = QVBoxLayout()
        # 滚动区创建
        self.scrollArea = QScrollArea()
        # 容器组件
        widget  = QWidget()
        layout = QVBoxLayout()
        tableView = QTableView()
        button = QPushButton("OK")
        layout.addWidget(tableView)
        layout.addWidget(button)
        widget.setLayout(layout)
        # 设置滚动区的容器组件
        self.scrollArea.setWidget(widget)
        self.layout.addWidget(self.scrollArea)
        self.setLayout(self.layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWidget()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())

Guess you like

Origin blog.51cto.com/9291927/2423254