在.ui文件中调用自定义类失败

4

当我试图从.ui文件中引用自定义类时,出现了这个错误。我做错了什么?

"QFormBuilder was unable to create a custom widget of the class 'TimelinePane'; defaulting to base class 'QWidget'." 

QWidget弹出了我在.ui文件中指定的布局。问题只是自定义类。

为了添加自定义类的描述,我手动修改了.ui文件(添加了整个<customwidgets>部分),这就是为什么我必须开一个新问题,因为我还没有找到相同的问题。我怀疑在.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


您必须为自定义部件创建设计师插件,以便将其与QUiBuilder一起使用。 - Kamil Klimek
1个回答

8

理论上,您只需要调用以下代码即可使您的自定义小部件起作用:

loader.registerCustomWidget(TimelinePane)

在调用loader.load()之前,您需要添加相关的导入语句,在您的情况下:

from timeline_pane import TimelinePane

然而,我刚在Ubuntu 12.04 x86_64上使用PySide 1.1.2测试了一下,结果导致了段错误。可能因人而异,请自行测试。
最终我采用了以下解决方法:http://www.mail-archive.com/pyside@qt-project.org/msg00306.html 简而言之,重写QUiLoader的creteWidget()方法,如果小部件的class_name不在self.availableWidgets()中,则根据您添加的customWidgets实例变量自己实例化它。
另外,您还可以利用Qt Designer中的右键单击“升级到”功能,而不是直接编辑ui文件。

同样/类似的问题在这里:虽然您的解决方法能够启动GUI,但仍无法创建customWidget:AttributeError: type object 'PySide.QtGui.QWidget' has no attribute 'customWidgets'。也许我使用了错误的基类? - cfi
这意味着您需要创建一个 QUiLoader 的子类。在其 __init__ 函数中,您调用父类的初始化,但还将 customWidgets 字典设置为 self(由调用者传入)。 - Charl Botha
1
在 PySide2(我的版本是5.15.2)中,只需在 loader.load 之前调用 loader.registerCustomWidget(TimelinePane) 即可。当然,必须已经导入 TimelinePane 类。 - Nikolai Saiko
1
@NikolaiSaiko,你得到了一次点赞,因为你解决了我与pyqtgraph和.ui文件相关的问题。非常感谢! - Paddy Harrison

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接