How to stylize custom classes in Qt using external style sheets? - qt

I have the following codes for my application-
file headerArea.cpp
void MainWindow::createDocks(){
//TOP DOCK OR TITLEBAR
titleBar = new headerArea();
addDockWidget(Qt::TopDockWidgetArea,titleBar);
}
void headerArea::paintEvent (QPaintEvent *){
QStyleOption opt;
opt.init (this);
QPainter p (this);
style ()->drawPrimitive (QStyle::PE_Widget, &opt, &p, this);
}
and in my style sheet-
headerArea#titleBar{
background: #ccc;
}
The style sheet doesn't seem to work on my application. It doesn't even work for-
headerArea{
background: #ccc;
}
But it works fine when I apply the style to the parent class QDockWidget which the class headerArea inherits from-
QDockWidget{
background: #ccc;
}
I'd really appreciate any kind of help.
Thanks!

You should call headerArea's base class paintEvent() in headerArea::paintEvent.

I didn't used your QDockWidget, but how I write style for my qss file is, for exmpale QToolButton
QToolButton {
text-transform: uppercase;
font-family:"Trebuchet MS", sans-serif ;
font-size:1.0em;
color:#fff;
border: 1px solid #000;
background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #565656, stop:1 #000);
}
QToolButton:checked {
border: 1px outset #424242;
background:qlineargradient(spread:pad,x1:1,y1:1,x2:1,y2:0,stop:0 #424242, stop:1 #6e6e6e);
}
QToolButton#buttonNameOne:disabled{
background-color: #d5d5d5;
color: #6ba722;
}
And you can set the style sheet path as setStyleSheet(stylesheetpath+'#buttonNameOne').
Hope this help you

Related

Define multiple styles for pushbutton control Qt Widgets stylesheets

How can I define multiple styles for one kind of control in one stylesheet? So later developer can select, what kind of style control should looks like.
For example, I need to define two styles for QPushButton: for normal button (on the left) and for action button (on the right)
For the first button I wrote the following style:
QPushButton {
background-color: #DCDCDC;
border: 1px solid #BEBEBE;
}
...
And this would apply to all QPushButtons in the project. Next I need to define another action style for QPushButtons also, but which should be selected by developer. How can I do this?
I expect something like this:
QPushButton#ActionButton* {
background-color: blue;
border: 1px solid darkerblue;
}
And then if developer will specify objectName of his button starting with "ActionButton" (example "ActionButtonSendRespond") then it will use the second style.
You can subclass QPushButton into ActionButton so you can write specific CSS :
C++
#include <QPushButton>
class ActionButton : public QPushButton
{
Q_OBJECT
public:
using QPushButton::QPushButton;
};
CSS :
QPushButton {
background-color: #DCDCDC;
border: 1px solid #BEBEBE;
}
ActionButton {
background-color: blue;
border: 1px solid darkerblue;
}
Then when developers use one class or the other, they know which style will apply. And it can be useful if you also need to change the behaviour in the subclass.

QRadioButton color change on Selected and deselected Qt

i am trying to change the color of radiobutton on selected and deselected as
QtStylesheet :Qt Stylesheet
but In this link it only refer to Loading a Image but how could I change it color and without loading Image and change border color or styling radiobutton
the requirement is attached in the Image :
Read documentation carefully. It describes all you need. It even almost described your case, the only difference is images instead of colours.
Style sheet for your case is like this:
QRadioButton {
background-color: gray;
color: white;
}
QRadioButton::indicator {
width: 10px;
height: 10px;
border-radius: 7px;
}
QRadioButton::indicator:checked {
background-color: red;
border: 2px solid white;
}
QRadioButton::indicator:unchecked {
background-color: black;
border: 2px solid white;
}
Setting style sheet to next works for me:
QRadioButton:checked{
background-color: red;
}
QRadioButton:unchecked{
background-color: black;
}
Setting style sheet to QRadioButton::indicator:checked doesn't work, because this only changes the settings of the indicator.
If you want to change the background color of your radiobutton when he's selected you should use both slots and stylesheet.
I will name your button MyButton.
In your .h you will find :
private :
QRadioButton MyButton;
private slots:
void changeColorMyButton();
and in your .cpp add in the setup of your Mainwindow :
QObject::connect(MyButton,SIGNAL(clicked(bool)),this,SLOT(changeColorMyButton));
Your button is now connected to the signal clicked and when you will click on your button, the slot changeColorMyButton will be executed. You can now customize your slot.
void Mainwindow::changeColorMyButton()
{
if(this.MyButton.isChecked())
{
this.MyButton->setStyleSheet("background-color: black");
}
else
{
this.MyButton->setStyleSheet("background-color: yellow");
}
}

