PDA

View Full Version : Changes in buttons' shapes and positions



franky
28th January 2017, 09:55
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()));
button -> setFixedSize(50,30);

signalMapper -> setMapping(button, texts[i]);
gridLayout -> addWidget(button, i/5, i%5);
}

The image is in the following link:
http://uploads.im/BCXZu.jpg

First I want to change the position of the buttons. For example arrange them. And make some ones smaller/bigger that other ones.

Then to give the buttons some colors to appear nice.

How to do these please? Should I firstly remove the gridlayout?
I wrote the app only in C++ code and didn't use the Designer.

Lesiok
28th January 2017, 11:03
You can use more then one layout (layout in layout). One QGridLayout for digits, second for operators (sum, divide etc.).
For labels Operations, Results and QLineEdits use QFormLayout. Something like this.

franky
28th January 2017, 13:26
Thank you for the reply, but I want to manipulate the size, position, color and the font of each button manually. I think I need to somehow take back the buttons from signalMapper so that I will be able to do the work above on then.

d_stranz
28th January 2017, 16:25
Instead of throwing away the value of "button" on every pass through your loop, make a QList< QPushButton * > or QVector< QPushButton * > as a member of your class and save them there.



// in class definition: QVector< QPushButton * > myButtons

myButtons.clear();
for(int i=0; i<texts.size(); ++i)
{
QPushButton* button = new QPushButton(texts[i]);

connect(button, SIGNAL(clicked(bool)),
signalMapper, SLOT(map()));
button -> setFixedSize(50,30);

signalMapper -> setMapping(button, texts[i]);
gridLayout -> addWidget(button, i/5, i%5);

myButtons.push_back( button );
}


Now that you have saved the button pointers, you can do whatever you want with the button properties. Of course, with fixed size buttons, changing the font will probably result in undesired effects (like the text becoming too big for the button). Why not let the layout do what it is designed to do, and allow the buttons to shrink and grow as the grid does?

Gokulnathvc
13th September 2017, 10:52
You can use something like this for customized shapes, images etc.



QPixmap pixmapLogin(":/new/prefix1/Resources/login_released.png");
QSize imagesize = pixmapLogin.size();
ui->LoginButton->setFixedSize(imagesize);
ui->LoginButton->setMask(pixmapLogin.mask());
ui->LoginButton->setCursor(QCursor(Qt::PointingHandCursor));