QtDesigner 中的重叠小部件 [英] Overlapping widgets in QtDesigner
问题描述
我想在 QtDesigner 中叠加两个小部件:有一个很大的 QTextBrowser,右下角应该是一个非交互式标签,我将使用它作为拖动手柄来调整窗口大小(主要小部件是无框的,所以我需要实现它).通常这个标签位于 QTextBrowser 下方,这会在手柄标签的左侧留下很多未使用的空间.所以我想把手柄标签放在 QTextBrowser 之上.我想在 QtDesigner 中实现这一点.但代码看起来像:
I want to overlay two widgets in QtDesigner: There is the big QTextBrowser, and below in the down right corner should be a non-interactiv label that I am going to use as a drag-grip to resize the window (the main widget is frameless so I need to implement it). Usually this label will sit below the QTextBrowser, which leaves on the left of the grip-label a lot of unused space. So I want to put the grip-label above the QTextBrowser. I want to achieve this in QtDesigner. But the code would look like:
QHBoxLayout *layout = new QHBoxLayout(videoWidget);
QLabel *overlayWidget = new QLabel();
overlay->setAlignment(Qt::AlignCenter);
overlay->setText("Overlaid Text");
layout->addWidget(overlay);
或者就像我在 python 中所做的那样:self.textedit = QTextBrowser(self);...GripImage=QLabel(self.textedit);
Or as I already did in python: self.textedit = QTextBrowser(self); ... gripImage=QLabel(self.textedit);
括号之间的部分是父小部件.
There the part between the brackets is the parent widget.
这就是它现在的样子,但这不是我想要的:
That's how it looks right now, but this is not what I want:
推荐答案
这通常是使用 QGridLayout
.它允许小部件占据相同的网格单元.对于这个特殊问题,一个 1x1 的网格就足够了.
This is usually simplest to achieve by using QGridLayout
. It allows widgets to occupy the same grid cells. For this particular problem, a 1x1 grid is enough.
与设计师一起尝试的步骤:
Steps to try it out with designer:
- 为简单起见创建新的表单,简单的小部件
- 向其添加文本编辑(从小部件框拖放),然后从对象检查器中您应该看到它成为根小部件的子部件
- 给它添加一个标签(从 Widget Box 拖放),然后从 Object Inspector 中你应该会看到它成为根小部件的子级
- 右键单击根小部件(在对象检查器中最简单),然后从上下文菜单底部选择布局 > - 在网格中布局立>
- 右键单击标签,然后从布局对齐>设置它对齐到你想要的角落
- Create new form, plain Widget for simplicity
- Add a text edit to it (drag and drop from Widget Box), and from Object Inspector you should see it becomes child of the root widget
- Add a label to it (drag and drop from Widget Box), and from Object Inspector you should see it becomes child of the root widget
- Right click on the root widget (easiest in the Object Inspector), and from the bottom of context menu, select Lay out > - Lay out in Grid
- Right click on the label, and from Layout alignment > set it aligned to the corner you want
完成.这是我的 Designer 中的样子:
Done. Here's what it looks like in my Designer:
现在根据您的真实形式进行调整.
Now adapt above to your real form.
好吧,使用 Designer 实现上述目标似乎很困难,而且做事恰到好处可能有点运气问题……看来,Designer 只是不支持做你想做的事.
Ok, it appears achieving above with Designer is hard, and perhaps a bit a matter of luck of doing things just right... Designer just doesn't support doing what you want, it seems.
为了清楚起见,这是一个完整的源代码:
For clarity this is a complete source code:
QGridLayout *layout = new QGridLayout(widget);
QTextBrowser *textBrowser = new QTextBrowser();
QLabel *label = new QLabel();
label->setText("Overlaid Text");
//label gets positioned above textBrowser and is an overlay
layout->addWidget(textBrowser, 0, 0, Qt::AlignLeft | Qt::AlignTop);
layout->addWidget(label, 0, 0, Qt::AlignRight | Qt::AlignBottom);
这篇关于QtDesigner 中的重叠小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!