PyQt4 treewidget 選擇改變顏色,并設(shè)置可編輯的方法
如下所示:
# -*- 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() # 實(shí)例化一個TreeWidget對象 self.tree.setColumnCount(2) # 設(shè)置部件的列數(shù)為2 self.tree.setDropIndicatorShown(True) self.tree.setSelectionMode(QAbstractItemView.ExtendedSelection) self.tree.setHeaderLabels(['Key', 'Value']) # 設(shè)置頭部信息對應(yīng)列的標(biāo)識符 # 設(shè)置root為self.tree的子樹,故root是根節(jié)點(diǎn) root = QTreeWidgetItem(self.tree) root.setText(0, 'root') # 設(shè)置根節(jié)點(diǎn)的名稱 root.setCheckState(0, Qt.Unchecked); root.setFlags(root.flags() | Qt.ItemIsEditable) #設(shè)置可編輯 # 為root節(jié)點(diǎn)設(shè)置子結(jié)點(diǎn) 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部件設(shè)置為該窗口的核心框架 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)
#設(shè)置可編輯
#item.setBackground(column,QColor(120,150,50))
#設(shè)置背景顏色
#getText 獲取所有item(迭代)
以上這篇PyQt4 treewidget 選擇改變顏色,并設(shè)置可編輯的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
CentOS中使用virtualenv搭建python3環(huán)境
virtualenv可以搭建虛擬且獨(dú)立的python環(huán)境,可以使每個項(xiàng)目環(huán)境與其他項(xiàng)目獨(dú)立開來,保持環(huán)境的干凈,解決包沖突問題。下面我們來詳細(xì)探討下centos中如何來搭建。2015-06-06python+html實(shí)現(xiàn)前后端數(shù)據(jù)交互界面顯示的全過程
最近項(xiàng)目中采用了前后端分離的技術(shù),感覺有必要給大家總結(jié)下,所以下面這篇文章主要給大家介紹了關(guān)于python+html實(shí)現(xiàn)前后端數(shù)據(jù)交互界面顯示的相關(guān)資料,需要的朋友可以參考下2022-06-06解讀調(diào)用jupyter?notebook文件內(nèi)的函數(shù)一種簡單方法
這篇文章主要介紹了解讀調(diào)用jupyter?notebook文件內(nèi)的函數(shù)一種簡單方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01python調(diào)用golang中函數(shù)方法
由于simhash方法有多種實(shí)現(xiàn)方式,現(xiàn)python中simhash方法與golang中的不一樣,需要兩者代碼生成結(jié)果保持一致,故采用python中的代碼調(diào)用golang編譯的so文件來實(shí)現(xiàn),需要的朋友可以參考下2024-02-02解決Django transaction進(jìn)行事務(wù)管理踩過的坑
這篇文章主要介紹了解決Django transaction進(jìn)行事務(wù)管理踩過的坑,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04分析Python中解析構(gòu)建數(shù)據(jù)知識
本篇文章給大家講述一下Python中解析構(gòu)建數(shù)據(jù)知識的相關(guān)內(nèi)容,有需要的朋友跟著學(xué)習(xí)下。2018-01-01pycharm下打開、執(zhí)行并調(diào)試scrapy爬蟲程序的方法
本篇文章主要介紹了pycharm下打開、執(zhí)行并調(diào)試scrapy爬蟲程序的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11