QTableView可使用自定義的數據模型來顯示內容,經過setModel綁定數據源,由QAbstractItemView類定義的接口來實現,使其可以顯示由QAbstractItemModel類派生的模型提供的數據。app
QStringListModel 字符串鏈表數據模型
QStandardItemModel標準數據項模型,存儲任意結構層次的數據
QDirModel 文件系統目錄模型
QSqlQueryModel SQL查詢結果集數據模型
QSqlTableModel SQL表格數據模型
QSqlRelationTableModel 關係型SQL表格數據模型
QSortFilterProxyModel 排序過濾代理模型框架
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是一種樹形結構的視圖,繼承自QAbstractItemView,是Model/View框架的一部分。ide
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_())
QListView是列表視圖,繼承自QAbstractItemView,不顯示錶頭和表框,爲Qt的Model/View結構提供了更靈活的方式。函數
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_())
QTableWidget表格單元組件繼承自QTableView,QTableView可使用自定義的數據模型來顯示內容,而QTableWidget則只能使用標準的數據模型,而且其單元格數據是QTableWidgetItem的對象來實現的,QTableWidgetItem用來表示表格中的一個單元格,整個表格都須要用逐個單元格構建起來。工具
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_())
QTreeWidget樹形單元組件繼承自QTreeView類,能夠用來來建立簡單地樹形結構列表。經過QTreeWidget類和QTreeWidgetItem類實現樹形結構,QTreeWidgetItem類實現結點的添加。ui
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_())
QListWidget列表單元組件繼承自QListView,能夠顯示一個列表,列表中的每一個項是QListWidgetItem的一個實例,每一個項能夠經過QListWidgetItem來操做。能夠經過QListWidgetItem來設置每一個項的圖像與文字。代理
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_())
QStackedWidget是棧式容器組件,開發人員可使用棧管理控件,QStackedWidget只顯示棧頂的控件,使用raiseWidget函數把棧中任何其餘控件移到棧頂,從而實現控件之間的切換。code
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_())
QDockWidget是停靠窗體組件,能夠做爲一個頂層窗口漂浮在桌面,主要做爲輔助窗體出如今界面中。QDockWidget包含工具欄和內容區域,工具欄用於顯示窗口標題,一個浮動按鈕和一個關閉按鈕。QDockWidget能夠做爲子窗口部件的封裝,經過setWidget()設置子窗口部件。
orm
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_())
QMdiArea組件是一種多文檔界面,MDI即Multiple Document Interface,主要適用於完成一項工做時須要用到多個文件。QMainWindow是SDI(Signal Document Interface,單文檔界面),每一個開啓的文件佔據一個視窗,主要適用於沒有太多文件的應用場景。
QMdiSubWindow類繼承自QWidget,主要用來建立MDI子窗體實例。對象
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_())
QScrollArea滾動區組件用來顯示子控件的內容的框架,若是子控件的尺寸超過了框架的大小,可使用滾動條,方便查看整個子控件。QScrollArea能夠給任何QWidget添加滾動條,但通常自定義窗體添加滾動條不顯示。
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_())