How to set size and font for a QPushbutton
-
Hi all,
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 would thank you if you tell me how to do these.
-
-
you can customize your QPushButton style with qt stylesheet, take a look at:
http://doc.qt.io/qt-5/stylesheet-examples.html#customizing-qpushbuttonThank you. I looked at this link but couldn't find useful method for the tasks I mentioned.
Would you please, given the button
my_button
do the changes? -
Three errors:
1-missing terminating " character
my_button ->setStylesheet("font: bold;background-color: red;font-size: 36px;height: 48px;width: 120px;}2- 'class QPushButton' has no member named 'setStylesheet'
my_button ->setStylesheet("font: bold;background-color: red;font-size: 36px;height: 48px;width: 120px;}3- expected ')' before ';' token
connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(get(QString))); -
Glad to see that this helped you,
for font color , the property is
color
, you can write : color: white;//for example,I advise you to list of available properties that you can use with stylesheet from Qt doc:
http://doc.qt.io/qt-5/stylesheet-reference.html#list-of-properties
and to see stylesheet example from qt examples:
http://doc.qt.io/qt-5/qtwidgets-widgets-stylesheet-example.html
-
I advise you to list of available properties that you can use with stylesheet from Qt doc:
http://doc.qt.io/qt-5/stylesheet-reference.html#list-of-properties
Thank you.
Color
was used but about positioning a button not yet!
I fount this using your link.
It appears to be what I need. But how to use it? I tried some ways using the contents of the page but no result.and to see stylesheet example from qt examples:
http://doc.qt.io/qt-5/qtwidgets-widgets-stylesheet-example.html
I also looked at it. It's about
Designer
, which widgets can be customized in property window (coding seems useless) -
Usually for positioning : buttons,widgets ,i am using Layout , i never used
position
property of stylesheet not with stylesheet,i do you have a layout on your application? are you using Qt designer ?If you are using Qt Designer positions of buttons can be changed directly with drag and drop
And yes widgets can be customized from qt Designer , on your mainWindow , on stylesheet property.
-
You can position your widget width QGridLayout, without any problem
http://doc.qt.io/qt-5/qgridlayout.html#addWidget
When you add widgets with gridLayout you choose the row , the column , the row span and column span, and the alignement
row and column are represented on the following picture: