1

ツリーリストにレベルが 1 つしかない場合でも問題なく動作する QTreewidget があります。子サブレベルを追加しようとすると、エラーが発生します。これは、"childs" 行がない場合にのみうまく機能するコードです ("child 1" と "child 2" の後で参照してください)。

def eqpt_centralwdg(self,MainWindow):
    self.centralwidget = QtGui.QWidget(MainWindow)
    self.centralwidget.setObjectName("centralwidget")

    self.colorTreeWidget = QtGui.QTreeWidget(self.centralwidget)
    self.colorTreeWidget.setGeometry(QtCore.QRect(60, 60, 191, 141))
    self.colorTreeWidget.setObjectName("colorTreeWidget")

    # father root 1
    item = QtGui.QTreeWidgetItem(self.colorTreeWidget)
    #child 1 - from father 1
    item = QtGui.QTreeWidgetItem(item)
    #child 2 - from father 1
    item = QtGui.QTreeWidgetItem(item)
    # father root 2
    item = QtGui.QTreeWidgetItem(self.colorTreeWidget)         

    self.connect(self.colorTreeWidget, QtCore.SIGNAL('itemClicked(QTreeWidgetItem*, int)'), self.eqpt_activateInput)

    MainWindow.setCentralWidget(self.centralwidget)

def eqpt_retranslateUi(self, MainWindow):
    MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)
    self.colorTreeWidget.headerItem().setText(0, QtGui.QApplication.translate("MainWindow", "color", None, QtGui.QApplication.UnicodeUTF8) 
    __sortingEnabled = self.colorTreeWidget.isSortingEnabled()    
    self.colorTreeWidget.setSortingEnabled(False) 
    # father root 1
    self.colorTreeWidget.topLevelItem(0).setText(0, QtGui.QApplication.translate("MainWindow", "Yellow", None, QtGui.QApplication.UnicodeUTF8) 
    #child 1 - from father 1
    self.colorTreeWidget.topLevelItem(0).child(0).setText(0, QtGui.QApplication.translate("MainWindow", "Yellow Sun", None, QtGui.QApplication.UnicodeUTF8))
    #child 2 - from father 1
    self.colorTreeWidget.topLevelItem(0).child(1).setText(0, QtGui.QApplication.translate("MainWindow", "Yellow Gold", None, QtGui.QApplication.UnicodeUTF8))        

    # father root 2
    self.colorTreeWidget.topLevelItem(1).setText(0, QtGui.QApplication.translate("MainWindow", "Blue", None, QtGui.QApplication.UnicodeUTF8) 

    self.colorTreeWidget.setSortingEnabled(__sortingEnabled)

これが機能するときの出力です

def eqpt_activateInput(self,item,col):
    print "Qtree ok! pressed"
    print item.text(col)

「子1」と「子2」に関連する行をコードから除外すると、実行されます。そうしないと、次のエラーが表示されます。

    AttributeError: 'NoneType' object has no attribute 'setText'

Qt Designer を使用してコードを生成し、イベントをトリガーする行をいくつか追加しました。

ヒントや提案は大歓迎です。

4

2 に答える 2

5

ツリー ノードは次のようになります。

Node 1
   Node 1.1
      Node 1.1.1
Node 2

(下手な紹介ですみません)

コードでは、最初の最上位ノードの 2 番目の子にアクセスしています。

#child 2 - from father 1
self.colorTreeWidget.topLevelItem(0).child(1)...

しかし、間違ったノードの子を誤って (私が推測すると) 追加したため、存在しません。

一般的に、私はあなたのツリーをそのように構築しません.これがどれほど混乱するかを見ることができます.

parent = QtGui.QTreeWidgetItem(self.colorTreeWidget)
firstchild = QtGui.QTreeWidgetItem(parent)
secondchild = QtGui.QTreeWidgetItem(parent)
parent = QtGui.QTreeWidgetItem(self.colorTreeWidget)

各ノードに一意の名前を付けると、はるかに明確になります。

またはこれでも:

parent = QtGui.QTreeWidgetItem(self.colorTreeWidget)
parent.addChild(...)
parent.addChild(...)
于 2009-11-17T11:50:09.853 に答える
0

解決しました!解決策は次のとおりです。

    parent1 = QtGui.QTreeWidgetItem(self.colorTreeWidget)
    child1_1 = QtGui.QTreeWidgetItem()
    child1_2 = QtGui.QTreeWidgetItem()
    parent1.addChild(child1_1)
    parent1.addChild(child1_2)

現在、正常に動作しています。

提案とコメントをありがとう!

于 2009-11-18T02:33:49.673 に答える