PyQt4 treewidget 选择改变颜色,并设置可编辑的方法
作者:七野 发布时间:2021-05-26 23:15:11
标签:PyQt4,treewidget,颜色,编辑
如下所示:
# -*- 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(迭代)
来源:https://blog.csdn.net/qq_38641985/article/details/83617657


猜你喜欢
- 今天遇到一个需要用javascript将url中的某些参数替换的需求,想起了不久前从网上淘到了一个parseUrl函数,正好可以借此实现,代
- 在使用Django自带的admin后台的时候,他提供了一些默认的指令可以对数据进行操作, 比如批量删除,修改等 同样的我们也可以
- 前段时日微软(Microsoft)正式发布了.NET Core 2.0,在很多开发社区中反响不错。但还是有一些开发者发出了疑问,.NET C
- 一、python多线程因为CPython的实现使用了Global Interpereter Lock(GIL),使得python中同一时刻只
- 字典是python中唯一内建的映射类型。字典中的值并没有特殊的顺序,但是都存储在一个特定的键(key)里。键可以是数字,字符串甚至是元组。1
- 学习Python数据分析挖掘实战一书时,在数据预处理阶段,有一节要使用拉格朗日插值法对缺失值补充,代码如下:#-*- coding:utf-
- 一、前言我们需要分析某句话,就必须检测该条语句中的词语。一般来说,一句话肯定包含多个词语,它们互相重叠,具体输出哪一个由自然语言的切分算法决
- 作为设计主管,Peter Stern 已经领导 microsoft.com 重新设计了主页并且开发了五个不同的交互工具,这些工具被用于下载中
- 今天看了看数据备份相关的东西,总结了几个MySQL数据备份的注意事项,简单分享一下吧。 对于MySQL备
- mock简介mock原是python的第三方库python3以后mock模块已经整合到了unittest测试框架中,不用再单独安装Mock这
- 存储过程的功能非常强大,在某种程度上甚至可以替代业务逻辑层,接下来就一个小例子来说明,用存储过程插入或更新语句。1、数据库表结构所用数据库为
- 本文实例讲述了Python3.4编程实现简单抓取爬虫功能。分享给大家供大家参考,具体如下:import urllib.requestimpo
- 前言Vuex 是一个专为 Vue.js 应用程序开发的 状态管理模式 。它借鉴了Flux、redux的基本思想,将共享的数据抽离到全局,同时
- 在互联网出现之前,“抄”很不方便,一是“源”少,而是发布渠道少;而在互联网出现之后,“抄”变得很简单,铺天盖地的“源”源源不断,发布渠道也数
- 简介一个基于Python的程序,使用tkinter模块创建一个GUI界面,在Checklistbox中列出系统中正在运行的进程的名称和PID
- 一、Pythont如何打开 txt 格式的文件?1.首先我使用pycharm创建一个项目,然后在这个项目里面再创建一个python的包,然后
- 前言众所周知字典(dict)对象是 Python 最常用的数据结构,社区曾有人开玩笑地说:"Python企图用字典装载整个世界&q
- Base64编码的深入认识与理解 之前在很多业务中都有见过或者用到过Base64编码,但一直一知半解,没有对它有一个深入的认识和
- 一、校验数字的表达式数字:^[0-9]*$n位的数字:^\d{n}$至少n位的数字:^\d{n,}$m-n位的数字:^\d{m,n}$零和非
- 树形目录显示程序问题描述:在我们项目中常常会出现自关联的数据表,如果我们从整体看去,整个表就呈现为一个树形数据结构(对于复杂的情况,它可能变