Question

I have no idea how to force pyuic4 to generate QSpacerItem's as a class members in a .py file. Everything else is generate as a members of a class, for example gridLayout

self.gridLay = QtGui.QGridLayout()

Everything except QSpacerItem

spacerItem = QtGui.QSpacerItem(20, 0, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Minimum)
self.gridLay.addItem(spacerItem, 0, 2, 1, 1)

Any idea how to correct that?

PS I don't ask how to correct it manually ;).

Was it helpful?

Solution

There's nothing you can do to change this behaviour through the pyuic4 interface.

However, pyuic4 is written in PyQt, so if you feel like hacking, the relevant code is in PyQt4/uic/uiparser.py. Specifically, the createSpacer and setupObject methods of the UIParser class. The setupObject method is what is normally used to create attributes for objects, but obviously the createSpacer method doesn't currently use it.

I'm not sure why things are currently done this way. To get a definitive answer, you'd probably have to ask the project maintainer (Phil Thompson) on the PyQt Mailing List.

Also note that, if you know the spacer's position in the grid-layout, you can access it like this:

self.gridLay.itemAtPosition(row, column).spacerItem()

OTHER TIPS

Pyuic4 is a thin wrapper on top of the Qt uic utility, which has very few options, and this is way beyond its abilities.

What you can do is, in the customary subclass, to save a reference to that item (you've designed the thing, so you're supposed to know on which row/column the spacer is):

# somewhere in your __init__(), *after* calling super()
self.spacerItem = self.gridLay.itemAtPosition ( row, column )

You really should subclass anything coming out of pyuic anyway, so this shouldn't be a big deal.

With your advices I found the solution. I modify PyQt4/uic/uiparser.py a little bit.

361     def createSpacer(self, elem):
362         name = elem.attrib.get('name') #get the name
363         width = elem.findtext("property/size/width")
364         height = elem.findtext("property/size/height")
365         
366         if width is None or height is None:
367             size_args = ()
368         else:
369             size_args = (int(width), int(height))
370             
371         sizeType = self.wprops.getProperty(elem, "sizeType",
372                 QtGui.QSizePolicy.Expanding)
373                 
374         policy = (QtGui.QSizePolicy.Minimum, sizeType)
375         
376         if self.wprops.getProperty(elem, "orientation") == QtCore.Qt.Horizontal:
377             policy = policy[1], policy[0]
378             
379         spacer = self.factory.createQObject("QSpacerItem",
380                 self.uniqueName(name), size_args + policy,
381                 is_attribute=True) #is_attribute=True + set name
382                 
383         if self.stack.topIsLayout():
384             lay = self.stack.peek()
385             gp = elem.attrib["grid-position"]
386             
387             if isinstance(lay, QtGui.QFormLayout):
388                 lay.setItem(gp[0], self._form_layout_role(gp), spacer)
389             else:
390                 lay.addItem(spacer, *gp)

Thanks for help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top