在多个小部件上显示相同的 QPushButton
我有两个小部件(真实的和假的),其中之一有一个 QPushButton。现在我希望在另一个小部件中显示相同的按钮。我该怎么做?
我不想创建副本,我希望在不更改父级的情况下同时向另一个小部件显示相同的 QObject。
作为示例,在下面我希望“mybutton”同时显示在两个小部件中;
QWidget *widgetReal = new QWidget();
QWidget *widgetFake = new QWidget();
widgetReal->setWindowTitle("Real");
widgetFake->setWindowTitle("Fake");
widgetReal->show();
widgetFake->show();
QGridLayout *layoutReal = new QGridLayout();
QGridLayout *layoutFake = new QGridLayout();
QPushButton *mybutton = new QPushButton();
layoutReal->addWidget(mybutton);
widgetReal->setLayout(layoutReal);
layoutFake->addWidget(mybutton); //this changes the parent and this is not what I want to do
widgetFake->setLayout(layoutFake);
mybutton->show();
我需要这个的原因是因为我有一堆 MDISubWindows 并且它们包含一些控件(按钮、复选框......等)。现在我想从这些小部件中选择其中一些控件并创建一个小部件。这样做的原因是,当我仅使用其中的单个按钮时,我不想显示所有 MDISubwindow 任何建议都非常有帮助。谢谢。
-简历
I have two widgets (Real and Fake) and one of them has a QPushButton. Now I want the same button to be shown in the other widget. How do I do it?
I dont want to create a copy, I want the same QObject on to be shown another widget at the same time without changing the parent.
As an example, in the following I want "mybutton" to be shown in both the widgets at the same time;
QWidget *widgetReal = new QWidget();
QWidget *widgetFake = new QWidget();
widgetReal->setWindowTitle("Real");
widgetFake->setWindowTitle("Fake");
widgetReal->show();
widgetFake->show();
QGridLayout *layoutReal = new QGridLayout();
QGridLayout *layoutFake = new QGridLayout();
QPushButton *mybutton = new QPushButton();
layoutReal->addWidget(mybutton);
widgetReal->setLayout(layoutReal);
layoutFake->addWidget(mybutton); //this changes the parent and this is not what I want to do
widgetFake->setLayout(layoutFake);
mybutton->show();
The reason I need this is because I have a bunch of MDISubWindows and they contain some controls (buttons, checkboxes....etc). Now I want to select some of these controls from these widgets and create a single widget. The reason am doing this is because, I dont want to display all of my MDISubwindow when am using only single button in it
Any suggestions are really helpful. Thank you.
-CV
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
QWidgets 应该只有一个父对象。毫无疑问,您可以想出一些技巧来大致实现您想要的目标,但是当您可以重新设计以适应 Qt GUI 系统的工作方式时,为什么还要这样做呢?
不要试图对抗框架。如果你这样做,你最终会得到难以理解和难以维护的代码。
QWidgets are to supposed to have only a single parent. No doubt you could come up with some hack that would let you achieve roughly what you want, but why do that when you could re-design to fit in with the way the Qt GUI system works?
Don't try and fight against the framework. If you do you'll just end up with hard-to-understand and hard-to-maintain code.
Qt 不支持你想要做的事情,所以我会引用 stu的回应:
尽管小部件不能同时出现在多个位置,但您可以使其看起来如此:
完成此操作后,您将需要处理任何单击和其他事件,并使其显示为已转发等,但除非你付出很大的努力,否则你会失去焦点提示和其他东西。
Qt doesn't support what you want to do so I'll quote stu's response:
Although a widget can't be in multiple places at once, you can make it appear to be so:
Once you've done that, you would need to handle any clicks and other events and make it appear they were forwarded, etc., but you'll lose the focus hints and other things unless you put forth a lot of effort.
不要认为你可以;一个小部件一次只有一个父级。您可以使用 setParent() 移动它,但不能让它同时显示在多个父级中。
Don't think you can; a widget has only one parent at a time. You could move it around with setParent() but not have it show simultaneously in several parents.
您可以创建 QAction 并将它们嵌入到多个 QToolButton 中(使用 QToolButton::setDefaultAction)或通过将操作添加到 QToolBar,而不是尝试在多个父级之间共享按钮。
QAction 可以是可检查/可切换的,并且可以添加到独占组 (QActionGroup) 中以具有与单选按钮相同的行为。
所有 QToolButtons 属性和状态将与它们链接到的 QAction 中的属性和状态保持同步。
Rather than trying to share a button between multiples parents, you can create QActions and embed them in multiple QToolButton (with QToolButton::setDefaultAction) or by adding the action to a QToolBar.
QAction can be checkable/togglable, and can be added to exclusive groups (QActionGroup) to have the same behavior as radio buttons.
All QToolButtons properties and states will be kept in sync with the ones from the QAction they are linked to.
不管怎样,我为此设计了一个技巧。我想做的是将外观与实际按钮对象分开。这是一个示例代码,需要扩展以获得更多功能
file:: VirtualQPushButton.h
file:: main.cpp
让我知道您的意见,感谢您的所有回复。
简历
Anyways, I designed a hack to this. What I am trying to do is separate the appearance from the actual button object. Here's a sample code which needs to be extended for more functionality
file:: VirtualQPushButton.h
file:: main.cpp
Let me know what you opinions, thanks for all your responses.
CV