QtPython Qtreewidget问题
我试图做一个 Qtreewidget 来参加客户的设计建议。我正在 QtPython 上对其进行编码。我第一次尝试使用 Qt Designer,然后生成代码。但是当我尝试运行它时,出现了一个错误:
self.centralwidget.setSortingEnabled(__sortingEnabled)
AttributeError: setSortingEnabled
我用谷歌搜索了一下,但没有找到解决这个问题的任何解决方案,除了一些建议只是简单地删除代码中导致编译错误的行。但这并没有真正帮助,因为如果你这样做,它会触发更多错误,就像这样:
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'
这是我当前的代码,用于生成一个漂亮的简单 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)
#//==========================//#
我需要使用 Designer 在 Qt 上实现的所有其他对象和到目前为止,一些代码运行良好,例如 inputLine、comboBox、TabWidget。我刚刚遇到了这个 TreeWidget 错误。
任何提示或建议都受到高度赞赏和欢迎。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您发布的代码是您通过 pyuic4 运行 UI 文件时得到的代码。我发现 PyQt UI 编译器有时会出现错误。您所能做的就是手动修复输出并向供应商投诉。
在您的情况下,该行
应该显示
我不明白
AttributeError: 'NoneType' object has no attribute 'setText'
来自何处。您没有在下面的代码中引用self.treeWidget.topLevelItem(0)
。由于 UI 看起来还不太复杂,因此在 QT Designer 中从头开始可能是值得的。尽量避免剪切和粘贴控件,以免混淆Designer/pyuic4。祝你好运!
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
should read
I don't understand where the
AttributeError: 'NoneType' object has no attribute 'setText'
comes from. You're not referencingself.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!
这是解决方案:
self.centralwidget.setSortingEnabled(__sortingEnabled)
然后编码:
并输出
希望这也可以帮助其他人。
ThreaderSlash“at”gmail“dot”com
Here is the solution:
self.centralwidget.setSortingEnabled(__sortingEnabled)
Then code:
and to the output
Hope this may help others too.
ThreaderSlash "at" gmail "dot" com