Remove space between two QLabel



  • I have a widget with two QLabel in a QVBoxLayout I used the setSpacing and setContentsMargins but it still with a huge spacing between them.

        mCentralLayout = new QVBoxLayout(this);
        mCentralLayout->setContentsMargins(0, 0, 0, 0);
        mCentralLayout->setSpacing(0);
    
        mTitle = new QLabel("Lorem ipsum dolor sit amet");
        mTitle->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
        mTitle->setFrameShape(QFrame::Box);
        mTitle->setFrameShadow(QFrame::Raised);
    
        mSubtitle = new QLabel("Lorem ipsum dolor sit amet");
        mSubtitle->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
        mSubtitle->setFrameShape(QFrame::Box);
        mSubtitle->setFrameShadow(QFrame::Raised);
    
        mCentralLayout->addWidget(mTitle);
        mCentralLayout->addWidget(mSubtitle);
    

    The QFrame::Box and QFrame::Raised is to make it better to see the results but it will be removed later.

    0_1523929971310_7b836427-e76c-4108-bb3b-c2727a4e44a4-image.png

    How to remove that vertical spacing between them? It's two far away one from the other.

    This is how it looks if I remove the box and raised frame.

    0_1523930034528_8f8d0d8e-bff6-4727-ba4a-63e0e2c39121-image.png


  • Moderators

    @Mr-Gisa You can add vertical spacer bellow the last label



  • But wouldn't that increase the space instead of bringing it together?


  • Lifetime Qt Champion

    Hi,

    If you want two lines that much close to each other, why not write them in the same QLabel ?



  • @SGaist Because I want to change them dynamically. But even if I write them in the same QLabel how to manage the spacing between them?



  • mCentralLayout->addWidget(mTitle);
    mCentralLayout->addWidget(mSubtitle);
    mCentralLayout->addStretch();
    

  • Moderators

    @Mr-Gisa said in Remove space between two QLabel:

    But wouldn't that increase the space instead of bringing it together?

    That's why I suggested to put the spacer BELLOW last label - it will push both labels to the top.



  • The addStretch helped but now the widget is way bigger.

    I created a minimumSizeHint method as I want the widget to be a fixed 60px height but it doesn't work:

    QSize AnnouncementWidget::minimumSizeHint() const
    {
        return QSize(width(), 60);
    }
    

  • Qt Champions 2017

    As @jsulm says
    alt text



  • I will have a QListView bellow them, when I put the QListView it does force the Announcement to be 60px height but it makes the text to be far away one from the other. When I use addStretch it makes the Announcement go the center. The same with the QSpacerItem as you mentioned.

    It has to be something like this:

    0_1523979565090_c8b6bbb7-5ae6-4e6e-b986-810bbfcf84d0-image.png

    The green is the Announcement widget with 60x height and the red is a QListView. The yellow is the two QLabel one above the other, but close not far.


  • Qt Champions 2017

    Hi
    Im not sure why it dont work for you
    alt text



  • The problem for me is that I don't want that space between the Announcement and the QListView :(


  • Qt Champions 2017

    @Mr-Gisa
    But setting layoutSpacing to zero seems to work ?
    alt text



  • No, it doesn't, unfortunately.

    I had to upload the files so you guys can see what I want instead of trying to talk without any code:

    download: https://wetransfer.com/downloads/7d50295b239dac3496c6b2d0d75ebb8420180417162623/bb102b0fdd9926b36249c4964cef6b9f20180417162623/bbd73e


  • Qt Champions 2017

    Hi
    Good move
    You had inner layout so set it in there.

    AnnouncementWidget::AnnouncementWidget(QWidget* parent) : QWidget(parent) {
      centralLayout = new QVBoxLayout(this);
    
      title = new QLabel("Lorem ipsum dolor sit amet");
      subtitle = new QLabel("Lorem ipsum dolor sit amet");
    
      centralLayout->setSpacing(0);
    
      centralLayout->addWidget(title);
      centralLayout->addWidget(subtitle);
    
      QSpacerItem *verticalSpacer = new QSpacerItem(0, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);  
      centralLayout->addItem(verticalSpacer) ;
    }
    
    

    alt text

    Something like that ?
    test project
    https://www.dropbox.com/s/ra7pu8itdm76tv8/announce2.zip?dl=0



  • There is still a spacing there on the bottom:

    0_1523996469594_d8a8276c-88e7-4b4f-bf22-2158efe1cb49-image.png

    That is what I said before, even tho it works it still with a spacing, no between them, but below them.



    • put the 2 labels inside a QWidget
    • set a layout with no spacing in that widget
    • call setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum); on the widget
    • add this widget in a layout with the list view
        mCentralLayout = new QVBoxLayout(this);
        mCentralLayout->setContentsMargins(0, 0, 0, 0);
        mCentralLayout->setSpacing(0);
    QWidget* labelContainer =new QWidget (this);
    QVBoxLayout* containerLay = new QVBoxLayout(labelContainer);
    containerLay->setContentsMargins(0, 0, 0, 0);
    containerLay->setSpacing(0);
    labelContainer->setSizePolicy(QSizePolicy::Maximum,QSizePolicy::Maximum);
        mTitle = new QLabel("Lorem ipsum dolor sit amet");
        mTitle->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
        mTitle->setFrameShape(QFrame::Box);
        mTitle->setFrameShadow(QFrame::Raised);
    
        mSubtitle = new QLabel("Lorem ipsum dolor sit amet");
        mSubtitle->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Fixed);
        mSubtitle->setFrameShape(QFrame::Box);
        mSubtitle->setFrameShadow(QFrame::Raised);
    containerLay->addWidget(mTitle);
    containerLay->addWidget(mSubtitle);
    mCentralLayout->addWidget(labelContainer);
    mCentralLayout->addWidget(new QListView(this));
    


  • @VRonin For me it didn't work, I set the setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum) to the AnnouncementWidget but it made everything disappear:

    0_1524095610460_5faefe65-aaa3-483f-8369-16a113b8f281-image.png

    It looks like this now. It's blue cause I have a background color set.



  • Your sizehints are playing you. use setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Maximum) then



  • I told you already that I'm in love with you ♥
    Thank you very much @VRonin


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.