赞
踩
QTableView可以使用自定义的数据模型来显示内容,通过setModel绑定数据源,由QAbstractItemView类定义的接口来实现,使其能够显示由QAbstractItemModel类派生的模型提供的数据。
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框架的一部分。
- 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类实现结点的添加。
- 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函数把栈中任何其他控件移到栈顶,从而实现控件之间的切换。
- 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()设置子窗口部件。
- 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_())
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。