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..
Related
I'm using QTableView class in my GUI and I would like the selected row to have the same color whether the TableView is active or inactive.
I tried to set this CSS stylesheet to achieve this:
QTableView:!active {
selection-background-color: palette(Highlight);
selection-color: palette(HighlightedText)
}
On Linux, it works just fine, but on Windows 7, when the TableView loses its focus, the text turns black instead of staying white (the background stays blue, so that part is OK). Am I missing something here ?
You also have to style text color, for example just add:
QTableView:!active {
...
selection-color: white;
}
This works well in python
pal = tbl_list.palette()
pal.setColor(QPalette.Inactive, QPalette.Highlight, pal.color(QPalette.Active, QPalette.Highlight))
tbl_list.setPalette(pal)
I'm trying to remove the border glow (please see screenshot below) that appears by default when a JavaFX button is selected:
I also want to do this using CSS, and not declaratively from within the main JavaFX script. However, I am having trouble figuring out what CSS property I need to use (er, set to 0?) in order to remove that border.
To remove the focus ring display from any control from within code:
control.setStyle("-fx-focus-color: transparent;");
To remove the focus ring for all controls, apply a stylesheet:
.root { -fx-focus-color: transparent; }
To only remove the ring for all buttons, use:
.button { -fx-focus-color: transparent; }
I find the -fx-focus-color attribute setting more straight-forward than relying on some weird combination of insets to remove the focus ring.
In addition, you can use the same setting to change the focus ring to a different color, such as -fx-focus-color: firebrick.
Update Jan 20, 2015
JavaFX 8 shipped with a new default user agent stylesheet (modena). This new user agent stylesheet shipped with an additional setting for focus highlights: -fx-faint-focus-color. For Java 8 applications, it is necessary to set both -fx-focus-color and -fx-faint-focus-color to transparent to remove all traces of the focus ring. See good4m's answer to this question.
Update Dec 10, 2015
If you only set the focus colors to transparent as previously recommended in this answer, for some controls you may see some subtle differentiation between when a control is focused and when it is not. For many applications this will not be an issue and setting the focus colors to transparent will be sufficient.
For more information and alternate solutions, review James_D's answer to Remove blue frame from JavaFX input field and Jens Deter's blog post about How to get rid of focus highlighting in JavaFX. Be aware that the link to Jens Deter's blog unfortunately has some annoying pop-ups.
-fx-focus-color: transparent;
-fx-faint-focus-color: transparent;
There is several way to do this. You can try any of this.
button.setStyle("-fx-focus-color: transparent;");
or
.button{
-fx-focus-color: transparent;
}
or
.button:focused{
-fx-focus-color: transparent;
}
If you want to remove this focus ring in JavaFX 8, rewrite the :focus selector with the .button selector style from modena.css.
.button:focused {
-fx-background-color: -fx-outer-border, -fx-inner-border, -fx-body-color;
-fx-background-insets: 0, 1, 2;
-fx-background-radius: 5, 4, 3;
}
The answer from Stelios Adamantidis is correct, which is
.button:focused {
-fx-background-insets: 0, 0, 1, 2;
}
Here is my explanation:
For example the definition
-fx-background-color: red, green, deepskyblue, blue;
seems to define four layers of background colors, with red as the color for the backmost layer.
For example the definition
-fx-background-radius: 0, 1, 4, 10;
sets the radius for all corners for each color layer. Here, the red layer has all corners with the radius of 0, the green layer has all corners with the radius of 1 and so on.
For example the definition
-fx-background-insets: -10, 0, 3, 5;
sets the padding for the color layers. You can also set negative values then the color will be around the control.
The default values for a button seem to be something like this:
.button:focused {
-fx-background-color: <blueGlowingColor>, <?>, <?>, linear-gradient(to bottom, <?>, <?>);
-fx-background-insets: -1, 0, 1, 2;
}
Setting the first value of insets to 0 hides the glowing color behind the second color.
More about JavaFX CSS you can find here:
http://docs.oracle.com/javafx/2/api/javafx/scene/doc-files/cssref.html
To completely disable this button selecting do:
button.setFocusTraversable(false);
It's much cleaner than editing in css.
So I am trying to change only the background color of my MainWindow. When I try to do this using this->setStyleSheet("background-color:black;"); for example it changes the background of everything: child widgets, QTextBoxEdit background, everything.
Is there a way to only change the background of just the main window?
As you know, every QMainWindow has a central widget and by default is named centralwidget.
So the best way of solving this issue is changing the background for that widget.
It's pretty simple when we use a style sheet. In this case would be the following one:
#centralwidget {
background-color: rgb(0, 0, 0);
}
you can use Qt class name before QSS, like
QMainWindow { background-color: rgb(0, 0, 0);}
in your example QMainWindow > QWidget { background-color: rgb(0, 0, 0);} maybe better.
please see http://doc.qt.io/qt-4.8/stylesheet-syntax.html for more information
I have a JavaFX 2 application, where all my buttons use the default style (grey gradient). In some special areas of the application, the background color is red, yellow or green. In these areas, I also have buttons.
Instead of re-styling all the different states (normal, hover, pressed) of the button in all three colors, I'd like to just give the button the tint of the background. Is this possible, and how?
If not, is there a way to easily re-style the base button style, and have the hover and pressed states (pseudo-selectors) automatically derived from this style?
If that's not possible, I'm open for suggestions.. My most important goal is to avoid redundant/duplicate declarations (especially of gradients), in case someone wants to add a different color panel later, or just change the shade of one of the background colors.
CSS for the red panel/button:
#my-red-panel {
-fx-border-width: 1;
-fx-border-radius: 5;
-fx-background-radius: 5;
-fx-smooth: true;
-fx-border-color: rgb(209, 65, 42);
-fx-background-color: rgba(255, 78, 50, 0.89);
}
#my-red-panel .button {
-fx-background: rgba(0, 0, 0, 0); /* Now borders look good, but button is still grey*/
}
My best bet so far, is to use a semi-transparent gradient, like so:
#my-red-panel .button {
-fx-background-color: linear-gradient(rgba(255, 255, 255, 0.3), rgba(0, 0, 0, 0.2));
}
I still have to declare each state, but at least I can change the underlying colors without having to modify each state. The main problem is that this overrides the entire look of the button, so I was hoping for something better... :-/
Not tested, but try experimenting with:
#my-red-panel {
-fx-base: rgba(255, 78, 50, 0.89);
}
or perhaps:
#my-red-panel .button {
-fx-base: ... ;
}
depending on the exact effects you want.
The trick here is that the default css (caspian.css for JavaFX2.2 or modena.css for JavaFX8) use some pre-defined lookup colors. You can dig out the source for these to see how they are used. If you redefine these lookups for a node in the scene graph, the new definition is propagated to all child nodes.
I am trying to make a QListWidget with QListWidgetItems.
I want the QListWidgetItems to have a border and a background e.g. Green.
The selected item should have another background e.g. Red.
I tried to create the border with a stylesheet. This works fine.
But I an not able to set the individual background color of the items anymore.
Below piece of code I am using
QListWidget *listWidget = new QListWidget();
QListWidgetItem *wi = new QListWidgetItem;
wi->setText("greenbg");
wi->setBackgroundColor(Qt::green);
listWidget->addItem(wi);
listWidget->setStyleSheet( "QListWidget::item {border-style: solid; border-width:1px; border-color:black;}");
QListWidgetItem *wi2 = new QListWidgetItem;
wi2->setText("redbg");
wi2->setBackgroundColor(Qt::red);
listWidget->addItem(wi2);
listWidget->show;
This shows the list transparent. When the setStyleSheet line is removed the items are green and red.
What am I doing wrong or is it not possible and should I use a custom Widget?
CSS is overriding the values you set there. Try to set the background color also in CSS:
listWidget->setStyleSheet(
"QListWidget::item {"
"border-style: solid;"
"border-width:1px;"
"border-color:black;"
"background-color: green;"
"}"
"QListWidget::item:selected {"
"background-color: red;"
"}");
Notice that you can specify different style for different states (ie. that item is selected).
Example and other information here.