QTableView可以使用自定義的數(shù)據(jù)模型來(lái)顯示內(nèi)容,通過(guò)setModel綁定數(shù)據(jù)源,由QAbstractItemView類(lèi)定義的接口來(lái)實(shí)現(xiàn),使其能夠顯示由QAbstractItemModel類(lèi)派生的模型提供的數(shù)據(jù)。
企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對(duì)外擴(kuò)展宣傳的重要窗口,一個(gè)合格的網(wǎng)站不僅僅能為公司帶來(lái)巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺(tái),創(chuàng)新互聯(lián)公司面向各種領(lǐng)域:混凝土泵車(chē)等成都網(wǎng)站設(shè)計(jì)、成都全網(wǎng)營(yíng)銷(xiāo)解決方案、網(wǎng)站設(shè)計(jì)等建站排名服務(wù)。
QStringListModel 字符串鏈表數(shù)據(jù)模型
QStandardItemModel標(biāo)準(zhǔn)數(shù)據(jù)項(xiàng)模型,存儲(chǔ)任意結(jié)構(gòu)層次的數(shù)據(jù)
QDirModel 文件系統(tǒng)目錄模型
QSqlQueryModel SQL查詢(xún)結(jié)果集數(shù)據(jù)模型
QSqlTableModel SQL表格數(shù)據(jù)模型
QSqlRelationTableModel 關(guān)系型SQL表格數(shù)據(jù)模型
QSortFilterProxyModel 排序過(guò)濾代理模型
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):
# 數(shù)據(jù)項(xiàng)增加
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是一種樹(shù)形結(jié)構(gòu)的視圖,繼承自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結(jié)構(gòu)提供了更靈活的方式。
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可以使用自定義的數(shù)據(jù)模型來(lái)顯示內(nèi)容,而QTableWidget則只能使用標(biāo)準(zhǔn)的數(shù)據(jù)模型,并且其單元格數(shù)據(jù)是QTableWidgetItem的對(duì)象來(lái)實(shí)現(xiàn)的,QTableWidgetItem用來(lái)表示表格中的一個(gè)單元格,整個(gè)表格都需要用逐個(gè)單元格構(gòu)建起來(lái)。
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()
# 設(shè)置數(shù)據(jù)項(xiàng)顯示數(shù)據(jù)角色的數(shù)據(jù)
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樹(shù)形單元組件繼承自QTreeView類(lèi),可以用來(lái)來(lái)創(chuàng)建簡(jiǎn)單地樹(shù)形結(jié)構(gòu)列表。通過(guò)QTreeWidget類(lèi)和QTreeWidgetItem類(lèi)實(shí)現(xiàn)樹(shù)形結(jié)構(gòu),QTreeWidgetItem類(lèi)實(shí)現(xiàn)結(jié)點(diǎn)的添加。
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)
# 設(shè)置數(shù)據(jù)項(xiàng)顯示數(shù)據(jù)角色的數(shù)據(jù)
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,可以顯示一個(gè)列表,列表中的每個(gè)項(xiàng)是QListWidgetItem的一個(gè)實(shí)例,每個(gè)項(xiàng)可以通過(guò)QListWidgetItem來(lái)操作??梢酝ㄟ^(guò)QListWidgetItem來(lái)設(shè)置每個(gè)項(xiàng)的圖像與文字。
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()
# 設(shè)置數(shù)據(jù)項(xiàng)顯示數(shù)據(jù)角色的數(shù)據(jù)
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是棧式容器組件,開(kāi)發(fā)人員可以使用棧管理控件,QStackedWidget只顯示棧頂?shù)目丶?,使用raiseWidget函數(shù)把棧中任何其他控件移到棧頂,從而實(shí)現(xiàn)控件之間的切換。
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是??看绑w組件,可以作為一個(gè)頂層窗口漂浮在桌面,主要作為輔助窗體出現(xiàn)在界面中。QDockWidget包含工具欄和內(nèi)容區(qū)域,工具欄用于顯示窗口標(biāo)題,一個(gè)浮動(dòng)按鈕和一個(gè)關(guān)閉按鈕。QDockWidget可以作為子窗口部件的封裝,通過(guò)setWidget()設(shè)置子窗口部件。
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,主要適用于完成一項(xiàng)工作時(shí)需要用到多個(gè)文件。QMainWindow是SDI(Signal Document Interface,單文檔界面),每個(gè)開(kāi)啟的文件占據(jù)一個(gè)視窗,主要適用于沒(méi)有太多文件的應(yīng)用場(chǎng)景。
????QMdiSubWindow類(lèi)繼承自QWidget,主要用來(lái)創(chuàng)建MDI子窗體實(shí)例。
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滾動(dòng)區(qū)組件用來(lái)顯示子控件的內(nèi)容的框架,如果子控件的尺寸超過(guò)了框架的大小,可以使用滾動(dòng)條,方便查看整個(gè)子控件。QScrollArea可以給任何QWidget添加滾動(dòng)條,但一般自定義窗體添加滾動(dòng)條不顯示。
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()
# 滾動(dòng)區(qū)創(chuàng)建
self.scrollArea = QScrollArea()
# 容器組件
widget = QWidget()
layout = QVBoxLayout()
tableView = QTableView()
button = QPushButton("OK")
layout.addWidget(tableView)
layout.addWidget(button)
widget.setLayout(layout)
# 設(shè)置滾動(dòng)區(qū)的容器組件
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_())