脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - PyQt4 treewidget 选择改变颜色,并设置可编辑的方法

PyQt4 treewidget 选择改变颜色,并设置可编辑的方法

2021-07-13 00:37七野 Python

今天小编就为大家分享一篇PyQt4 treewidget 选择改变颜色,并设置可编辑的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# -*- 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_()

PyQt4 treewidget 选择改变颜色,并设置可编辑的方法

#root.setflags(root.flags() | qt.itemiseditable)
#设置可编辑
#item.setbackground(column,qcolor(120,150,50))
#设置背景颜色
#gettext 获取所有item(迭代)

以上这篇pyqt4 treewidget 选择改变颜色,并设置可编辑的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_38641985/article/details/83617657

延伸 · 阅读

精彩推荐