Adding styles to QPushButton - c++

I have created a qpushbutton in the source file ".cpp":
QPushButton * btn = new QPushButton("Click me");
and now I want to add styles to it. Like changing the background, adding border-radius, changing the cursor, and so on.

This works fine for me:
btn->setObjectName("mybtn");
btn->setStyleSheet(QString(""
"#mybtn{background-color: #182848; border-radius: 5px; border: 1px solid transparent; color: white;}"
"#mybtn:hover{background-color: white; border-color: #182848; color: #182848;}"
));

Related

QMenu: How to customize the menu items of QMenu

I want to build a dropdown list control with QPushButton and QMenu like below:
QPushButton* menuBt = new QPushButton("Please select");
menuBt->setFlat(true);
QMenu* menu = new QMenu();
menuBt->setMenu(menu);
QWidgetAction* wa1 = new QWidgetAction(menu);
QLabel* l1 = new QLabel("Option1");
wa1->setDefaultWidget(l1);
menu->addAction(wa1);
QWidgetAction* wa2 = new QWidgetAction(menu);
QLabel* l2 = new QLabel("Option2");
wa2->setDefaultWidget(l2);
menu->addAction(wa2);
menu->setStyleSheet("QMenu::item {font-family: \"Arial\"; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}"
"QMenu::item:hover {background-color: rgb(0, 0, 255);}");
menuBt->setStyleSheet("QPushButton {font-family: \"Arial\"; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);}");
I set the font and hover background color to the menu items by setStyleSheet, but seems it doesn't work.
How make the font and hover background color work on menu items?
Answer:
class QTDropDownButton : public QPushButton
{
Q_OBJECT
public:
QTDropDownButton(QString text, QWidget *parent = nullptr);
void addItem(QString text);
protected slots:
void menuAboutToShow();
private:
QMenu* menu_;
};
QTDropDownButton::QTDropDownButton(QString text, QWidget *parent) :
QPushButton(text, parent)
{
setFlat(true);
menu_ = new QMenu();
setMenu(menu_);
connect(menu_, SIGNAL(aboutToShow()), this, SLOT(menuAboutToShow()));
setStyleSheet("font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);");
menu_->setStyleSheet("QMenu::item {font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}"
"QMenu::item:selected {background-color: rgb(0, 255, 255);}"
"QLabel {font-family: Arial; font-size: 13pt;}"
"QLabel:hover {background-color: rgb(0, 0, 255);}");
}
void QTDropDownButton::addItem(QString text)
{
if(!menu_)
return;
QWidgetAction* wa1 = new QWidgetAction(menu_);
QLabel* l1 = new QLabel(text);
wa1->setDefaultWidget(l1);
menu_->addAction(wa1);
}
void QTDropDownButton::menuAboutToShow()
{
if(menu_)
menu_->setFixedWidth(this->width());
}
To set the font-family you don't need to put quotes around the Arial. I believe this prevents your style sheet from parsing correctly.
A side note: at the moment only your menuBt is styled, other buttons will look like default buttons. To change button style for all buttons in the menu, move the style into the setStylesheet() call of the menu like this:
menu->setStyleSheet("QMenu::item {font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}" +
"QMenu::item:hover {background-color: rgb(0, 0, 255);}" +
"QPushButton {font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);}");
But if you want only this one button to look different, it is correct to call
setStylesheet() on it, but you can omit the selector, like this:
menuBt->setStyleSheet("font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);");

Customizing QDateEdit style

How can i get the appearance of QDateEdit as shown on the image?
I tried changing the stylesheet, but I dont know how to achieve the spacing between the button and the edit, nor how can I change the down arrow button.
It comes close to the image you've posted:
QDateEdit
{
background-color: white;
border-style: solid;
border-width: 4px;
border-color: rgb(100,100,100);
spacing: 5px;
}
QDateEdit::drop-down {
image: url(:/new/myapp/cbarrowdn.png);
width:50px;
height:15px;
subcontrol-position: right top;
subcontrol-origin:margin;
background-color: white;
border-style: solid;
border-width: 4px;
border-color: rgb(100,100,100);
spacing: 5px;
}
Maybe the key-word here is "sub-control". The arrows of the DateEdit or Combo-Box or anything else, don't apply all the style definitions defined in parent-control. You have to find out, how to address these sub-controls for each Qt-Class and customize the styles for each.
I hope these links will be helpful:
http://doc.qt.io/qt-4.8/stylesheet-customizing.html
http://doc.qt.io/qt-4.8/stylesheet-examples.html

