[PyQt] help with dip and PyQt4 widgets
Phil Thompson
phil at riverbankcomputing.com
Mon Jul 11 17:08:04 BST 2011
On Fri, 8 Jul 2011 09:49:30 -0400, Lic. José M. Rodriguez Bacallao
<jmrbcu at gmail.com> wrote:
> hi folks, I am creating a composite widget with PyQt4 and Dip, the
> problem I have is that when I use dip properties for setting PyQt4
> properties in the constructor I am getting an error saying that the
> underlying C++ object has been delete, I think this is due to the way
> dip works because it call properties methods before the actual Qt4
> widget as been created when I pass an initial value in the
> constructor. When I construct the object with properties initial
> values and the use the properties accesors to set the value, this
> doens't happen. So, my question is, which is the right way to
> construct a custom composite widget with dip?
>
> # dip imports
> from dip.model import Model, Instance, Str
>
> # PyQt4 imports
> from PyQt4 import QtCore, QtGui
>
> class Indicator(QtGui.QToolButton, Model):
>
> # the indicator identifier, it must be unique for all indicators
> id = Str()
>
> # the indicator text, this text will be shown
> # beside the icon if one is defined
> text = Str()
>
> # the indicator tooltip
> tooltip = Str()
>
> # the indicator icon
> icon = Instance(QtGui.QIcon)
>
> @id.getter
> def id(self):
> print 'getting value'
> return self.objectName()
>
> @id.setter
> def id(self, id):
> print 'setting value'
> self.setObjectName(id)
>
> @text.getter
> def text(self):
> return self.text()
>
> @text.setter
> def text(self, text):
> self.setText(text)
>
> @tooltip.getter
> def tooltip(self):
> return self.toolTip()
>
> @tooltip.setter
> def tooltip(self, tooltip):
> self.setToolTip(tooltip)
>
> @icon.getter
> def icon(self):
> return self.icon()
>
> @icon.setter
> def icon(self, icon):
> self.icon = icon
>
> def perform(self):
> raise NotImplementedError
>
> if __name__ == '__main__':
> app = QtGui.QApplication([])
>
> i = Indicator(text='xxx')
> i.show()
>
> app.exec_()
Your interpretation of the problem is correct - attributes are initialised
(via setters if they have them) before __init__ is called.
I'm not sure what you are trying to achieve. If you simply want to add
behaviour to a QToolButton then I'd just sub-class it as normal. If you
want to add properties then use pyqtProperty().
If you want to create a new Indicator type then I would define an
IIndicator interface and implement an adapter from QToolButton to
IIndicator. The interface and the adapter together would be pretty much
what you have above. If your code sticks to the IIndicator API then it will
work with any widget for which a suitable adapter exists.
Phil
More information about the PyQt
mailing list