Qt widget (with layout). What is it? How to delete?

I wrote a program that uses widgets as a container (for other widgets). Since the contents of the container change depending on the service life of the program, the container widget has a layout associated with it, so it changes correctly. The problem is that the container seems to be consuming some space.

In the next program, I reproduced the problem: I have a group with several shortcuts where one of them is included in the container (the widget w - and its layout t - includes the shortcut "what is this extra space?"). My goal is to make the spaces between all labels the same, regardless of whether they are in containers or not. (the container should not consume space)

I also tried to colorize different parts of the widgets. Where is my supplement? What is the extra space between widgets (between blue ones). And how to remove it?

#include <QApplication> #include <QtCore> #include <QMainWindow> #include <QGroupBox> #include <QHBoxLayout> #include <QLabel> #include <QMdiArea> #include <QMdiSubWindow> #include <stdlib.h> QMdiArea* g1; QGroupBox* g1a; int main(int argc, char *argv[]) { QApplication app(argc, argv); QMainWindow* main_window = new(QMainWindow); main_window->resize(200, 200); main_window->setWindowTitle("Hello"); g1a = new QGroupBox("G1A", g1); QVBoxLayout *g1a_l = new QVBoxLayout(g1a); g1a_l->setSpacing(0); main_window->setCentralWidget(g1a); g1a_l->addWidget((QLabel*)new QLabel(" Nice Label1")); g1a_l->addWidget((QLabel*)new QLabel(" Nice Label2")); QWidget* w=new QWidget(0); w->setStyleSheet( "border: 2 solid blue; padding: 2 solid yellow;" ); QVBoxLayout* t=new QVBoxLayout(w); t->setSpacing(0); t->addWidget(new QLabel("What is that extra space??",w)); g1a_l->addWidget(w); g1a_l->addWidget((QLabel*)new QLabel(" Nice Label3")); g1a_l->addWidget((QLabel*)new QLabel(" Nice Label4")); //sub_window->adjustSize(); main_window->show(); //How to I get that to recaclulate the size of its contents? return app.exec(); } 
+4
source share
3 answers

This is contentsMargin

To remove it:

 t->setContentsMargins(0,0,0,0); 
+7
source

The space you refer to is the boundary of the content. The document here describes how widgets are drawn. To get rid of excess space, you need to call:

 widget->setContentsMargins(0, 0, 0, 0); 

I believe that the end result should look like this:

enter image description here

+2
source

The method I remember for controlling the interval is to add the highlighted QSpacerItem elements between the widgets.

But first I try to understand what w doing. Why not call g1a_l->addLayout(t); directly?

0
source

Source: https://habr.com/ru/post/1394617/


All Articles