[PyKDE] QWidgetFactory and classes defined in Python

Yann Cointepas yann at sapetnioc.org
Mon Oct 3 14:53:28 BST 2005


Hi,

PyQt loose the link between a Python QWidget subclass and the corresponding 
C++ object when using QWidgetFactory.

In the following example, a simple custom widget for designer is created in Python. 
QWidgetFactory is specialized to create custom widget instances and 
registered it with  QWidgetFactory.addFactory. Then a simple .ui file is using this 
custom widget. It almost work but the widget returned by QWidgetFactory.create() 
do not have access to the Python methods defined in its class. Is there a way to 
go around this problem ?

Regards,

      Yann Cointepas


The example is composed of form1.ui and testQWidgetFactory.py and produces 
the following output:

$> python testQWidgetFactory.py
MyCustomWidget.__init__ <__main__.MyCustomWidget object at 0xb7efaaac>
Created widget from .ui file: <qt.QWidget object at 0xb7efaaac>
Traceback (most recent call last):
  File "testQWidgetFactory.py", line 22, in ?
    w.pythonMethod()
AttributeError: pythonMethod

---------- testQWidgetFactory.py ----------
import sys
import qt
import qtui

class MyCustomWidget( qt.QWidget ):
  def __init__( self, parent=None, name=None ):
    qt.QWidget.__init__( self, parent, name )
    print 'MyCustomWidget.__init__', self

  def pythonMethod( self ):
    print 'in MyCustomWidget.pythonMethod()'

class MyWidgetFactory( qtui.QWidgetFactory ):
  def createWidget( self, className, parent, name ):
    if className == 'MyCustomWidget':
      return MyCustomWidget( parent, name )

qApp = qt.QApplication( sys.argv )
qtui.QWidgetFactory.addWidgetFactory( MyWidgetFactory() )
w = qtui.QWidgetFactory.create( 'form1.ui' )
print 'Created widget from .ui file:', w
w.pythonMethod()


---------- form1.ui ----------
<!DOCTYPE UI><UI version="3.3" stdsetdef="1">
<class>Form1</class>
<widget class="QWidget">
    <property name="name">
        <cstring>Form1</cstring>
    </property>
    <property name="geometry">
        <rect>
            <x>0</x>
            <y>0</y>
            <width>600</width>
            <height>480</height>
        </rect>
    </property>
    <property name="caption">
        <string>Form1</string>
    </property>
    <widget class="MyCustomWidget">
        <property name="name">
            <cstring>myCustomWidget1</cstring>
        </property>
        <property name="geometry">
            <rect>
                <x>170</x>
                <y>110</y>
                <width>261</width>
                <height>161</height>
            </rect>
        </property>
    </widget>
</widget>
<customwidgets>
    <customwidget>
        <class>MyCustomWidget</class>
        <header location="local">mywidget.h</header>
        <sizehint>
            <width>-1</width>
            <height>40</height>
        </sizehint>
        <container>0</container>
        <sizepolicy>
            <hordata>5</hordata>
            <verdata>5</verdata>
            <horstretch>0</horstretch>
            <verstretch>0</verstretch>
        </sizepolicy>
        <pixmap>image0</pixmap>
    </customwidget>
</customwidgets>
<images>
    <image name="image0">
        <data format="PNG" length="1125">89504e470d0a1a0a0000000d4948445200000016000000160806000000c4b46c3b0000042c49444154388db5954f6c14551cc73fefcd7476b65bdaae4bb78bb5502a14d404e4801c88182d1c4c2c693da847400f9c24c68b878684238660e2b1e01f12c19493012ef2478c814412d354a46017a8a564bb6da5bbedccee767776e63d0ffb073751d483bfe49799974c3eeffb7ebf37df9fd05a530b2184040cc0042420aaf9a4d0d554800f045a6b256ae0e1e1e1d6bebebe838ee31c48a7d39b5cd7fd075e251cc7617272f2ded8d8d819cff33e0316819259537aead4a9839d5dd6d1784f91f55b0a94830242088404d304292bef68a89f520802a598fecddaa04f1a876f5c250c7c0a64cdeac686e33807e23d45e6b297c8b877f1831542614550b6599835c83c2a81b6786a75134faf2f1169f12997350881d9021d0903e06de0745d3160a6d3e94dbd5b0a64dcbb94b5831d0e3375ab892b1772dcf9790528543f8dd0d367b36768153b5e31503a0f1aecb004580b44ffac58baae8b1714f0833c7638cc8dab303a320f4822ab4c7a37c69196203de3319d5ce1c4d13c733331dedc67a129a154fd128401ab0616d55a130ac3d42d93d1913940d13fd0c9ee0183685c60da01c5421bd72f7a8c8efccef9afd374267ad93d642365be0636a0d28ec7600941d9e6f23917f0e97f23ce5bef35d19ec863da0ed9059b2be70bec196c66dfa10ec0e49b338f7017258651bf95021035c595429bb0903248fe52a2b5b595dd7b4d945cc2340cdca536be389ee3f67886c5798f773fe8e0dac508c989659277a2180da4ca4ff07821058b8b251445d63d6b13ed1098a6417e39cac85197dbe31962ab9bd9f1f22a226d45366f6d0620fdb08c900d281af6110284b20085b414861d905d88f2e52739ee8cbb8022143259d3dd84691730aa2d52da441a8de0c6958068870022a41e9629ad3473fd3b8fdbe319dadb9b4924da994d2d716c7896fbe35152f78b48245d6b2da4507faf582be8eaf159b721cc837b05ae7debb1f79d08cb8b515edad942a22bc4b1c33eb3d34b1c797f06af90a72d16e2f96d9a74aa11dca8586b222d01af0fb60070f6c402d72f15d97f28c6f6d7027a5f5ce6c3233dc4e2ede496b278be4fff608cee8d3e1add806aeca51094cbb06397c1ecc328e746537c7e3ccdb5cb1136bf60635882d4d41c6ec6836ab37efa214f72208ed9f4d7cdd38ee310280542e38b1c43fb6de26b3672e1ec3cc99bcb246f66a938a3241ab3e91f7c861fbf77710b1e5e49915bae974203ba0e9e9c9cbc373d6d6d305a040a89c2a77f50b27d5782bbbf7acccf28349235dd16cf6dd374f7295e1de8a45c02d37499182b01cc0201a085d61a2144d8b2ac8fb6ed340e77240c4261890e04c250185262546d534a032154b59e0ad394e41c98182bf268ce6721ed9f064e0253356f6da2e24c1f030f783c15fe6da680af8021602bd051532ca9b8521488559f61aa86c29343578fbf0264a94c906c7d3409214c20043457a116ff6de6795578012889ff6b98fe016ea0ce1c6a2573410000000049454e44ae426082</data>
    </image>
</images>
<layoutdefaults spacing="6" margin="11"/>
<includehints>
    <includehint>mycustomwidget.h</includehint>
</includehints>
</UI>

-- 
Yann Cointepas                         Tel: +33 1 69 86 78 52
CEA - SHFJ                             Fax: +33 1 69 86 77 86
4, place du General Leclerc
91401 Orsay Cedex France




More information about the PyQt mailing list