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] QTabWidget does not respect spacing from layout
Forum Updated to NodeBB v4.3 + New Features

[Solved] QTabWidget does not respect spacing from layout

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 4.5k Views 3 Watching
  • 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.
  • A Offline
    A Offline
    Anastaziel
    wrote on last edited by JKSH
    #1

    I have this test case:

    // Scroll
    QScrollArea *sa = new QScrollArea(ui->centralWidget);
    sa->setWidgetResizable( true );
    
    // Layout for widgets
    QVBoxLayout *vl_2 = new QVBoxLayout();
    vl_2->setSpacing(0);
    
    // Widget to attach the scroll to and the layout
    QWidget *widget = new QWidget()
    widget->setLayout(vl_2);
    sa->setWidget(widget);
    
    // Test widgets
    QComboBox *cb_1 = new QComboBox();
    QComboBox *cb_2 = new QComboBox();
    vl_2->addWidget( cb_1 );
    vl_2->addWidget( cb_2 );
    

    And the widgets have 0 space between them.

    But if I add them to a QTabWdiget, it all breaks as if QTabWidget does not respect the set setSpacing(0);

    // TabWidget
    QTabWidget *run_results = new QTabWidget(ui->centralWidget);
    run_results->resize( this->size().width() -20, this->size().height() -80 );
    run_results->show();
    
    // Scroll
    QScrollArea *sa = new QScrollArea(ui->centralWidget);
    sa->setWidgetResizable( true );
    
    // Layout for widgets
    QVBoxLayout *vl_2 = new QVBoxLayout();
    vl_2->setSpacing(0);
    
    // Widget to attach the scroll to and the layout
    QWidget *widget = new QWidget()
    widget->setLayout(vl_2);
    sa->setWidget(widget);
    
    // Add the scroll to as the TabWidget tab.
    run_results->addTab(sa, "test");
    
    // Test widgets
    QComboBox *cb_1 = new QComboBox();
    QComboBox *cb_2 = new QComboBox();
    vl_2->addWidget( cb_1 );
    vl_2->addWidget( cb_2 );
    

    Anyone know what I need to do to force QTabWidget to not resize and move my widgets so that they take all the space?

    I tried to add Qt::AlignTop to the addWdiget but it did nothing other than place the first widget at the top and the next in the middle of the screen.

    JKSHJ 1 Reply Last reply
    0
    • M Offline
      M Offline
      msms
      wrote on last edited by
      #2

      If I am not mistaken, you shouldn't be using setSpacing. Spacing refers to the space between items in, say, a QVBoxLayout. Rather, it sounds like you want to make margins around your QTabWidget be zero.

      You do that with

       run_results->setContentsMargins(0, 0, 0, 0);
      
      1 Reply Last reply
      0
      • A Offline
        A Offline
        Anastaziel
        wrote on last edited by
        #3

        Thanks for the reply.

        I actually want to set the spacing between my widgets. I do not want it to be 0 but that is just my example. The problem is that when I put them into a QTabWidget's tab item the widgets are placed in such a way as to take as much space as possible. They are stretched from 1 side of the QTabWidget to the other and the space between them is big (so that it has equal space above the first widget, after it, and after the last widget).

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by SGaist
          #4

          Hi and welcome to devnet,

          If I understand you correctly you would like to have your two combo boxes on top of each other at the top of your widget then add

           vl_2->addStretch(1);
          

          after

           vl_2->addWidget( cb_2 );
          

          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

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Anastaziel
            wrote on last edited by
            #5

            Thanks for the suggestion.

            It does work. I can make it "work" for my project but I would prefer if there is a way for me to force QTabWidget to respect the setSpacing of the Layer. Let me explain why.

            I have 1 GridLayer that is on the widget of the QTabWidget's tab. Inside that GridLayer I add QVBoxLayers as cells. Think of row 1 of GridLayer with 4 QVBoxLayers as 4 columns. The number of QVBoxLayers per row and the number of rows of the GridLayer is controlled dynamically at run time. Inside each QVBoxLayer I have a QHBoxLayers as rows containing 4 widgets each. Think of the QHBoxLayers as drawers. There are different number of QHBoxLayers per QVBoxLayer, again controlled dynamically.

            As you can see it is a bit more complex than my example with the 2 QComboBoxes. If I use addStretch(1) after I add a QHBoxLayer to a QVBoxLayer I will have to setStretch(0) the previously added stretch and addStretch(1) again to the end. That is not an issue but I would like if there is a solution in which I can avoid this. Why is it that QT works as expected (at least what I expected as normal from it) when I add all these layers to the centralWidget but if I add them to the QTabWidget it requires addStretch(1)?

            1 Reply Last reply
            0
            • A Anastaziel

              I have this test case:

              // Scroll
              QScrollArea *sa = new QScrollArea(ui->centralWidget);
              sa->setWidgetResizable( true );
              
              // Layout for widgets
              QVBoxLayout *vl_2 = new QVBoxLayout();
              vl_2->setSpacing(0);
              
              // Widget to attach the scroll to and the layout
              QWidget *widget = new QWidget()
              widget->setLayout(vl_2);
              sa->setWidget(widget);
              
              // Test widgets
              QComboBox *cb_1 = new QComboBox();
              QComboBox *cb_2 = new QComboBox();
              vl_2->addWidget( cb_1 );
              vl_2->addWidget( cb_2 );
              

              And the widgets have 0 space between them.

              But if I add them to a QTabWdiget, it all breaks as if QTabWidget does not respect the set setSpacing(0);

              // TabWidget
              QTabWidget *run_results = new QTabWidget(ui->centralWidget);
              run_results->resize( this->size().width() -20, this->size().height() -80 );
              run_results->show();
              
              // Scroll
              QScrollArea *sa = new QScrollArea(ui->centralWidget);
              sa->setWidgetResizable( true );
              
              // Layout for widgets
              QVBoxLayout *vl_2 = new QVBoxLayout();
              vl_2->setSpacing(0);
              
              // Widget to attach the scroll to and the layout
              QWidget *widget = new QWidget()
              widget->setLayout(vl_2);
              sa->setWidget(widget);
              
              // Add the scroll to as the TabWidget tab.
              run_results->addTab(sa, "test");
              
              // Test widgets
              QComboBox *cb_1 = new QComboBox();
              QComboBox *cb_2 = new QComboBox();
              vl_2->addWidget( cb_1 );
              vl_2->addWidget( cb_2 );
              

              Anyone know what I need to do to force QTabWidget to not resize and move my widgets so that they take all the space?

              I tried to add Qt::AlignTop to the addWdiget but it did nothing other than place the first widget at the top and the next in the middle of the screen.

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by JKSH
              #6

              Hi,

              @Anastaziel said:

              Why is it that QT works as expected (at least what I expected as normal from it) when I add all these layers to the centralWidget but if I add them to the QTabWidget it requires addStretch(1)?

              The difference lies in how you put the QScrollArea inside the parent widget.

              @Anastaziel said:

              QScrollArea *sa = new QScrollArea(ui->centralWidget);
              

              Here, you simply set centralWidget as the parent of sa. You did not set sa as the central widget, and you did not put sa inside the central widget's layout. This means:

              • sa is placed in the upper-left corner of centralWidget.
              • If you enlarge your QMainWindow, centralWidget will also get enlarged, but sa will not.

              However, things will be very different if you did it this way (try it!):

              // Method X
              QScrollArea *sa = new QScrollArea;
              ui->setCentralWidget(sa);
              

              @Anastaziel said:

              run_results->addTab(sa, "test");
              

              Here, you set sa as the "central widget" of the QTabWidget, just like Method X that I posted above. This means:

              • sa always fills the entire space taken by the tab.
              • If you enlarge your QTabWidget, sa will also get enlarged.

              There are 2 ways to achieve what you want:

              1. (Method X) Add 1 more row and column to your QGridLayout 1 more row and column, and call setRowStretch()/setColumnStretch() on them.
              2. (Your original QMainWindow method) Don't add sa directly to the QTabWidget. Instead, create a blank QWidget, make it sa's parent, and add your blank QWidget to the QTabWidget.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              1 Reply Last reply
              1
              • A Offline
                A Offline
                Anastaziel
                wrote on last edited by
                #7

                Thanks @JKSH. Now I understand what the problem was. I fixed it by the second approach. :)

                How do I mark something as answered?

                1 Reply Last reply
                0
                • JKSHJ Offline
                  JKSHJ Offline
                  JKSH
                  Moderators
                  wrote on last edited by
                  #8

                  You're welcome, @Anastaziel :)

                  At the bottom of the page, click "Topic Tools" -> "Mark Solved"

                  Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    Anastaziel
                    wrote on last edited by
                    #9

                    I am sorry to post again but on "Topic Tools" I only have "Delete Topic".

                    JKSHJ 1 Reply Last reply
                    0
                    • A Anastaziel

                      I am sorry to post again but on "Topic Tools" I only have "Delete Topic".

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on last edited by
                      #10

                      @Anastaziel said:

                      I am sorry to post again but on "Topic Tools" I only have "Delete Topic".

                      I reported this at http://forum.qt.io/topic/51926/real-quick-forum-things/4. At the moment, it looks like only moderators can mark "solved". Tero, our community manager, is looking into this.

                      For now, I've marked it for you.

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      1 Reply Last reply
                      0

                      • Login

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