Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Widget inside another widget Qt

Tags:

c++

qt

qt5

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.

like image 562
carobnodrvo Avatar asked Jul 15 '16 08:07

carobnodrvo


People also ask

What is stacked widget in Qt?

QStackedWidget can be used to create a user interface similar to the one provided by QTabWidget. It is a convenience layout widget built on top of the QStackedLayout class. Like QStackedLayout, QStackedWidget can be constructed and populated with a number of child widgets ("pages"):

Should I use Qt Quick or Qt widgets?

Qt Widgets provide means for customization via style sheets, but Qt Quick is a better performing choice for user interfaces that do not aim to look native. Qt Widgets do not scale well for animations. Qt Quick offers a convenient and natural way to implement animations in a declarative manner.

Is Qt widgets deprecated?

It's been so from the beginnings of Qt 5 around 5 years ago. Widgets are not going away anytime soon.


1 Answers

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

like image 170
mohabouje Avatar answered Nov 15 '22 08:11

mohabouje