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

yipeiwu_com6年前Python基础

如下所示:

# -*- 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 选择改变颜色,并设置可编辑的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python WEB应用部署的实现方法

Python WEB应用部署的实现方法

本文介绍了Python WEB应用部署的实现方法,分享给大家,具体如下: 使用Apache模块mod_wsgi运行Python WSGI应用 Flask应用是基于WSGI规范的,所以...

Python利用operator模块实现对象的多级排序详解

前言 最近在工作中碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可以实现这个功能。本文介绍了Python用op...

简单谈谈python的反射机制

  对编程语言比较熟悉的朋友,应该知道“反射”这个机制。Python作为一门动态语言,当然不会缺少这一重要功能。然而,在网络上却很少见到有详细或者深刻的剖析论文。下面结合一个web路由的...

Python数学形态学实例分析

Python数学形态学实例分析

本文实例讲述了Python数学形态学。分享给大家供大家参考,具体如下: 一 原始随机图像 1、代码 import numpy as np import matplotlib.pypl...

深入理解Python中的内置常量

前言 大家都知道Python内置的常量不多,只有6个,分别是True、False、None、NotImplemented、Ellipsis、__debug__。下面就来看看详细的介绍:...