English 中文(简体)
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.QTreeWidgetItem(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





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签