Calling a custom class in a .ui file

I get this error when I try to access my custom class from a .ui file. What happened to what I'm doing?

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

QWidget appears with the layout that I specify in the .ui file. The problem is this is only a custom class.

To add a description of the custom class, I changed the .ui file manually (I added the entire <customwidgets> section), so I need to open a new question since I have not found the same Q. I suspect the class path in the .ui file. but none of the parameters I tried (see the Part I commented on) worked. I also suggest that using python should not be here, but I'm not quite sure. Have not tried C++ yet.

 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. 

(environment) Ubuntu 12.04, python 2.7.3

+3
python qt pyside qt-designer
Oct. 30 '12 at 19:01
source share
1 answer

In theory, for your custom widget to work, you only need to call:

 loader.registerCustomWidget(TimelinePane) 

before calling loader.load (). You need to add the relevant import statements in your case:

 from timeline_pane import TimelinePane 

However, I just tested this with PySide 1.1.2 on Ubuntu 12.04 x86_64, and this caused segfault. YMMV, so please check.

I ended up implementing this workaround: http://www.mail-archive.com/pyside@qt-project.org/msg00306.html

In short, override the creteWidget () QUiLoader method, and if the widget_name does not belong to self.availableWidgets (), create it yourself according to the customWidgets instance variable you added.

By the way, you can also use the right-click β€œAssist” in Qt Designer instead of directly editing the ui file.

+4
Feb 14 '13 at 14:57
source share



All Articles