Flat QPushButton, background-color doesn't work

I created QPushButton in Qt Designer with this stylesheet:
QPushButton#pushButton {
background-color: #ffffff;
}
QPushButton#pushButton:disabled {
background-color: yellow;
}
QPushButton#pushButton:pressed {
background-color: orange;
}
QPushButton#pushButton:focus:pressed {
background-color: black;
}
QPushButton#pushButton:focus {
background-color: green;
}
QPushButton#pushButton:hover {
background-color: red;
}
QPushButton#pushButton:checked {
background-color: pink;
}
It Works, but when i check "flat" in the properties, then it doesn't work anymore, I would like to ask you why? And how can i do it, if i need flat QPushButton ?
For the second part of your question: Don't use the "flat" property, but modify your stylesheet to achieve a flat look, perhaps like this:
QPushButton#pushButton {
background-color: #ffffff;
border: 0px;
}
/* ... plus the rest of your stylesheet ... */
Concerning the "why doesn't it work" part? In my experience, mixing stylesheets and non-stylesheet-options for widgets yields many mysterious effects that are hard to explain. I have given up asking for the "why"s.
// Send Button
m_send_button = new QPushButton("Send", this);
m_send_button->setFlat(true);
m_send_button->setStyleSheet(QString::fromUtf8("QPushButton:flat {"
"color: #FFFFFF;"
"border: 0px;" // or border: none;
"background-color: #F39200;"
"}"));

Color tag not working on QPushButtons

I am in the process of styling buttons in my user interface, using the UI designer in QT Creator 3.0.1 (with QT 4.8). I am trying to have these buttons behave more like links on a website--without borders, and responding to mouse hovers. Here is the stylesheet I am currently using:
QPushButton {
border: none;
color: #a8a8a8;
}
QPushButton:hover {
color: #ffffff;
}
I thought it was pretty straightforward, but for some reason the color tag is not functioning on hovers. To test, I tried changing the button in other ways, such as changing the background color, and that worked flawlessly.
I also tried changing the selectors to something more specific, by including an ancestor (QWidget QPushButton:hover) and by using the ID (QPushButton#templateButton), but neither have worked.
Is this a problem with the color tag, or am I missing something obvious?
It is also possible to use QLabel to create clickable links. Create custom class ClickableLabel which inherits QLabel and handles mousePressEvents
class ClickableLabel : public QLabel
{
Q_OBJECT
public:
explicit ClickableLabel(QWidget *parent = 0);
signals:
void clicked();
protected:
void mousePressEvent(QMouseEvent * event) ;
};
And
void ClickableLabel::mousePressEvent(QMouseEvent * event)
{
Q_UNUSED(event);
emit clicked();
}
It is also probably possible to handle mouse hover events and change style of the label based on them. However, I have not tested it.
This solution has been copied from somewhere but I do not remember anymore the original source.
Try this :
#templateButton {
border: 0px;
color: #a8a8a8;
text-decoration: underline;
text-align: right;
}
#templateButton:hover {
color: #FFFFFF;
}
#Anthony Hilyard I show you some pictures so I post another answer here.
1.normal
2.hover
#pushButtonForgetPassword {
border: none;
color: #0066ff;
text-decoration: underline;
text-align: right;
}
#pushButtonForgetPassword:hover {
color: #FFFFFF;
}
pushButtonForgetPassword would be the ID of my link button

