I want to set background color of a selected inactive item in a QTreeView using style sheet as follows:
MyTreeView->setStyleSheet("QTreeView::item:selected:!active { background-color: rgb(150, 180, 220) }");
But the code has no effect in the tree view, but the similar code works for QTableView.
Can anyone tell me what I'm missing here?
Thanks in advance.
rgb(150, 180, 220); you need the semi colon inside too.
Related
I am using a QSplitter, but the handle color is not changing, it has always the default color:
m_pSplitMainWin = new QSplitter;
m_pSplitMainWin->setOrientation(Qt::Horizontal);
m_pSplitMainWin->setHandleWidth(10);
m_pSplitMainWin->setStyleSheet("QSplitter::handle:background-color: rgb(55, 100, 110);");
I got some idea from previous post but I am not able to do it with m_pSplitMainWin object
Your CSS syntax is wrong. It should read
QSplitter::handle { background-color: rgb(55, 100, 110); }
I have a QFrame, titled 'bannerframe' embedded in a dialog, with other widgets.
I want to dynamically change the background of the frame. If I use a stylesheet defined in the top level dialog (MessageDlg), it appears correctly, like this: -
The stylesheet is defined as
#bannerFrame
{
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,
stop:0 rgba(195, 40, 9, 255),
stop:0.7 rgba(225, 121, 113, 255),
stop:1 rgba(237, 154, 152, 255));
}
However, if I remove this and add the style directly to the bannerframe widget, the gradient doesn't seem to work properly: -
The same effect is seen, regardless of setting the stylesheet in the Designer, or in code:
QString style = QString("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,\
stop:0 rgba(%1, %2, %3, 255),\
stop:1 rgba(%4, %5, %6, 255));").arg(red1).arg(green1).arg(blue1).arg(red2).arg(green2).arg(blue2);
ui->bannerFrame->setStyleSheet(style);
So, what's going on here?
Why does the gradient not work correctly around the frame's widgets when the stylesheet is set directly on the Frame, rather than on the top-level dialog?
That's probably because the widgets in the frame will inherit their parents stylesheet. So what you see is the QLabel widget with the icon having a gradient background. Add a selector to limit the widgets your styles should apply to.
QString style = QString("QFrame {...}");
or
QString style = QString("QFrame#bannerFrame {...}");
I've had to deal with that too until I discovered that Qt style is decided by the LAST line that sets a style. That means that if you have
something->setStyleSheet(Style1);
something->setStyleSheet(Style2);
something->setStyleSheet(Style3);
Only Style 3 will remain (If they are equal but changing values. If there are diferences, i.e style1 puts text in Bold and style2 puts text in size 14, they will both work BUT if style3 says that text size is 17, then it will be 17.
To avoid that and be able to change it for every type of widget, you need to define for who is that style:
generallayout->setStyleSheet("NAME{css_code}");
like:
myLayout->setStyleSheet("QLineEdit{background-color:#ff0066"};
with that only LineEdits will have that background. In your case, you just need to use QFrame or QFrame#bannerFrame depending on what you want.
I found out that a way to do this is to simply just pass null to setFill(Color color) but that seems like a hacker solution to me. I am wondering if there is a better/proper way to do this?
CubicCurve cubicCurve = new CubicCurve(
50,
75,
80,
-25,
110,
175,
140,
75);
cubicCurve.setStrokeType(StrokeType.CENTERED);
cubicCurve.setStroke(Color.BLACK);
cubicCurve.setStrokeWidth(3);
cubicCurve.setFill(null); //this is in my opinion a hacker solution
That also brings me to my second question, why is the default fill color white for any shape? Is there an efficiency reason for this? What is the point?
A more elegant way to write it would be to assign the fill as TRANSPARENT for a similar effect.
cubicCurve.setFill(Color.TRANSPARENT);
As far as your other questions are concerned, the default fill is Black and not White. I have no idea about the reason behind it. This is how the JavaFX developers decided it to be.
In the Shape.java :
public final Paint getFill() {
return fill == null ? Color.BLACK : fill.get();
}
I'm using a QTreeWidget to show a list of categorized properties. I would like the top level items to be a different background colour; however, the arrow indicating it has children is always the default black on white (Windows 8.1, Qt 5.2.1). Here's how I'm adding the QTreeWidget Item:
QBrush fg(Qt::white);
QBrush bg(Qt::darkGray);
QTreeWidgetItem *header = new QTreeWidgetItem();
header->setText(0, "Sound File");
this->addTopLevelItem(header);
header->setFirstColumnSpanned(true);
header->setData(0, Qt::ForegroundRole, fg);
header->setData(0, Qt::BackgroundRole, bg);
header->setExpanded(true);
Here's a screenshot of how this gets rendered.
How can I give it a solid background across the whole row?
For the indicators you can use StyleSheets something like this:
QTreeWidget::branch::!has-children:selected {background-color: rgb(255, 255, 255);}
QTreeWidget::branch::!has-children:selected:alternate {background-color: rgb(0, 0, 0);}
and set this StyleSheet to the QTreeWidget..
If someone knows any other way to do this for the individual items feel free to share... I have similar situation where I was one of the item that have children to be different color..
I'm trying to change the background color of a QAbstractButton (either a QPushButton or QCheckBox) in Qt5 and having zero luck.
This does nothing:
pButton->setAutoFillBackground(true);
QPalette palette = pButton->palette();
palette.setColor(QPalette::Window, QColor(Qt::blue));
pButton->setPalette(palette);
pButton->show();
and if I try changing the style sheet:
pButton->setStyleSheet("background-color: rgb(255,255,0);");
then Qt throws up its hands and draws an afwul-looking blocky button.
There is a page titled "How to change the background color of QWidget" but it just talks about those two methods.
There is also a page "Qt Style Sheets Examples" that implies that if you want to change the background color, you have to take over all aspects of drawing the button, which just seems like overkill.
I need this to run on Mac, Windows, and Ubuntu Linux, and it's really not a happy thing if I have to manually draw everything about the button 3 times (once for each platform).
Am I missing something obvious?
p.s. By "background color" I mean the area surrounding the button, not the color under the text on the face of the button.
I had the same issue, but finally got this to work. I'm using Qt 5 with the Fusion color theme:
QPalette pal = button->palette();
pal.setColor(QPalette::Button, QColor(Qt::blue));
button->setAutoFillBackground(true);
button->setPalette(pal);
button->update();
Try these commands in the exact order as above, and if that still doesn't work, set your theme to Fusion and try again.
Good luck!
Add propoerty border:none; to the stylesheet
For some reason this removes the default background color also.
Example: background-color: rgba(46, 204, 113, 0.4); border: none;
Try this:
QColor col = QColor(Qt::blue);
if(col.isValid()) {
QString qss = QString("background-color: %1").arg(col.name());
button->setStyleSheet(qss);
}
as mentioned at the QT Forum by #goetz.
I used some different definition of Qcolor col as QColor col = QColor::fromRgb(144,238,144); and this works for me.
Changing the Dialog styleSheet Property works for me, set this property to:
QPushButton:pressed {
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(60, 186, 162, 255), stop:1 rgba(98, 211, 162, 255))
}
QPushButton {
background-color: #3cbaa2; border: 1px solid black;
border-radius: 5px;
}
QPushButton:disabled {
background-color: rgb(170, 170, 127)
}
I found a stupid way, tried every attribute in palette, and it works for me when changing 'QPalette::Base'.
Maybe you can have a try.
pButton->setAutoFillBackground(true);
QPalette palette = pButton->palette();
//palette.setColor(QPalette::Window, QColor(Qt.blue));
//palette.setColor(QPalette::Foreground, QColor(Qt.blue));
palette.setColor(QPalette::Base, QColor(Qt.blue));
//palette.setColor(QPalette::AlternateBase, QColor(Qt.blue));
//palette.setColor(QPalette::ToolTipBase, QColor(Qt.blue));
//palette.setColor(QPalette::ToolTipText, QColor(Qt.blue));
//palette.setColor(QPalette::Text, QColor(Qt.blue));
//palette.setColor(QPalette::Button, QColor(Qt.blue));
//palette.setColor(QPalette::ButtonText, QColor(Qt.blue));
//palette.setColor(QPalette::BrightText, QColor(Qt.blue));
pButton->setPalette(palette);
pButton->show();
reference link : How to get a stylesheet property?
I've struggled with same problem. You need to choose QPalette::Button instead of QPalette::Window. Qt reference says this: QPaletteButton - The general button background color. This background can be different from Window as some styles require a different background color for buttons.
So I solved it this way:
QPalette pal=this->palette(); \\"this" is my derived button class
pal.setColor(QPalette::Window, style.background);
QColor col=style.background; \\ my style wrapper, returns QColor
this->setAutoFillBackground(true);
this->setPalette(pal);
I want to toggle the color of a button On/Off.
This works for me ...
QPalette pal = ui->pushButton->palette();
if (bIn)
pal.setColor(QPalette::Button, QColor(Qt::green));
else
pal.setColor(QPalette::Button, QColor(Qt::red));
ui->pushButton->setPalette(pal);
ui->pushButton->setAutoFillBackground(true);
ui->pushButton->repaint();
I flip the value of bIn on a Clicked Signal/Slot.
void BtnFrame::on_pushButton_clicked()
{
if (bIn)
bIn=false;
else
bIn=true;
setColor();
}