在父级中公开子级的属性
我想创建一个包含 QSpinBox 的自定义小部件。我希望自定义小部件将 QSpinBox 的一些属性公开为自己的属性,以便它可以在 Designer 中方便地工作。
在 Qt 中是否有一种方便的方法来进行这种属性代理?
我想重申一下,我的自定义小部件和 QSpinBox 之间的关系是包含,而不是继承。我正在使用 pyqt,但很乐意接受纯 qt 答案。
为了澄清,我想创建具有一些额外装饰的标准小部件的变体。例如,我将在 QSpinBox 旁边添加一个可切换图标。不过我仍然希望能够在设计器中配置QSpinBox(即设置后缀、上下限、增量等)。
I want to create a custom widget that contains a QSpinBox. I want the custom widget to expose some of QSpinBox's properties as its own so it can work conveniently in Designer.
Is there a convenient way to do this kind of property proxying in Qt?
I want to reiterate that the relationship between my custom widget and the QSpinBox is containment, not inheritance. I am using pyqt, but will happily accept a pure qt answer.
To clarify, I want to create variations of standard widgets that have some extra decorations. For example, I will be adding a toggleable icon to sit beside my QSpinBox. However I still want to be able to configure the QSpinBox in designer (ie, set the suffix, upper and lower limits, increment, etc).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Alrite,这是一种方法。
这比为每个属性手动编写一个函数要好。基本上,您编写一个宏来为您扩展代码。
我的小部件中有一个 doubleSpinBox,每当我更改属性时,它的值都会更改。每当属性发生变化时,您都会发出一个信号,该信号连接到 doubleSpinBox->setValue()。经过测试并且工作完美。
完整代码请访问此链接。
Alrite, so here's a way to do it.
It's better than writing a function manually for each property. Basically, you write a macro that expands the code for you.
I have a doubleSpinBox in my widget and its value is changed whenever I change the property. And you emit a signal whenever the property changes, which is connected to the doubleSpinBox->setValue(). Tested and works perfectly.
Complete code at this link.
这是受 blueskin 启发的 PyQt 版本。这里的主要操作差异是自定义小部件类是在运行时而不是编译时组成的。这样做的优点是我们可以以编程方式检查目标元对象并使用它来生成代理属性。
现在PyQt是从c++编译而来的,所以我不得不相信在c++中运行时生成qt类型也是可能的。然而,我敢打赌那将是地狱般的。
Here is a PyQt version inspired by blueskin. The main operational difference here is that the custom widget class is composed at runtime rather than compile time. The advantage to this is that we can programmatically inspect the target metaobject and use that to generate the proxy properties.
Now PyQt is compiled from c++, so I have to believe that it is also possible to generate qt types at runtime in c++. I am betting that would be hellish, however.
因此,为了让事情变得更清楚,
所以,
So to make things a bit clear,
So,