问题描述
我有一个 QTabBar,并且所有选项卡中都应该有相同的小部件:
I have a QTabBar, and all tabs are supposed to have the same widget in them:
layout1->addWidget(w);
layout2->addWidget(w);
但是第二次调用 addWidget
会导致这个小部件在第一个布局中消失.
However calling addWidget
the second time causes this widget to disappear in the first layout.
有没有办法用一个小部件将它插入多个标签中?
Is there any way to use one widget to insert it in several tabs?
当然,我总是可以为每个选项卡创建一个新的小部件实例,但这需要额外的时间和内存.
Of course I can always create a new widget instance for every tab, but that takes extra time and memory.
推荐答案
没有.如果您希望小部件被看到两次,那么您需要它的两个实例.
No. If you want the widget to be seen twice, then you need two instances of it.
当然,我总是可以为每个选项卡创建一个新的小部件实例,但这需要额外的时间和内存.
简单的小部件在内存/创建时间方面相对便宜.除非您的 GUI 变得无响应并且您的分析器指出这是一个问题,否则您很可能正在尝试过早优化.
Simple widgets are relatively cheap in regards to memory/time to create. Unless your GUI is becoming unresponsive and your profiler says that this is a problem, you are likely attempting a premature optimization.
根据您要实现的特定目标,有一些模式,例如使用多个视图共享一个可能被采用的模型.
Depending on the specific goal you're trying to accomplish, there are patterns such as using multiple views that share a single model that can potentially be employed.
这篇关于Qt:在多个布局中使用一个小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!