Important: Please read the Qt Code of Conduct -

Make changes in buttons' shapes and sizes in a gridlayout

  • Hi guys,

    This is a for loop of the code positioning and giving sizes to 22 buttons:

    for(int i=0; i<texts.size(); ++i)
             QPushButton* button = new QPushButton(texts[i]);
             connect(button, SIGNAL(clicked(bool)),
                     signalMapper, SLOT(map()));
             signalMapper -> setMapping(button, texts[i]);
             gridLayout -> addWidget(button, i/5, i%5);

    The result is in as follows:

    First I want to make buttons smaller.
    Then to arrange them by manually giving them coordinates.

    How to do these please? Should I firstly remove the gridlayout?

  • Re: Make changes in buttons' shapes and sizes in a gridlayout

    One way to do it:

    Pass the parent 'this' during initialization of the buttons and then move them as desired.

    QPushButton** buttonArray;
    buttonArray = new QPushButton*[22];
    for(int i=0; i<texts.size(); ++i)
             buttonArray[i] = new QPushButton(texts[i], this);
             connect(buttonArray[i], SIGNAL(clicked(bool)),
                     signalMapper, SLOT(map()));
             signalMapper -> setMapping(buttonArray[i], texts[i]);
    buttonArray[i]->move(xPos, yPos);

    As for making the buttons smaller, take a look atQWidget Class for setting max or fixed sizes.

  • @Carmoneer
    Thank you. I set fixed size for the buttons and they appear better now.
    But about positioning, your solution is a bit advanced for me as a newcomer to Qt.

    Please read this scenario:
    Can't I use a vector of strings and then put the strings ("2", "3", "+", "/" and the rest) into it and by a for loop call a function for the work?

    That way I could set the size and position individually for each button manually.

Log in to reply