[英]Creating item with a integer, QStandardItem doesn't provide QSpinBox
[英]QStandardItem doesn't add children on first row
顯然,在同一行但在新列上設置一個項目,總是會添加一個新行。
from PySide2 import QtCore, QtWidgets, QtGui
# Items for the first row
my_item = QtGui.QStandardItem('Row 0, Col 0')
sub_item = QtGui.QStandardItem('Row 0, Col 1')
# This should add my sub item on the row 0...
my_item.setChild(0, 1, sub_item)
# Model and view
view = QtWidgets.QTreeView()
model = QtGui.QStandardItemModel()
model.setHorizontalHeaderLabels(['col1', 'col2'])
view.setModel(model)
model.appendRow(my_item)
view.show()
我真正想要的是將我的數據放在一行中,而不是添加新行來顯示列。
重要提示:在創建項目的上下文中,我無權訪問 model。
從您想要獲得的內容可以清楚地看出,“sub_item”不是“my_item”的子項,而是兄弟項,因此您必須使用以下代碼添加它:
from PySide2 import QtCore, QtWidgets, QtGui
if __name__ == "__main__":
app = QtWidgets.QApplication()
# Items for the first row
my_item = QtGui.QStandardItem("Row 0, Col 0")
sub_item = QtGui.QStandardItem("Row 0, Col 1")
# Model and view
view = QtWidgets.QTreeView()
model = QtGui.QStandardItemModel()
model.setHorizontalHeaderLabels(["col1", "col2"])
view.setModel(model)
model.appendRow([my_item, sub_item])
view.show()
app.exec_()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.