5
votes

I am trying to achieve this layout:

enter image description here

where Widget1 is some widget (central widget of QMainWindow) and I want to add second widget Widget2 over it but it should be in left bottom corner of Widget1.


EDIT: my previous description wasn't very useful so I will try to describe it in more details.

I am inheriting QWidget class (class MyClass : public QWidget) and creating my own widget where I in void MyClass ::paintEvent(QPaintEvent *event) draw something on screen. MyClass is then centralWidget of my QMainWindow.

Now on top of that I want to add smaller widget (Widget2 in image) where I would display some video (here I am not asking how to display video only how to add this Widget2 to my view). Main thing here is that Widget2 is inside (floating in) Widget1.

EDIT2: Previous code I posted is rubbish.

1
" it doesn't seems to work" is not a very good problem description. Also, need more info, preferably more code. Which layout are you using? What else is there in the layout? Depending on what else there is, you probably want QGridLayout, though you can achieve what you want in many other ways too. Are you using Designer or creating the widgets in your own code? - hyde
you should first create layout object according requirement, check this link - Atul N
Could you check my edit, I tried to be more precise. - carobnodrvo

1 Answers

7
votes

Use QGridLayout to set the position of the widget:

QGridLayout* layout = new QGridLayout(this);
// 2x2 layout
QWidget* green = new QWidget(this);
green->setStyleSheet("background:green;");
QWidget* yellow = new QWidget(this);
yellow->setStyleSheet("background:yellow;");
QWidget* red = new QWidget(this);
red->setStyleSheet("background:red;");
QWidget* blue = new QWidget(this);
blue->setStyleSheet("background:blue;");
layout->addWidget(green, 0, 0); // Top-Left
layout->addWidget(yellow, 0, 1); // Top-Right
layout->addWidget(red, 1, 0); // Bottom-Left
layout->addWidget(blue, 1, 1); // Bottom-Right
ui->centralWidget->setLayout(layout);

Will give you something like that:

enter image description here

So, custom your own widget using QGridLayout and set the position of your widget inside it.

Set another widget as parent with black background:

QGridLayout* layout = new QGridLayout(this);
// 2x2 layout
QWidget* green = new QWidget(this);
green->setStyleSheet("background:green;");
QWidget* yellow = new QWidget(this);
yellow->setStyleSheet("background:yellow;");
QWidget* red = new QWidget(this);
red->setStyleSheet("background:red;");
QWidget* blue = new QWidget(this);
blue->setStyleSheet("background:blue;");
layout->addWidget(green, 0, 0); // Top-Left
layout->addWidget(yellow, 0, 1); // Top-Right
layout->addWidget(red, 1, 0); // Bottom-Left
layout->addWidget(blue, 1, 1); // Bottom-Right

QWidget* mainWidget = new QWidget(this);
mainWidget->setStyleSheet("background:black;");
mainWidget->setLayout(layout);

QHBoxLayout* centralLayout = new QHBoxLayout(this);
centralLayout->addWidget(mainWidget);
ui->centralWidget->setLayout(centralLayout);

enter image description here