Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Resizing Custom Widgets in a Layout Leaves Empty Space Between Widgets
QtWS25 Last Chance

[SOLVED] Resizing Custom Widgets in a Layout Leaves Empty Space Between Widgets

Scheduled Pinned Locked Moved General and Desktop
layoutqt 5.4.1qt5.4customc++widgetqt 5.4widgetsapplicationdesktop
20 Posts 4 Posters 15.2k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bolt2strike
    wrote on 24 Apr 2015, 23:19 last edited by Bolt2strike
    #1

    Hello fellow Qt developers, right now I am working on resizing some custom widgets (Named "Pods") (Derive QFrame, member widgets: 2 scroll area both containing labels) within a vertical box layout. I use QWidget::resize(int,int) to adjust the size of the member widgets and the widget as it's QFrame base class. When I resize my widgets I notice that within the layout containing my custom widget there is empty space between each custom widget. I have looked through other threads and tried multiple things such as, updating geometries, setting layout size constraints, size policies and so on. So, please help me, it is appreciated. I could just post tons of code here and make a huge post, but instead you can provide the concepts and if you need code I can give it at request just ask for something specific such as the custom class header etc.

    1 Reply Last reply
    0
    • V Offline
      V Offline
      Varius
      wrote on 25 Apr 2015, 15:52 last edited by
      #2

      Did you also try to set the margins to zero in your layout(s)? QLayout::setContentsMargins(int left, int top, int right, int bottom). To me it sounds that your margins still have the default value (11 or 12 etc.) and changing size constraints or policies would not make the widgets closer as the margin values, I think.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on 25 Apr 2015, 18:45 last edited by
        #3

        The margins control the spacing around the children as suggested by @Varius and to modify the spacing between the items you can use setSpacing.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bolt2strike
          wrote on 25 Apr 2015, 20:05 last edited by Bolt2strike
          #4

          I appreciate the ideas guys, I have now tested both but neither appear to have yielded any positive result :( Any other ideas? Additionally, I have now noticed that some widget, extends out of the top level scroll area and blocks mouse interaction despite not being visible. Any ideas for that one?

              MainLayout->setContentsMargins(0,0,0,0);
              MainLayout->setSpacing(0);
          

          No dice.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on 25 Apr 2015, 20:13 last edited by
            #5

            It's hard to help without seeing the code that creates the widget. It could be anything really. You didn't add something to a layout or are trying to modify the size of something governed by a layout etc.

            B 1 Reply Last reply 25 Apr 2015, 20:16
            0
            • C Chris Kawa
              25 Apr 2015, 20:13

              It's hard to help without seeing the code that creates the widget. It could be anything really. You didn't add something to a layout or are trying to modify the size of something governed by a layout etc.

              B Offline
              B Offline
              Bolt2strike
              wrote on 25 Apr 2015, 20:16 last edited by
              #6

              @Chris-Kawa Well, if you'd like I can provide code I just need to know what you want to see, and I am resizing a widget governed by a layout in using QWidget::resize(int,int), is this not correct?

              1 Reply Last reply
              0
              • C Offline
                C Offline
                Chris Kawa
                Lifetime Qt Champion
                wrote on 25 Apr 2015, 20:19 last edited by Chris Kawa
                #7

                It would be nice to see how this widget is created and how children elements are placed in it. Or(if it's large) you could prepare a minimal example that demonstrates the problem and describe the wanted behavior.

                I'm not sure I understand the part about the resize. Do you resize a widget that has a layout set or do you try to resize a widget that is inside a layout of its parent (because that's not right)?

                1 Reply Last reply
                0
                • B Offline
                  B Offline
                  Bolt2strike
                  wrote on 25 Apr 2015, 20:45 last edited by Bolt2strike
                  #8

                  @Chris-Kawa Okay, I'll provide a more thorough explanation and show some code (I probably should have from the beginning). So what I have is a mainwindow with a QScrollArea in it, in this scroll area I have an empty QWidget with a layout (Vertical Box) set, so that in practice whatever I put in the layout is displayed in the scroll area in a fashion like you would probably expect. This layout is filled with x amount of a custom class called Pod the .cpp file is as shown (I know, I am a messy programmer but whatever! Throws organization to the wind!)

                  #include "pod.h"
                  #include "mainwindow.h"
                  
                  Pod::Pod(int xI, int yI, int widthI, int heightI, QWidget *parent) : QFrame(parent), width(widthI),
                      height(heightI), x(xI), y(yI)
                  {
                      this->show();
                      //this->setStyleSheet(".Pod {border: 3px solid orange; border-radius: 3px;}");
                      this->setMinimumSize(width,height);
                      TextLabel = new QLabel(this);
                      TextLabel->setGeometry(QRect(0,0,(width * 0.6),height));
                      //TextLabel->setContentsMargins(5,5,0,10);
                      TextLabel->setWordWrap(true);
                      Image = new QPixmap();
                      ImageScroll = new QScrollArea(this);
                      ImageScroll->setGeometry(QRect(TextLabel->width(),0,(width * 0.4),height));
                      ImageLabel = new QLabel(this);
                      ImageLabel->setGeometry(QRect(TextLabel->width(),0,(width * 0.4),height));
                      ImageLabel->setAlignment(Qt::AlignCenter);
                      //ImageLabel->setContentsMargins(5,0,0,0);
                      ImageLabel->setStyleSheet("border: 2px solid #D50918; border-radius: 3px;");
                      ImageScroll->setWidget(ImageLabel);
                      ImageScroll->setSizeIncrement(ImageScroll->sizeIncrement().width(),5);
                      ImageScroll->setAlignment(Qt::AlignCenter);
                      TextScroll = new QScrollArea(this);
                      TextScroll->setGeometry(QRect(0,0,(width * 0.6),height));
                      TextScroll->setWidget(TextLabel);
                      TextLabel->setStyleSheet("border: 3px solid orange; border-radius: 3px;");
                      //TextLabel->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::ExpandFlag);
                      TextLabel->show();
                      ImageLabel->show();
                      ImageScroll->show();
                      TextScroll->show();
                  }
                  
                  void Pod::setText(QString Text)
                  {
                      TextLabel->setText(Text);
                      TextLabel->adjustSize();
                      TextScroll->resize(TextScroll->width(),TextLabel->height());
                  }
                  
                  void Pod::setImage(QByteArray Data)
                  {
                      Image->loadFromData(Data);
                      ImageLabel->setPixmap(*Image);
                      ImageLabel->resize(Image->width()+10,Image->height()+10);
                      if (TextScroll->height() < ImageScroll->height())
                      {
                          if (TextLabel->height() < Image->height() == false)
                          {
                              ImageScroll->resize(ImageScroll->width(),ImageLabel->height()+10);
                          }
                          else
                          {
                              //ImageScroll->resize(ImageScroll->width(),TextScroll->height());
                              ImageScroll->setGeometry(ImageScroll->x(),ImageScroll->y(),ImageScroll->width(),TextScroll->height());
                          }
                      }
                      this->resize(this->width,(ImageScroll->height() > TextScroll->height()) ? ImageScroll->height() : TextScroll->height());
                      ImageScroll->updateGeometry();
                      TextScroll->updateGeometry();
                      this->updateGeometry();
                  }
                  
                  void Pod::setUrl(QUrl Url)
                  {
                      qDebug() << "SetUrl Called";
                      Download = new Downloader(Url,this);
                      connect(Download,SIGNAL(Downloaded(QByteArray)),this,SLOT(setImage(QByteArray)));
                  }
                  
                  Pod::~Pod()
                  {
                  
                  }
                  

                  Well, that is a long post. I'm not sure if I should explain it (as you can probably figure it out from the code) but, this custom widget has 2 scroll areas in it which each contain a label. Does this information help?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 25 Apr 2015, 21:08 last edited by
                    #9

                    Hi,

                    If I've read Pod's code correctly, there's no layout on it, so you have to do all the placing/resizing for the widgets in it by hand. A QGridLayout might be a better alternative for that.

                    Hope it helps

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    B 1 Reply Last reply 25 Apr 2015, 21:16
                    0
                    • S SGaist
                      25 Apr 2015, 21:08

                      Hi,

                      If I've read Pod's code correctly, there's no layout on it, so you have to do all the placing/resizing for the widgets in it by hand. A QGridLayout might be a better alternative for that.

                      Hope it helps

                      B Offline
                      B Offline
                      Bolt2strike
                      wrote on 25 Apr 2015, 21:16 last edited by Bolt2strike
                      #10

                      @SGaist Are you meaning to say apply a layout directly to the widget as in QWidget::setLayout(QLayout *)? I've just checked the documentation and I'm not quite sure what effect this has/what it does. Could you please elaborate a little bit more?

                      Edit: After reading your post again and thinking about it a bit more, do you mean that I should have a layout as a member of Pod? If this is correct in what you are saying, I think I have not explained correctly, The spacing issue is not within the pods themselves but rather between pods.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 25 Apr 2015, 21:22 last edited by
                        #11

                        If you manage all your pods like the widgets inside them, then you need to do all the mathematics to ensure they are placed and spaced correctly. That's why I suggest using a layout manager inside them and also to handle them

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        B 1 Reply Last reply 25 Apr 2015, 21:28
                        0
                        • S SGaist
                          25 Apr 2015, 21:22

                          If you manage all your pods like the widgets inside them, then you need to do all the mathematics to ensure they are placed and spaced correctly. That's why I suggest using a layout manager inside them and also to handle them

                          B Offline
                          B Offline
                          Bolt2strike
                          wrote on 25 Apr 2015, 21:28 last edited by Bolt2strike
                          #12

                          @SGaist @Chris-Kawa ..... Still not seeing your point, and yes now that I think about it it probably would have been better to have another layout as a member of Pod. But, as for managing placing and spacing, I don't get it, I just construct the object in a QList<Pod *> named PodList and subsequently place it into the layout, then they need not be moved again. I'm not sure if I've mentioned this previously but, the issue with the empty space comes in when I resize TextLabel, ImageLabel and their scroll areas.

                              QList<Pod*> PodList;
                              for (int p=0; p!=NumPods; p++)
                              {
                                  PodList.insert(p,new Pod(0,0,400,150,this));
                                  MainLayout->insertWidget(p,PodList.at(p));
                              }
                          

                          *MainLayout is the layout in the highest level scroll area

                          Here I'll also include a picture to provide additional context to my issue
                          ""

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 26 Apr 2015, 19:38 last edited by
                            #13

                            Since you don't use a layout manager, you also need to take into account the position of your widgets when you resize

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            B 1 Reply Last reply 27 Apr 2015, 15:11
                            0
                            • S SGaist
                              26 Apr 2015, 19:38

                              Since you don't use a layout manager, you also need to take into account the position of your widgets when you resize

                              B Offline
                              B Offline
                              Bolt2strike
                              wrote on 27 Apr 2015, 15:11 last edited by
                              #14

                              @SGaist Okay. But, if I've adjusted the size of the widgets inside the v box layout, shouldn't it resize to fit its children (OR is there a fixed space within the layout that I can position Pods in)? As evidently it doesn't, how can I create this behavior? And yes, I've tried setting size policy to fixed size.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 27 Apr 2015, 21:19 last edited by
                                #15

                                If the children of your widgets are not themselves handled by a layout manager, then no, they won't do anything automatically

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                B 1 Reply Last reply 27 Apr 2015, 21:32
                                0
                                • S SGaist
                                  27 Apr 2015, 21:19

                                  If the children of your widgets are not themselves handled by a layout manager, then no, they won't do anything automatically

                                  B Offline
                                  B Offline
                                  Bolt2strike
                                  wrote on 27 Apr 2015, 21:32 last edited by
                                  #16

                                  @SGaist What? If a widget and all its children in a position above that of another widget were to uniformly decrease in size so that they take up less vertical space expanding downwards but maintain the same position of being in the top left, just like why? Why would this empty space exist the widgets have all decreased in size meaning that they have retracted from their position leaving space which I would have assumed would be removed so that the widgets in the next index position underneath could shift upwards. In other terms, if the size decreases as to retract from a position then shouldn't the layout adjust its allocated space for this widget? I've never had a problem with this before... And one last thing, as an answer to your statement, could I just apply a newly constructed layout to the widgets without setting anything and it would work?

                                  I'm sorry if I do sound a bit rude... I appreciate the help you and others on this form willingly provide, even if I am being difficult.

                                  1 Reply Last reply
                                  0
                                  • S Offline
                                    S Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on 27 Apr 2015, 21:53 last edited by
                                    #17

                                    Widgets in a layout manager are evenly distributed unless you modify their stretch factor (by default 0 IIRC). In the case of a vertical layout, if you add a stretch with a higher value at the last position you should have all your widgets pushed against one another to the top.

                                    QVBoxLayout *layout = new QVBoxLayout(this);
                                    layout->addWidget(widget1);
                                    layout->addWidget(widget2);
                                    etc.
                                    layout->addStretch(1);
                                    

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    B 1 Reply Last reply 28 Apr 2015, 21:14
                                    0
                                    • S SGaist
                                      27 Apr 2015, 21:53

                                      Widgets in a layout manager are evenly distributed unless you modify their stretch factor (by default 0 IIRC). In the case of a vertical layout, if you add a stretch with a higher value at the last position you should have all your widgets pushed against one another to the top.

                                      QVBoxLayout *layout = new QVBoxLayout(this);
                                      layout->addWidget(widget1);
                                      layout->addWidget(widget2);
                                      etc.
                                      layout->addStretch(1);
                                      
                                      B Offline
                                      B Offline
                                      Bolt2strike
                                      wrote on 28 Apr 2015, 21:14 last edited by Bolt2strike
                                      #18
                                      This post is deleted!
                                      1 Reply Last reply
                                      0
                                      • S Offline
                                        S Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 28 Apr 2015, 23:02 last edited by
                                        #19

                                        Good !

                                        However, I'm not sure I'm following you on that one. Where did you put that resize ?

                                        If you can't from the Topic Tools menu, then just edit the thread title and prepend [solved]

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        B 1 Reply Last reply 29 Apr 2015, 21:59
                                        0
                                        • S SGaist
                                          28 Apr 2015, 23:02

                                          Good !

                                          However, I'm not sure I'm following you on that one. Where did you put that resize ?

                                          If you can't from the Topic Tools menu, then just edit the thread title and prepend [solved]

                                          B Offline
                                          B Offline
                                          Bolt2strike
                                          wrote on 29 Apr 2015, 21:59 last edited by
                                          #20
                                          This post is deleted!
                                          1 Reply Last reply
                                          0

                                          5/20

                                          25 Apr 2015, 20:13

                                          topic:navigator.unread, 15
                                          • Login

                                          • Login or register to search.
                                          5 out of 20
                                          • First post
                                            5/20
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved