如何检索所选树视图项的类对象?有没有办法可以实例化实际的类对象' Asset'在每个树项目上,以便我以后检索或编辑它更容易?
如果您查看下面的代码,当用户点击“应用”#39;按钮我希望它打印所有选定的资产'树视图中的类对象,我该怎么做?
有人可以告诉我如何升级QStandardItem并使其适用于我的工具吗?
这是我的班级对象。
class Asset(object):
def __init__(self, name="", nickname=""):
self.name = name
self.nickname = nickname
self.age = random.randint(1, 10)
self.number = random.randint(1, 10)
self.size = random.uniform(0.0, 100.0)
from __future__ import print_function
# Modules
import sys
import random
from PySide import QtGui, QtCore
class Asset(object):
def __init__(self, name="", nickname=""):
self.name = name
self.nickname = nickname
self.age = random.randint(1, 10)
self.number = random.randint(1, 10)
self.size = random.uniform(0.0, 100.0)
class ToolTest(QtGui.QMainWindow):
def __init__(self):
super(ToolTest, self).__init__()
# ui
self.setWindowTitle(self.tr("Tool"))
self.setMinimumWidth(150)
self.setMinimumHeight(150)
# treeview
self.uiAssetViewer = QtGui.QTreeView()
self.uiAssetViewer.setAlternatingRowColors(True)
self.uiAssetViewer.setModel(createAssetsModel(self))
self.uiAssetViewer.setSortingEnabled(True)
self.uiAssetViewer.sortByColumn(0, QtCore.Qt.AscendingOrder)
self.uiAssetViewer.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers)
self.uiAssetViewer.header().setResizeMode(QtGui.QHeaderView.ResizeToContents)
self.uiAssetViewer.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
self.uiAssetViewer.header().setStretchLastSection(False)
self.uiRunRenamer = QtGui.QPushButton("Apply")
# layout: controls
gdl = QtGui.QGridLayout()
gdl.setContentsMargins(10, 5, 10, 5)
gdl.addWidget(self.uiAssetViewer, 0, 0)
gdl.addWidget(self.uiRunRenamer, 1, 0)
# main layout
main_widget = QtGui.QWidget()
main_widget.setLayout(gdl)
self.setCentralWidget(main_widget)
# actions
self.uiRunRenamer.clicked.connect(self.printData)
def printData(self):
print ("Printing Selected Data...")
def addAsset(model, asset):
model.insertRow(0)
model.setData(model.index(0, 0), asset.name)
model.setData(model.index(0, 1), asset.nickname)
model.setData(model.index(0, 1),
QtGui.QBrush(QtGui.QColor(50, 80, 190, 255)),
QtCore.Qt.ForegroundRole)
def createAssetsModel(parent):
columns = ["Name","New Name"]
model = QtGui.QStandardItemModel(parent)
model.setHorizontalHeaderLabels(columns)
addAsset(model, Asset("Josh", "J-Dog"))
addAsset(model, Asset("Kevin", "Kev"))
addAsset(model, Asset("Michael", "Mike"))
addAsset(model, Asset("Curt", "Urbanis"))
addAsset(model, Asset("Michelle", "El"))
return model
# Main
# -----------------------------------------------------------------------------
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = ToolTest()
window.show()
app.exec_()