QGridLayout adding weird spaces



  • I am having a little trouble with my 'addRow' and 'addColumn' code.

    I have a gridlayout in a scroll area and initially I populate it with a 3x3 matrix of push buttons that go off and do something else:

    Header main:
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QGridLayout>
    #include <QApplication>
    #include <QPushButton>
    #include <QMainWindow>
    #include <QScrollArea>
    #include <QSignalMapper>
    #include <QHBoxLayout>
    #include <QVBoxLayout>
    #include <trackclickeddialog.h>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {  
    Q_OBJECT
    
    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    void populateViewGrid();
    int x;
    int y;
    int i;
    int j;
    
    public slots:
    void addTrack(QString);
    void trackClicked(QString);
    void upLeftAdded();
    void downLeftAdded();
    void straightAdded();
    void downRightAdded();
    void upRightAdded();
    
    void addRow();
    void addColumn();
    void deleteTrack(QString);
    
    private:
    addTrackDialog *Tom;
    trackClickedDialog *Jerry;
    QScrollArea*scrollArea;
    QWidget * central;
    QGridLayout *gridLayout;
    QSignalMapper *signalMapper;
    
    QPushButton *upLeftTrack;
    QPushButton *downLeftTrack;
    QPushButton *straightTrack;
    QPushButton *downRightTrack;
    QPushButton *upRightTrack;
    
    QVBoxLayout *virtLayout;
    QHBoxLayout *horzLayout;
    
    QWidget *spaceHolder;
    QPushButton *addRowButton;
    QPushButton *addColumnButton;
    
    
    QWidget *container;
    
    Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    

    Main cpp file:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QMainWindow>
    #include <QGridLayout>
    #include <QPushButton>
    #include <QScrollArea>
    #include <QDebug>
    #include <QString>
    #include <QSignalMapper>
    #include <QStringList>
    #include <QLayoutItem>
    #include <QLabel>
    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include "addtrackdialog2.h"
    
    
    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    // to make a grid layout inside a scroll area, you need to make the grid layout the layout to a widget
    // and then put that widget in the scroll area
    central = new QWidget(this);
    gridLayout = new QGridLayout(central);
    scrollArea = new QScrollArea(this);
    
    // making a 3x3 of buttons and connecting them to a dialog
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            QString position= QString("%1,%2").arg(i).arg(j);
            QPushButton*button = new QPushButton("addTrack",central);
            button->setFixedSize(100,50);
            gridLayout->addWidget(button, i, j);
            signalMapper = new QSignalMapper(central);
            connect(button, SIGNAL(clicked()),signalMapper, SLOT(map()));
            signalMapper->setMapping(button, position);
            connect(signalMapper, SIGNAL(mapped(QString)),this, SLOT(addTrack(QString )));
        }
    }
    gridLayout->setSpacing(0);
    central->setLayout(gridLayout);
    
    virtLayout = new QVBoxLayout(this);
    horzLayout = new QHBoxLayout(this);
    
    spaceHolder = new QWidget(this);
    addRowButton = new QPushButton("addRow",central);
    connect(addRowButton,SIGNAL(clicked()),this,SLOT(addRow()));
    
    addColumnButton = new QPushButton("addColumn",central);
    connect(addColumnButton,SIGNAL(clicked()),this,SLOT(addColumn()));
    
    spaceHolder->setLayout(horzLayout);
    horzLayout->addWidget(addRowButton);
    horzLayout->addWidget(addColumnButton);
    
    
    
    virtLayout->addWidget(spaceHolder);
    virtLayout->addWidget(central);
    
    container = new QWidget(this);
    container->setLayout(virtLayout);
    scrollArea->setWidget(container);
    
    
    scrollArea->setWidgetResizable(true);
    setCentralWidget(scrollArea);
    
    
    i=3;
    j=3;
    
    }
    
    
    
    void MainWindow::addTrack(QString position)
    {
    
    //opens up new window and adds a piece of track based on user
    
    }
    
    void MainWindow::addRow()
    {
    //i is x coordinate
    //j is y coordinate
    //if you add a row, y gets deeper so j++
    i++;
    //int q = 0;
    //Now we want to add one button per existing column, or x long
    
    for (int q = 0; q < j; q++) {
        // so now we will do q to i at row j
        QString position= QString("%1,%2").arg(i).arg(q);
        QPushButton*button = new QPushButton("addTrack",central);
        gridLayout->addWidget(button, i, q);
        button->setFixedSize(100,50);
        signalMapper = new QSignalMapper(central);
        connect(button, SIGNAL(clicked()),signalMapper, SLOT(map()));
        signalMapper->setMapping(button, position);
        connect(signalMapper, SIGNAL(mapped(QString)),this, SLOT(addTrack(QString )));
    }
    gridLayout->update();
    gridLayout->setSpacing(0);
    qDebug()<<i<<j;
    
    }
    
    void MainWindow::addColumn()
    {
    //i is x coordinate
    //j is y coordinate
    //if you add a row, y gets deeper so i++
    j++;
    //Now we want to add one button per existing row, or y long
    for (int q = 0; q < i; q++) {
        // so now we will do q to i at row j
        QString position= QString("%1,%2").arg(q).arg(j);
        QPushButton*button = new QPushButton("addTrack",central);
        button->setFixedSize(100,50);
        gridLayout->addWidget(button, q, j);
        signalMapper = new QSignalMapper(central);
        connect(button, SIGNAL(clicked()),signalMapper, SLOT(map()));
        signalMapper->setMapping(button, position);
        connect(signalMapper, SIGNAL(mapped(QString)),this, SLOT(addTrack(QString )));
    }
    gridLayout->update();
    gridLayout->setSpacing(0);
    
    qDebug()<<i<<j;
    }
    
    MainWindow::~MainWindow()
    {
    delete ui;
    }
    

    When I run the program and press the 'addRow' button, it gives me another row, as expected. (I would include pictures, but I can't).

               addRow    addColumn
    
         addTrack   addTrack   addTrack
         addTrack   addTrack   addTrack
         addTrack   addTrack   addTrack
    

    turns into:

               addRow    addColumn
    
         addTrack   addTrack   addTrack
         addTrack   addTrack   addTrack
         addTrack   addTrack   addTrack
         addTrack   addTrack   addTrack
         addTrack   addTrack   addTrack
         addTrack   addTrack   addTrack
    

    (Yay!)

    However, when I click 'addColumn' after I add rows, these weird spaces appear in the layout:

               addRow    addColumn
    
         addTrack   addTrack   addTrack   addTrack
         addTrack   addTrack   addTrack   addTrack
         addTrack   addTrack   addTrack   addTrack
                                          addTrack
         addTrack   addTrack   addTrack   addTrack
         addTrack   addTrack   addTrack   addTrack
         addTrack   addTrack   addTrack
    

    (Booo!)

    Its adding the right number of buttons, and the buttons are all 'hooked up' to the correct signals and slots, but I just cannot figure out how to get rid of these spaces.

    The same concept applies if I make columns first and then add rows, and continues to worsen if I alternate between 'addRow' and 'addColumn'

    I was hoping that you all could help =D

    Thanks!



  • This post is deleted!

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Can you post a complete minimal sample that reproduces this behavior ?



  • @SGaist

    Sorry about that. This should be everything.

    Thanks!


Log in to reply
 

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