2017-10-11 118 views
0

當用戶在UI中點擊「編輯」時,如何更改樹視圖中所選行的第二列中顯示的文本?我正在使用Python和Pyside,但我不清楚如何做到這一點。QTreeView更改所選項目的顯示文本

我想要發生的是:當用戶在UI中單擊編輯時,我希望它更改所選Treeview行第二列的文本。你可以改變文字來說'你好'或簡單的東西。

​​

import sys 
from PySide import QtGui, QtCore 

class SortModel(QtGui.QSortFilterProxyModel): 

    def __init__(self, *args, **kwargs): 
     super(SortModel, self).__init__(*args, **kwargs) 

    def lessThan(self, left, right): 
     leftData = self.sourceModel().data(left) 
     rightData = self.sourceModel().data(right) 

     if leftData: 
      leftData = leftData.lower() 
     if rightData: 
      rightData = rightData.lower() 

     print('L:', leftData, 'R:', rightData) 
     return leftData < rightData 


class Browser(QtGui.QDialog): 
    def __init__(self, parent=None): 
     super(Browser, self).__init__(parent) 

     self.initUI() 

    def initUI(self): 
     self.resize(200, 300) 
     self.setWindowTitle('Assets') 
     self.setModal(True) 

     self.results = "" 

     self.uiItems = QtGui.QTreeView() 
     self.uiItems.setAlternatingRowColors(True) 
     self.uiItems.setSortingEnabled(True) 
     self.uiItems.sortByColumn(0, QtCore.Qt.AscendingOrder) 
     self.uiItems.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers) 
     self.uiItems.header().setResizeMode(QtGui.QHeaderView.ResizeToContents) 
     self.uiItems.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows) 
     self.uiItems.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection) 
     self._model = self.create_model(self) 
     self._spmodel = SortModel(self) 
     self._spmodel.setSourceModel(self._model) 
     self._spmodel.setDynamicSortFilter(False) 
     self.uiItems.setModel(self._spmodel) 

     self.uiEdit = QtGui.QPushButton('Edit') 

     grid = QtGui.QGridLayout() 
     grid.setContentsMargins(0, 0, 0, 0) 
     grid.addWidget(self.uiItems, 0, 0) 
     grid.addWidget(self.uiEdit, 1, 0) 
     self.setLayout(grid) 

     self.uiItems.doubleClicked.connect(self.doubleClickedItem) 
     self.show() 

    def doubleClickedItem(self, idx): 
     name = idx.data(role=QtCore.Qt.DisplayRole) 
     model = idx.model() 
     model.setData(idx, 'great', role=QtCore.Qt.DisplayRole) 

    def create_model(self, parent): 

     items = [ 
      'Cookie dough', 
      'Hummus', 
      'Spaghetti', 
      'Dal makhani', 
      'Chocolate whipped cream' 
     ] 

     model = QtGui.QStandardItemModel() 
     model.setHorizontalHeaderLabels(['Name', 'Great']) 

     for item in items: 

      root = [] 
      parentNode = QtGui.QStandardItem(item) 
      root.append(parentNode) 

      # add child row with 2 columns 
      for i in range(3): 
       row = [] 
       col1 = QtGui.QStandardItem() 
       col1.setData('COLUMN 1', role=QtCore.Qt.DisplayRole) 
       row.append(col1) 
       col2 = QtGui.QStandardItem() 
       col2.setData('COLUMN 2', role=QtCore.Qt.DisplayRole) 
       row.append(col2)   
       parentNode.appendRow(row) 

      model.appendRow(root) 

     return model 

    def showEvent(self, event): 
     geom = self.frameGeometry() 
     geom.moveCenter(QtGui.QCursor.pos()) 
     self.setGeometry(geom) 
     super(Browser, self).showEvent(event) 

    def keyPressEvent(self, event): 
     if event.key() == QtCore.Qt.Key_Escape: 
      # self.hide() 
      self.close() 
      event.accept() 
     else: 
      super(Browser, self).keyPressEvent(event) 


def main(): 
    app = QtGui.QApplication(sys.argv) 
    ex = Browser() 
    sys.exit(app.exec_()) 


if __name__ == '__main__': 
    main() 
+0

當您按下按鈕時,您希望在GUI中發生什麼? – eyllanesc

+0

@eyllanesc更新後解釋 – JokerMartini

回答