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. How to add new widgets between spacers in a QGridLayout?
QtWS25 Last Chance

How to add new widgets between spacers in a QGridLayout?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.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.
  • K Offline
    K Offline
    Kattia
    wrote on 7 Jan 2023, 23:55 last edited by
    #1

    Given this form:

    enter image description here

    How I could add some buttons between the horizontalSpacers at the bottom?

    // slideshow.h
    void SlideShow::addImage(QPixmap pixmap)
    {
        //...
        int index = slideButtons.size();
    
        SlideButton* btn = new SlideButton(this);
        slideButtons.insert(index, btn);
    
        gridLayout->addWidget(btn, 2, index + 1);
    }
    

    #include "slideshow.h"
    MainWindow::MainWindow(QWidget* parent)
        : QMainWindow(parent)
    {
        ui.setupUi(this);
    
        ui.slideShow->gridLayout = ui.gridLayout;
        ui.slideShow->addImage(QPixmap("D:/pictures/pet1.png"));
        ui.slideShow->addImage(QPixmap("D:/pictures/pet2.png"));
        ui.slideShow->addImage(QPixmap("D:/pictures/pet3.png"));
        ui.slideShow->addImage(QPixmap("D:/pictures/pet4.png"));
        ui.slideShow->addImage(QPixmap("D:/pictures/pet5.png"));
    }
    

    Result:

    enter image description here

    Only the first button get in the middle, and its also moving the slideShow widget to the left.

    I'm confused about how to proper add the positions in the layout, to be more precise here:

    gridLayout->addWidget(btn, 2, index + 1);
    
    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 8 Jan 2023, 02:20 last edited by
      #2

      You are asking the grid layout to add a new widget at the far right (index+1), and that is what it is doing. Since the overall grid layout just grew a column the other columns are moved left to make space for it.

      I suspect that you do not want a grid layout at all. In my mind the form's layout should be a QVBoxLayout with three rows: your stacked widget, a vertical space (although why is not clear), and a nested layout. The item in the bottom row is a QHBoxLayout containing a spacer, as many small fixed-width widgets as you need, and another spacer. To insert another widget at the left of the line of small widgets: hLayout->insertWidget(1, widget);
      or at the right end:
      hLayout->insertWidget(hLayout->count() - 1, widget);
      In the middle is something like:
      hLayout->insertWidget(hLayout->count() / 2, widget);

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Kattia
        wrote on 8 Jan 2023, 02:43 last edited by Kattia 1 Aug 2023, 02:46
        #3

        @ChrisW67 said in How to add new widgets between spacers in a QGridLayout?:

        ou are asking the grid layout to add a new widget at the far right (index+1), and that is what it is doing.

        At index 0 is the left spacer, and at index 1 the right spacer, when I add the first button at index 1, it is added before or after the right spacer?

        The strange is that only one button gets to the middle.
        I also tried gridLayout->addWidget(btn, 2, index); and resulted on this:

        1e4e0778-b60e-47f1-9a81-ac9ba9784f49-image.png

        I don't think adding a QHBoxLayout at the bottom is necessary for this task.

        1 Reply Last reply
        0

        1/3

        7 Jan 2023, 23:55

        • Login

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