简体   繁体   English

PyQt4 pyuic4 spacerItem作为类的成员

[英]PyQt4 pyuic4 spacerItem as a member of class

I have no idea how to force pyuic4 to generate QSpacerItem's as a class members in a .py file. 我不知道如何强制pyuic4生成QSpacerItem作为.py文件中的类成员。 Everything else is generate as a members of a class, for example gridLayout 其他所有内容都是作为类的成员生成的,例如gridLayout

self.gridLay = QtGui.QGridLayout()

Everything except QSpacerItem 除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 ;). PS我不会问如何手动纠正它;)。

There's nothing you can do to change this behaviour through the pyuic4 interface. 你无法通过pyuic4接口改变这种行为。

However, pyuic4 is written in PyQt, so if you feel like hacking, the relevant code is in PyQt4/uic/uiparser.py . 但是, pyuic4是用PyQt编写的,所以如果你觉得自己喜欢黑客攻击,那么相关的代码就是PyQt4/uic/uiparser.py Specifically, the createSpacer and setupObject methods of the UIParser class. 具体来说,是UIParser类的createSpacersetupObject方法。 The setupObject method is what is normally used to create attributes for objects, but obviously the createSpacer method doesn't currently use it. setupObject方法通常用于为对象创建属性,但显然createSpacer方法当前不使用它。

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 . 为了得到确定的答案,你可能不得不在PyQt邮件列表上询问项目维护者(Phil Thompson)。

Also note that, if you know the spacer's position in the grid-layout, you can access it like this: 另请注意,如果您知道spacer在网格布局中的位置,您可以像这样访问它:

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

Pyuic4 is a thin wrapper on top of the Qt uic utility, which has very few options, and this is way beyond its abilities. Pyuic4是Qt uic实用程序之上的一个薄包装,它只有很少的选择,这远远超出了它的能力。

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. 你真的应该把任何来自pyuic的东西都分类,所以这应该不是什么大问题。

With your advices I found the solution. 根据您的建议,我找到了解决方案。 I modify PyQt4/uic/uiparser.py a little bit. 我修改了PyQt4 / uic / uiparser.py。

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! 感谢帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM