Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlapping widgets in QtDesigner

Tags:

qt

qt-designer

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:

enter image description here

like image 214
user2366975 Avatar asked Feb 13 '14 19:02

user2366975


1 Answers

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:

enter image description here

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); 
like image 130
hyde Avatar answered Sep 30 '22 05:09

hyde