您好,登錄后才能下訂單哦!
QTableView可以使用自定義的數(shù)據(jù)模型來顯示內(nèi)容,通過setModel綁定數(shù)據(jù)源,由QAbstractItemView類定義的接口來實(shí)現(xiàn),使其能夠顯示由QAbstractItemModel類派生的模型提供的數(shù)據(jù)。
QStringListModel 字符串鏈表數(shù)據(jù)模型
QStandardItemModel標(biāo)準(zhǔn)數(shù)據(jù)項(xiàng)模型,存儲任意結(jié)構(gòu)層次的數(shù)據(jù)
QDirModel 文件系統(tǒng)目錄模型
QSqlQueryModel SQL查詢結(jié)果集數(shù)據(jù)模型
QSqlTableModel SQL表格數(shù)據(jù)模型
QSqlRelationTableModel 關(guān)系型SQL表格數(shù)據(jù)模型
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):
# 數(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是一種樹形結(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ù)模型來顯示內(nèi)容,而QTableWidget則只能使用標(biāo)準(zhǔn)的數(shù)據(jù)模型,并且其單元格數(shù)據(jù)是QTableWidgetItem的對象來實(shí)現(xiàn)的,QTableWidgetItem用來表示表格中的一個單元格,整個表格都需要用逐個單元格構(gòu)建起來。
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樹形單元組件繼承自QTreeView類,可以用來來創(chuàng)建簡單地樹形結(jié)構(gòu)列表。通過QTreeWidget類和QTreeWidgetItem類實(shí)現(xiàn)樹形結(jié)構(gòu),QTreeWidgetItem類實(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,可以顯示一個列表,列表中的每個項(xiàng)是QListWidgetItem的一個實(shí)例,每個項(xiàng)可以通過QListWidgetItem來操作??梢酝ㄟ^QListWidgetItem來設(shè)置每個項(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是棧式容器組件,開發(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組件,可以作為一個頂層窗口漂浮在桌面,主要作為輔助窗體出現(xiàn)在界面中。QDockWidget包含工具欄和內(nèi)容區(qū)域,工具欄用于顯示窗口標(biāo)題,一個浮動按鈕和一個關(guān)閉按鈕。QDockWidget可以作為子窗口部件的封裝,通過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)工作時需要用到多個文件。QMainWindow是SDI(Signal Document Interface,單文檔界面),每個開啟的文件占據(jù)一個視窗,主要適用于沒有太多文件的應(yīng)用場景。
????QMdiSubWindow類繼承自QWidget,主要用來創(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滾動區(qū)組件用來顯示子控件的內(nèi)容的框架,如果子控件的尺寸超過了框架的大小,可以使用滾動條,方便查看整個子控件。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()
# 滾動區(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è)置滾動區(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_())
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。