QTableView/QTableWidget grid stylesheet - grid line width

I would like to display table in Qt with specific style. I want to draw all grid lines with same color and same width.
Problem is, that it is hard to style QHeaderView. All the time, I get 2px grid width or no grid at all.
I Have folowing window with one QTableWIdget
and asociated styleSheet
QWidget {
background-color: #333333;
color: #fffff8;
}
QHeaderView::section {
background-color: #646464;
padding: 4px;
border: 1px solid #fffff8;
font-size: 14pt;
}
QTableWidget {
gridline-color: #fffff8;
font-size: 12pt;
}
QTableWidget QTableCornerButton::section {
background-color: #646464;
border: 1px solid #fffff8;
}
Are there any tricks to have all grid lines 1px width? I'm using 4.8.5 and I can't upgrade to version 5.x.
The trick is border-style: none; in QHeaderView::section after witch border-left, border-right, border-top and border-bottom starts working. Correct style for QHeaderView::section should be
QHeaderView::section {
background-color: #646464;
padding: 4px;
font-size: 14pt;
border-style: none;
border-bottom: 1px solid #fffff8;
border-right: 1px solid #fffff8;
}
QHeaderView::section:horizontal
{
border-top: 1px solid #fffff8;
}
QHeaderView::section:vertical
{
border-left: 1px solid #fffff8;
}
I think what you did is you added additional border for section cells, and the section properties should look like that ( although I did not try this solution )
QHeaderView::section {
background-color: #646464;
padding: 4px;
border: 0px;
font-size: 14pt;
}
For more information how to style your headers, see:
http://pastebin.com/svReqHr3
HowTo draw correct CSS border in header?

Changing the background color of QTabWidget

I currently have a QTabWidget that looks like this.The QtabWidget has a QtableView inside it
I wanted to know how I could change the background color of the QtabWidget. I want to maintain my existing stylesheet for the QtabWidget and only add the blue background in the area marked by the red arrows. In short i want to add a background color under the tabs. I currently have a stylesheet that looks like this
QTAB
QTabWidget
{
/* min-width:5000em;*/
}
QTabWidget::tab-bar
{
left: 5px; /* move to the right by 5px */
}
QTabWidget::pane
{
border-top: 1px solid gray;
border-left: 1px solid gray;
border-right: 1px solid gray;
border-bottom: 1px solid gray;
}
QTabBar::tab
{
background-color: rgb(166, 166, 166);
min-width: 70px;
padding-top : 6px;
padding-bottom : 8px;
color: rgb(255, 255, 255);
font: 10pt "Arial";
}
QTabBar::tab:selected, QTabBar::tab:hover
{
background-color: rgb(127, 127, 127);
}
QTabBar::tab:selected
{
/*border-color: #9B9B9B;*/
}
QTabBar::tab:!selected
{
margin-top: 2px; /* make non-selected tabs look smaller */
}
Any suggestions on how I could add a background color under a tab would be appreciated. Thanks.
You can use stStyleSheet in the constructor:
ui->YOURWIDGET->setStyleSheet("background-color: YOURCOLOR");
In order to make subclass of QWidget works on background color, you need override this function
def paintEvent(self, event):
o = QtGui.QStyleOption()
o.initFrom(self)
p = QtGui.QPainter(self)
self.style().drawPrimitive(QtGui.QStyle.PE_Widget, o, p, self)

Styling a QPushButton with two images

I used QSS to style a QPushButton. I wanted to use two images to be set as the background-image. One for normal state & one when the button is pressed. But image shows with a border around it.
My code:
QPushButton{
background-image: url();
border-image: url(images/back/up.png);
color:rgb(255,255,255);
border-width: 2px;
border-radius: 22px;
}
QPushButton::pressed {
border-image:url(images/back/up1.png);
border-width: 2px;
background-image: url();
color:rgb(255,255,255);
}
Try following style snippet. You only need to change the background-image during the :pressed state.
QPushButton{
background-image: url(images/back/up.png);
color:rgb(255,255,255);
border-width: 2px;
border-radius: 22px;
}
QPushButton::pressed {
background-image:url(images/back/up1.png);
}