PyQt4 treewidget 选择改变颜色,并设置可编辑的方法
程序员文章站
2023-12-12 16:11:10
如下所示:
# -*- coding: utf-8 -*-
import sys
from pyside.qtgui import *
from pysid...
如下所示:
# -*- coding: utf-8 -*- import sys from pyside.qtgui import * from pyside.qtcore import * global item_temp item_temp='' class treewidget(qwidget): def __init__(self): super(treewidget, self).__init__() self.setwindowtitle('treewidget') self.tree = qtreewidget() # 实例化一个treewidget对象 self.tree.setcolumncount(2) # 设置部件的列数为2 self.tree.setdropindicatorshown(true) self.tree.setselectionmode(qabstractitemview.extendedselection) self.tree.setheaderlabels(['key', 'value']) # 设置头部信息对应列的标识符 # 设置root为self.tree的子树,故root是根节点 root = qtreewidgetitem(self.tree) root.settext(0, 'root') # 设置根节点的名称 root.setcheckstate(0, qt.unchecked); root.setflags(root.flags() | qt.itemiseditable) #设置可编辑 # 为root节点设置子结点 child1 = qtreewidgetitem(root) child1.settext(0, 'child1') child1.settext(1, 'name1') child1.setcheckstate(0, qt.unchecked); child2 = qtreewidgetitem(root) child2.settext(0, 'child2') child2.settext(1, 'name2') child2.setcheckstate(0, qt.unchecked); child3 = qtreewidgetitem(root) child3.settext(0, 'child3') child3.setcheckstate(0, qt.unchecked); child4 = qtreewidgetitem(child3) child4.settext(0, 'child4') child4.settooltip(0,'child4') #child4.statustip(0) qtooltip.setfont(qfont('oldenglish', 30)) child4.settext(1, 'name4') child4.settooltip(1,'name4') child4.setcheckstate(0, qt.unchecked); child5 = qtreewidgetitem(child3) child5.settext(0, 'child5') child5.settooltip(0,'child5') #child5.statustip(0) qtooltip.setfont(qfont('oldenglish', 30)) child5.settext(1, 'name5') child5.settooltip(1,'name5') child5.setcheckstate(0, qt.unchecked); button=qpushbutton("test") self.lay=qvboxlayout() self.lay.addwidget(button) self.lay.addwidget(self.tree) button.clicked.connect(self.gettext) #self.tree.itemchanged.connect(self.handlechanged) self.tree.itemdoubleclicked.connect(self.handlechanged) #self.tree.itemdoubleclicked.connect(self.handlechanged) self.tree.addtoplevelitem(root) self.setlayout(self.lay) # 将tree部件设置为该窗口的核心框架 def handlechanged(self, item, column): #print dir(item) global item_temp if item_temp=="": item_temp=(item,column) item.setbackground(column,qcolor(100,150,50)) print item_temp else: print item_temp item_temp[0].setbackground(item_temp[1],qcolor(255,255,255)) item.setbackground(column,qcolor(120,150,50)) item_temp=(item,column) print item_temp #self.tree.selecteditems() #item.setbackgroundcolor(column,qcolor(40,150,50)) #col=qcolor(190,150,50) #item.setforeground(column,qbrush(col)) #print dir(item) def gettext(self): t=qtreewidgetitemiterator(self.tree); #print dir(qtreewidgetitemiterator) while(t): try: print t.value().text(0) except: break t.next() #print t app = qapplication(sys.argv) #app.abouttoquit.connect(app.deletelater) tp = treewidget() tp.show() #app.installeventfilter(tp) app.exec_()
#root.setflags(root.flags() | qt.itemiseditable)
#设置可编辑
#item.setbackground(column,qcolor(120,150,50))
#设置背景颜色
#gettext 获取所有item(迭代)
以上这篇pyqt4 treewidget 选择改变颜色,并设置可编辑的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。