开发者

QtPython Qtreewidget Problem

I trying to do a Qtreewidget to attend a customer design suggestion. I am coding it on QtPython. I did a first try using Qt Designer, then generated the code. But when I try to run it, an error comes out:

self.centralwidget.setSortingEnabled(__sortingEnabled)
AttributeError: setSortingEnabled

I googled around, but didn't find any solution for this problem, except some suggestion just to simply delete the lines in the code that results in the compiling error. But it didn't really help, because if you do so, it triggers more error, just like that:

self.treeWidget.topLevelItem(0).child(1).setText(0, QtGui.QApplication.translate("MainWindow", "Item Name", None, QtGui.QApplication.UnicodeUTF8))
AttributeError: 'NoneType' object has no attribute 'setText'

Here is my current code to generate a nice simple QtreeWidget/View:

#//==========================//#
def color_setupUi(self, MainWindow,phrase):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.eqpt_centralwdg(MainWindow)
self.eqpt_retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
#//==========================//#
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")

item = QtGui.QTreeWidgetI开发者_如何转开发tem(self.colorTreeWidget)
item = QtGui.QTreeWidgetItem(self.colorTreeWidget)

self.centralwidget.setSortingEnabled(__sortingEnabled)
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)
self.colorTreeWidget.topLevelItem(0).setText(0, QtGui.QApplication.translate("MainWindow", "Yellow", None, QtGui.QApplication.UnicodeUTF8)
self.colorTreeWidget.topLevelItem(1).setText(0, QtGui.QApplication.translate("MainWindow", "Blue", None, QtGui.QApplication.UnicodeUTF8)
self.colorTreeWidget.setSortingEnabled(__sortingEnabled)
#//==========================//#

All other object I needed to implement on Qt using Designer and a little bit of code has worked fine so far, e.g. inputLine, comboBox, TabWidget. I just got stuck with this TreeWidget error.

Any hints or suggestion are highly appreciated and welcome.


I'm assuming the code you posted is what you got when you ran your UI file through pyuic4. I found the PyQt UI compiler to be buggy sometimes. All you can do is fix the output manually and complain to the vendor.

In your case, the line

self.centralwidget.setSortingEnabled(__sortingEnabled) 

should read

self.colorTreeWidget.setSortingEnabled(__sortingEnabled) 

I don't understand where the AttributeError: 'NoneType' object has no attribute 'setText' comes from. You're not referencing self.treeWidget.topLevelItem(0) in the code below.

Since the UI doesn't seem too complex yet, it might be worth it to start again from scratch in the QT Designer. Try to avoid cutting and pasting controls, so as not to confuse the Designer / pyuic4. Good luck!


Here is the solution:

  1. delete/comment only the following line: self.centralwidget.setSortingEnabled(__sortingEnabled)

Then code:

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")

  item = QtGui.QTreeWidgetItem(self.colorTreeWidget)
  item = QtGui.QTreeWidgetItem(self.colorTreeWidget)         

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

  MainWindow.setCentralWidget(self.centralwidget)  

and to the output

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

Hope this may help others too. ThreaderSlash "at" gmail "dot" com

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