[PyQt] uic.loadUi() and custom widgets

Albert Cervera i Areny albert at nan-tic.com
Thu Jun 25 08:58:20 BST 2009


A Dijous, 25 de juny de 2009, Greg Smith va escriure:
> I was curious if there was a way for a tool to have its Ui generated
> from a .ui file but still use custom widgets?
>
> I have a widget I wrote that inherits the QLineEdit widget in which I
> needed to modify the event() method so that a custom property will be
> modified if the backspace key was pressed
>
> I was able to get everything to work properly, in a simple dialog where
> I defined the layout within the __init__() method, however for more
> complex dialog windows I'd like to keep the ability to modify the layout
> within the designer and just swap out the which ever widget is supposed
> to be the custom one, that way the __init__() method remains tidy and
> less lines of code.
>
>
>
> Is there any straight forward ways to do this?

Sure. In designer add a QLineEdit, right click on it and select "Promote to 
...". In "Promoted class name" put the name of your class and in header class 
what you'd write in the import statement. For example if you have a class 
called "MyLineEdit" and it's in module "MyWidgets" you'd put:

Promoted class name: MyWidgets
Header file: MyWidgets

Should MyWidgets __init__.py file not include "MyLineEdit" file then you'd do:

Promoted class name: MyWidgets
Header file: MyWidgets.MyLineEditFile

Note that you can also write a plugin in python so your widgets are shown in 
Qt Designer the same way you'd do in C++. Take a look at [1] for more info. 

[1] 
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/pyqt4ref.html#writing-
qt-designer-plugins
>
>
>
> Greg


-- 
Albert Cervera i Areny
http://www.NaN-tic.com
Mòbil: 669 40 40 18


More information about the PyQt mailing list