【问题标题】:How to highlight QTreeWidgetItem text如何突出显示 QTreeWidgetItem 文本
【发布时间】:2016-05-28 16:37:16
【问题描述】:

单击按钮时,我需要将树项变为突出显示(编辑模式)(见下图)。目前我必须双击该项目以将其设置为突出显示模式。如何做到这一点?

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

class Dialog(QtGui.QDialog):
    def __init__(self, parent=None):
        super(Dialog, self).__init__(parent)
        self.setLayout(QtGui.QVBoxLayout())
        tree = QtGui.QTreeWidget()
        self.layout().addWidget(tree)
        button = QtGui.QPushButton()
        button.setText('Add Item')
        self.layout().addWidget(button)
        button.clicked.connect(self.onClick)

        item = QtGui.QTreeWidgetItem()
        item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable)
        item.setText(0, 'Item number 1')
        tree.addTopLevelItem(item)

    def onClick(self):
        print 'onClick'

dialog=Dialog()
dialog.show()
app.exec_()

【问题讨论】:

    标签: qt pyqt qtreewidget qtreewidgetitem


    【解决方案1】:

    from PyQt4 import QtCore, QtGui
    app = QtGui.QApplication([])
    
    class ItemDelegate(QtGui.QItemDelegate):
        def __init__(self, parent):
            QtGui.QItemDelegate.__init__(self, parent)
    
        def createEditor(self, parent, option, index):
            if not index: return
            line = QtGui.QLineEdit(parent)
            line.setText('Please enter the ')
            # line.selectAll()
            line.setSelection(0, len(line.text()))
            line.setFocus()
            return line
    
    class Dialog(QtGui.QDialog):
        def __init__(self, parent=None):
            super(Dialog, self).__init__(parent)
            self.setLayout(QtGui.QVBoxLayout())
            self.tree = QtGui.QTreeWidget()
            self.layout().addWidget(self.tree)
            button = QtGui.QPushButton()
            button.setText('Add Item')
            self.layout().addWidget(button)
            button.clicked.connect(self.onClick)
            self.tree.setColumnCount(3)
    
            delegate=ItemDelegate(self)
            self.tree.setItemDelegate(delegate)
    
            for row in range(5):
                rootItem = QtGui.QTreeWidgetItem()
                self.tree.addTopLevelItem(rootItem)
                for column in range(3):
                    rootItem.setText(column, 'Root %s row %s'%(row, column))
    
                for child_number in range(2):
                    childItem = QtGui.QTreeWidgetItem(rootItem)
    
                    for col in range(3):
                        childItem.setText(col, 'Child %s row %s'%(child_number, col))
    
        def onClick(self):
            rootItem = QtGui.QTreeWidgetItem()
            rootItem.setText(0, 'New Item')
            rootItem.setFlags(rootItem.flags() | QtCore.Qt.ItemIsEditable)
            self.tree.addTopLevelItem(rootItem)
            self.tree.openPersistentEditor(rootItem, 0)
            rootItem.setSelected(True)
    
    
    dialog=Dialog()
    dialog.show()
    app.exec_()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-12
      • 2012-05-12
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 2010-09-10
      • 2018-10-06
      • 2017-02-04
      相关资源
      最近更新 更多