from PySide import QtGui, QtCore
def __init__(self):
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.view = MyStandardTree(self)
self.addMoveAction()
self.setCentralWidget(self.view)
def addMoveAction(self):
itemUpAction
=QtGui.
QAction("Move up", self
) itemUpAction.
setIcon(QtGui.
QIcon("images/moveItemUp.png")) itemUpAction.triggered.connect(self.moveRowUp)
self.toolbar = self.addToolBar("Mover")
self.toolbar.addAction(itemUpAction)
def moveRowUp(self):
selectedIndexes = self.view.selectedIndexes()
self.view.moveRowUp(selectedIndexes[0])
def __init__(self, parent = None):
self.model.setHorizontalHeaderLabels(['Title', 'Summary'])
rootItem = self.model.invisibleRootItem()
titles = ['Parent', 'Child0', 'Child1', 'Child2', 'Child10', 'Child11']
summaries =['I am the parent', 'I am child0', 'I am child1', 'I am child2', 'foo0', 'foo1']
dataItems = [list(datPair) for datPair in zip(titles, summaries)]
rootItem.appendRow(parentRow)
parentRow[0].appendRow(child0)
parentRow[0].appendRow(child1)
parentRow[0].appendRow(child2)
child1[0].appendRow(child10)
child1[0].appendRow(child11)
self.setModel(self.model)
self.expandAll()
def moveRowUp(self, index):
'''Improved version'''
sourceRowNum = index.row()
if sourceRowNum > 0:
targetRowNum = sourceRowNum - 1
indexParent =index.parent()
if indexParent.isValid():
itemParent = self.model.itemFromIndex(indexParent)
sourceRowItems = itemParent.takeRow(sourceRowNum)
itemParent.insertRow(targetRowNum, sourceRowItems)
else:
sourceRowItems = self.model.takeRow(sourceRowNum)
self.model.insertRow(targetRowNum, sourceRowItems)
selectIndex = self.model.index(targetRowNum, 0, indexParent)
self.selectionModel().clear()
import sys
myTree = MainWindow()
myTree.show()
sys.exit(app.exec_())
from PySide import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self, parent = None)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.view = MyStandardTree(self)
self.addMoveAction()
self.setCentralWidget(self.view)
def addMoveAction(self):
itemUpAction=QtGui.QAction("Move up", self)
itemUpAction.setIcon(QtGui.QIcon("images/moveItemUp.png"))
itemUpAction.triggered.connect(self.moveRowUp)
self.toolbar = self.addToolBar("Mover")
self.toolbar.addAction(itemUpAction)
def moveRowUp(self):
selectedIndexes = self.view.selectedIndexes()
self.view.moveRowUp(selectedIndexes[0])
class MyStandardTree(QtGui.QTreeView):
def __init__(self, parent = None):
QtGui.QTreeView.__init__(self, parent)
self.model = QtGui.QStandardItemModel()
self.model.setHorizontalHeaderLabels(['Title', 'Summary'])
rootItem = self.model.invisibleRootItem()
titles = ['Parent', 'Child0', 'Child1', 'Child2', 'Child10', 'Child11']
summaries =['I am the parent', 'I am child0', 'I am child1', 'I am child2', 'foo0', 'foo1']
dataItems = [list(datPair) for datPair in zip(titles, summaries)]
parentRow = [QtGui.QStandardItem(x) for x in dataItems[0]]
child0 = [QtGui.QStandardItem(x) for x in dataItems[1]]
child1 = [QtGui.QStandardItem(x) for x in dataItems[2]]
child2 = [QtGui.QStandardItem(x) for x in dataItems[3]]
child10 = [QtGui.QStandardItem(x) for x in dataItems[4]]
child11 = [QtGui.QStandardItem(x) for x in dataItems[5]]
rootItem.appendRow(parentRow)
parentRow[0].appendRow(child0)
parentRow[0].appendRow(child1)
parentRow[0].appendRow(child2)
child1[0].appendRow(child10)
child1[0].appendRow(child11)
self.setModel(self.model)
self.expandAll()
def moveRowUp(self, index):
'''Improved version'''
sourceRowNum = index.row()
if sourceRowNum > 0:
targetRowNum = sourceRowNum - 1
indexParent =index.parent()
if indexParent.isValid():
itemParent = self.model.itemFromIndex(indexParent)
sourceRowItems = itemParent.takeRow(sourceRowNum)
itemParent.insertRow(targetRowNum, sourceRowItems)
else:
sourceRowItems = self.model.takeRow(sourceRowNum)
self.model.insertRow(targetRowNum, sourceRowItems)
selectIndex = self.model.index(targetRowNum, 0, indexParent)
self.selectionModel().clear()
self.selectionModel().select(selectIndex, QtGui.QItemSelectionModel.Rows | QtGui.QItemSelectionModel.SelectCurrent)
import sys
app = QtGui.QApplication(sys.argv)
myTree = MainWindow()
myTree.show()
sys.exit(app.exec_())
To copy to clipboard, switch view to plain text mode
Bookmarks