Tab close button position

I want to style my tabs in my Qt app as follows:
I used following style sheet:
QTabBar{background-color: #fff; border-top: 0px;}
QTabBar::tab {
border-image: url(:/New_UI/tab_inactive.png) 7 17 7 2;
margin-left: 2px;
border-right: 17px;
border-top: 5px;
border-bottom: 5px;
font: 400 9.2pt "Segoe UI";
color: #ccc;
padding: 0px 13px 0px 5px;
max-height: 26px;
}
QTabBar::tab:selected, QTabBar::tab:hover {
border-image: url(:/New_UI/tab_active.png) 6 17 6 2;
}
QTabBar::close-button {
image: url(:/New_UI/tab_close.png);
subcontrol-origin: padding;
subcontrol-position: right;
width: 13px;
height: 13px;
}
The result is as follows (close button position is not as I wanted):
What am I doing wrong & how could I get my desired result ?
EDIT : I know this post is old, but I hope it could help someone else.
After a couple of tests, I think there is one way to do this, but it does not use Qt style sheets :
Subclass your QTabWidget to have complete access to the protected features
Create your own QWidget or QPushButton as your close button
Manage the position of your button with the stylesheet property (margin-right for example)
Add your button to the tab tabBar()->setTabButton(index, QTabBar::RightSide, closeButton);
The code I used for the test :
MyTab::MyTab(QWidget *parent) : QTabWidget(parent)
{
/// Create your button
QPushButton *close = new QPushButton(this);
// Add a tab
addTab(new QWidget(), QIcon(), "Tab 1");
setStyleSheet("QTabBar::tab { width : 150px;}");
// Size and move your button
close->setStyleSheet("max-height: 14px; max-width: 15px; margin-right: 50px;");
// Add your button to the tab
tabBar()->setTabButton(0, QTabBar::RightSide, close);
}
Finally, in the MainWindow, I added my own TabWidget to a layout :
ui->layout->addWidget(new MyTab(this));
The result :
But now you will have to handle the close action manually by connecting the button and get the index for a removeTab(index) call.
I am doing the same thing as you do, here is my stylesheet:
QTabBar::close-button{
image:url(:tabclose.png);
margin-right:4px;
}
Do not use "width" and "height" property, those two doesn't work here, setting a "image:url()" on sub controls implicitly sets the width and height of the sub-control (unless the image in a SVG).
Use "margin-right" property to control the distance from the right edge of the tab;
Add a custom button is a good answer.but if you use margin to decide close-button's position,the close-button's mouse area will be abnormal,so I add a SpacerItem and button in a widget,finally add this widget to TabWidget.
void TabBarCloseable::tabInserted(int index)
{
QWidget *widget = new QWidget(this);
QHBoxLayout *layout = new QHBoxLayout(this);
widget->setLayout(layout);
QToolButton *closeBtn = new QToolButton(this);
layout->addWidget(closeBtn);
layout->insertSpacing(1, 15);
closeBtn->setStyleSheet("max-height: 16px; max-width: 16px;");
this->setTabButton(index, QTabBar::RightSide, widget);
QTabBar::tabInserted(index);
}
You have wrong padding
Top
QTabBar::tab {
min-width: 25ex;
padding: 10px 50px 10px 10px;
}
buttom
QTabBar::tab {
min-width: 25ex;
padding: 10px 0px 10px 10px;
}
This is a pure stylesheet solution, without creating the buttons manually:
QTabBar::close-button {
image: url(:/tab-close.png);
padding-left: -13px;
}
If you check the Qt source, the image painting code uses only the padding values, not the margin values.

Resources