Franky
Franky

Reputation: 1176

How to customize a QPushbutton in Qt code

Consider I have created a button in "C++ code" named my_button:

QPushButton* my_button = new QPushButton (tr("OK"));

Now I want to manipulate this button as follows:

  1. Change the font to Italic or Bold (e.g., OK OK)
  2. Set a red or blue color for it
  3. Make the font bigger/smaller
  4. Change its size (height and width)
  5. Change its position (to right/left/up/down)

I searched the web mush but couldn't find a way for doing all of the works. I would thank you if you tell me how to do these.

Upvotes: 2

Views: 2394

Answers (2)

eyllanesc
eyllanesc

Reputation: 244301

To change the characteristics of the buttons you can use the font and stylesheet, to move the move() function, and to resize the resize() function is used. You do not need to search the entire web, the best source is always the documentation.

QFont font = {your Button}->font();
font.setBold(true); //set style bold if is true
font.setItalic(true); //set style italic if is true
font.setPixelSize(20); // Sets the font size to pixelSize pixels.
{your Button}->setFont(font);
//change color
QString Buttonstyle = "QPushButton {background-color: #0000FF,
                       color: red;}");

{your Button}->setStyleSheet(Buttonstyle);
{your Button}->move({posx}, {posy});
{your Button}->resize({width}, {height});

Upvotes: 1

CMLDMR
CMLDMR

Reputation: 344

QString ButtonStye = "{font: 75 12pt \"Tahoma\"";
        "color: rgb(255, 0, 0);"
        "width : 50px;"
        "height: 25px;"
    "}";

my_button->setStyleSheet(ButtonStye);

you can change width, height, color abd font's pointsize and family. Qt Limited Support CSS. Look At Qt Official Page's How CSS Code implement a specific Widget.

and if you put the your widget(QPushButton) to right position, you can use QHBoxLayout or QVBoxLayout for positiong.

Upvotes: 0

Related Questions