python – 在.ui文件中调用自定义类失败
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 在.ui文件中调用自定义类失败,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3600字,纯文字阅读大概需要6分钟。
内容图文
当我尝试从.ui文件中引用我的自定义类时出现此错误.我的工作有什么问题?
"QFormBuilder was unable to create a custom widget of the class 'TimelinePane'; defaulting to base class 'QWidget'."
QWidget弹出我在.ui文件中指定的布局.问题只是自定义类.
要添加自定义类的描述,我手动修改了.ui文件(添加了整个< customwidgets>部分),这就是为什么我必须打开一个新问题,因为我还没有找到相同的Q.我怀疑.ui文件中的类的路径,但我尝试的任何选项(参见我注释掉的部分)都没有.我也猜测使用python不应该是这里的事情,但我不完全确定.还没试过C呢.
from PySide import QtGui
from PySide import QtCore
from PySide import QtUiTools
class MyWidget(QtGui.QMainWindow):
def __init__(self, *args):
apply(QtGui.QMainWindow.__init__, (self,) + args)
loader = QtUiTools.QUiLoader()
file = QtCore.QFile('./src/prove_qt_ui_file/prove_main_widget.ui')
file.open(QtCore.QFile.ReadOnly)
self.myWidget = loader.load(file, self)
file.close()
self.setCentralWidget(self.myWidget)
if __name__ == '__main__':
import sys
import os
print("Running in " + os.getcwd() + " .\n")
app = QtGui.QApplication(sys.argv)
win = MyWidget()
win.show()
app.exec_()
prove_main_widget.ui
<?xml version="1.0" encoding="UTF-8" ?>
<ui version="4.0">
<class>MyWidget</class>
<widget class="QWidget" name="MyWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>687</width>
<height>698</height>
</rect>
</property>
<property name="windowTitle">
<string>Runtime Monitor</string>
</property>
<layout class="QVBoxLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="handleWidth">
<number>9</number>
</property>
<widget class="QTreeWidget" name="warn_tree">
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
<widget class="QTreeWidget" name="tree_all_devices">
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
<widget class="TimelinePane" name="timeline_pane" native="true">
<property name="minimumSize">
<size>
<width>0</width>
<height>80</height>
</size>
</property>
<property name="whatsThis">
<string extracomment="Timeline"/>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>TimelinePane</class>
<extends>QWidget</extends>
<!-- <header>timeline_pane</header> --> <!-- NG -->
<!-- <header>prove_qt_ui_file.timeline_pane</header> --> <!-- NG -->
<header>src.prove_qt_ui_file.timeline_pane</header> <!-- NG -->
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
timeline_pane.py
from PySide.QtGui import QWidget, QGraphicsScene, QGraphicsView, QColor, QHBoxLayout, QPushButton
class TimelinePane(QWidget):
def __init__(self, parent):
super(TimelinePane, self).__init__()
print '\tTimelinePane init 1' # This doesn't print.
(环境)Ubuntu 12.04,python 2.7.3
解决方法:
理论上,要使您的自定义小部件工作,您只需要调用:
loader.registerCustomWidget(TimelinePane)
在调用loader.load()之前.在您的情况下,您需要添加相关的import语句:
from timeline_pane import TimelinePane
但是,我刚刚在Ubuntu 12.04 x86_64上使用PySide 1.1.2进行了测试,它导致了段错误. YMMV,所以请测试.
我最终实现了这个解决方法:http://www.mail-archive.com/pyside@qt-project.org/msg00306.html
简而言之,覆盖QUiLoader的creteWidget()方法,如果窗口小部件的class_name不在self.availableWidgets()中,则根据您添加的customWidgets实例变量自行实例化它.
顺便说一句,你也可以在Qt Designer中右键单击“Promote To”功能,而不是直接编辑ui文件.
内容总结
以上是互联网集市为您收集整理的python – 在.ui文件中调用自定义类失败全部内容,希望文章能够帮你解决python – 在.ui文件中调用自定义类失败所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。