Home > Back-end >  Problem adding QPushButtons to my app. When I run the code, only the last button is placed in my win
Problem adding QPushButtons to my app. When I run the code, only the last button is placed in my win

Time:06-20

This is what I have so far. When I run the app, the last button in the array gets placed which means that the previous 8 are being stepped on.

QWidget *qwid = new QWidget(this);
QGridLayout *gl = new QGridLayout(qwid);
qwid->setLayout(gl);
QTabWidget *qtab = new QTabWidget();

qtab->addTab(qwid, "name");
QStringList buttonLbls({"button1", "button2", "button3:});
QHBoxLayout *hbox = new QHBoxLayout(qwid);
QPushButton *btnCount[3];

for(int i=0; i<3; i  )
{
   btnCount[i] = new QPushButton(buttonsLbls[i], qwid);
   hbox->addWidget(btnCount[i]);
   qwid->setLayout(hbox);
}

CodePudding user response:

You are setting two different layouts for the main widget. Remove the first (gl). Try to set the layout for the main widget and then add buttons:

qwid->setLayout(hbox);
for(int i=0; i<3; i  )
{
   btnCount[i] = new QPushButton(buttonsLbls[i], qwid);
   hbox->addWidget(btnCount[i]);
}

CodePudding user response:

First you need to decide whether you want to have your buttons in a grid or not.

If you go with gridlayout then you can just add your widgets with the QGridLayout::addWidget and pass the correct row and column where the widget should go inside the grid. You won't need the separate horizontal layout in that case. Finally you set the gridlayout as the layout of your parent widget (qwid).

QWidget* qwid = new QWidget(this);
QGridLayout* gridLayout = new QGridLayout();
QStringList buttonLabels({"button1", "button2", "button3", "button4"});
QHash<QString, QPushButton*> buttons; // For storing button pointer for easy access
int columns = 2;
for (int n = 0; n < buttonLabels.size(); n  )
{
    int column = n % columns;
    int row = n / columns;
    const QString& buttonLabel = buttonLabels.at(n);
    QPushButton* button = new QPushButton(buttonLabel);
    buttons[buttonLabel] = button;
    gridLayout->addWidget(button, row, column);
}

qwid->setLayout(gridLayout);

An alternative is to use combination of horizontal and vertical layout where you create horizontal layout for each row (in a loop), add your widgets to the horizontal layout. Then add each horizontal layout inside a single vertical layout with addLayout function. Finally you set the vertical layout as the layout of your parent widget (qwid).

QWidget* qwid = new QWidget(this);
QVBoxLayout* verticalLayout = new QVBoxLayout();
QStringList buttonLabels({"button1", "button2", "button3", "button4"});
QHash<QString, QPushButton*> buttons; // For storing button pointer for easy access
int columns = 2;
int rows = columns / buttonLabels.size();
int index = 0;
for (int row = 0; row < rows; row  )
{
    QHBoxLayout* rowLayout = new QHBoxLayout()
    for (int col = 0; col < columns; col  )
    {
        const QString& buttonLabel = buttonLabels.at(index);
        QPushButton* button = new QPushButton(buttonLabel);
        buttons[buttonLabel] = button;
        rowLayout->addWidget(button);
        index  ;
    }

    verticalLayout->addLayout(rowLayout);
}

qwid->setLayout(verticalLayout);

Another alternative is to just use the horizontal (or vertical) layout only and just add the widgets there and set the layout as the layout of your parent widget (qwid).

QWidget* qwid = new QWidget(this);
QHBoxLayout* horizontalLayout = new QHBoxLayout();
QStringList buttonLabels({"button1", "button2", "button3", "button4"});
QList<QPushButton*> buttons; // For storing button pointer for easy access
for (const QString& buttonLabel : buttonLabels)
{
    QPushButton* button = new QPushButton(buttonLabel);
    buttons << button;
    horizontalLayout->addWidget(button);
}

qwid->setLayout(horizontalLayout);

It depends on what you actually want to do with your widgets.

  •  Tags:  
  • qt
  • Related