Overlapping widgets in QtDesigner

夙愿已清 提交于 2019-12-10 03:56:09

问题


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); 

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:


回答1:


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:

  1. Create new form, plain Widget for simplicity
  2. 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
  3. 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
  4. 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
  5. Right click on the label, and from Layout alignment > set it aligned to the corner you want

Done. Here's what it looks like in my Designer:

Now adapt above to your real form.


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); 


来源:https://stackoverflow.com/questions/21763949/overlapping-widgets-in-qtdesigner

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!