I am using QLabel widgets to display error messages to the user in the status bar. This is working fine with the following code;
self.statusbar = self.statusBar()
label = QtGui.QLabel("this is a test error message")
stylesheet = """
QLabel {
font-weight: bold;
color: #FF0000;
}
"""
label.setStyleSheet(stylesheet)
self.statusbar.addWidget(label)
The only problem is that the widgets have a border around them that I can not get rid of. This is not functionally a problem as the message is still visible but it does look rather ugly and I'd like to get rid of it. I can not work out where it is coming from. Whether it is something I need to set on the statusbar or the widget. I have tried modifying the stylesheet for both the statusbar and label to add "border: 0px" to no avail. I have tried setting the labels frame to label.setFrameShape(QtGui.QFrame.NoFrame) but that doesnt seem to be it either.
Anyone have any ideas how I can get rid of it?
You do this with Style sheets. You probably have a line like this
Application app(argc, argv);
underneath that, add one like this:
app.setStyleSheet("QStatusBar::item { border: 0px solid black }; ");
and those pesky boxes will be gone.
try using self.statusbar.showMessage('this is a test error message'), as the QStatusBar is not designed for showing labels. If you need more frexibility than this you may consider subclassing QStatusBar and changing its paintEvent function to special-case labels. Either of these approaches will be much easier to maintain than setting stylesheets for each label you want to but on there anyway, but as usual, YMMV.
for more info check out the manual page for QStatusBar
Related
When my QDoubleSpinBox is focused, it gets a blue outline to it (in "Fusion" style):
How do I turn this off?
Doing this with stylesheets only is doable, but has an important drawback: styling complex widgets like a QSpinBox requires to correctly set all sub control properties.
The basic solution is to set the border for the widget:
QSpinBox {
border: 1px inset palette(mid);
border-radius: 2px;
}
Keep in mind that offering proper visible response of the focus is really important; you might not like the "glow" (and color) the Fusion style offers, but nonetheless it should always be visible when a widget has focus or not, even if it has a blinking text cursor. You can do that by specifying a slightly different color with the :focus selector:
QSpinBox:focus {
border: 1px inset palette(dark);
}
Unfortunately, as explained in the beginning, this has an important drawback: as soon as the stylesheet is applied, the widget painting falls back to the basic primitive methods (the spinbox on the right uses the stylesheet above):
Unfortunately, there's almost no direct way to restore the default painting of the arrows, as using the stylesheet prevents that. So, the only solution is to provide the properties for the controls as explained in the examples about customizing QSpinBox.
There is an alternative, though, using QProxyStyle. The trick is to intercept the control in the drawComplexControl() implementation and remove the State_HasFocus flag of the option before calling the default implementation.
In the following example, I also checked the focus before removing the flag in order to provide sufficient visual feedback, and I also removed the State_MouseOver flag which shows the glowing effect when hovering.
class Proxy(QtWidgets.QProxyStyle):
def drawComplexControl(self, cc, opt, qp, widget=None):
if cc == self.CC_SpinBox:
opt = QtWidgets.QStyleOptionSpinBox(opt)
if opt.state & self.State_HasFocus:
opt.palette.setColor(QtGui.QPalette.Window,
opt.palette.color(QtGui.QPalette.Window).darker(100))
else:
opt.palette.setColor(QtGui.QPalette.Window,
opt.palette.color(QtGui.QPalette.Window).lighter(125))
opt.state &= ~(self.State_HasFocus | self.State_MouseOver)
super().drawComplexControl(cc, opt, qp, widget)
# ...
app = QtWidgets.QApplication(sys.argv)
app.setStyle(Proxy())
# ...
Note that the above "color correction" only works for Fusion style and other styles that use the Window palette role for painting the border. For instance, the Windows style doesn't consider it at all, or you might want to use higher values of darker() or lighter() in order to provide better differentiation.
I'm adding a button to the application 'Nuke'. I've added a QToolButton, and now I want to style it. I removed the border, and now I want to add a :hover.
I seem to be able to only set one stylesheet. How would I go about adding a second selector, given I can only use one stylesheet?
This will override the top one:
snapshotToolButton.setStyleSheet("#SnapShotButton {border : none;}")
snapshotToolButton.setStyleSheet("#SnapShotButton:hover {background-color : yellow;}")
This is my code:
snapshotToolButton = QtWidgets.QToolButton()
snapshotToolButton.setObjectName("SnapShotButton")
snapshotToolButton.setStyleSheet("#SnapShotButton {border : none;}")
snapshotToolButton.setBaseSize(12,12)
snapshotToolButton.setIcon(QtWidgets.QIcon("C:/Users/nfran/.nuke/icons/cameraIcon.png"))
snapshotToolButton.setToolTip("Take Snapshot")
c.parentWidget().layout().insertWidget(0,snapshotToolButton)
The method "setStyleSheet" is SETTING the Stylesheet, not adding a new one, so you keep overriding the one you already set.
To specify multiple styles, try the following:
snapshotToolButton.setStyleSheet("#SnapShotButton {border: none;} #SnapShotButton:hover { background-color: pink; }")
I'm trying to change the background color of the QLineEdit and I can't figure it out at all.
I tried using stylesheets originally like this
QLineEdit *le = new QLineEdit();
le->setStyleSheet("background:#000;");
but that didn't do anything. I tried using QPalette like this
QPalette palette;
palette.setColor(QPalette::Base, Qt::black);
palette.setColor(QPalette::Background, Qt::black);
le.setPalette(palette);
but this didn't do anything either. I've been looking all day and can't find anything. am I doing something wrong or is there another way to do it?
You can set the background and text colors of line edit by setting the palette like :
QLineEdit *le = new QLineEdit();
QPalette palette;
palette.setColor(QPalette::Base,Qt::black);
palette.setColor(QPalette::Text,Qt::white);
le->setPalette(palette);
Works fine for me:
QLineEdit *le = new QLineEdit();
le->setStyleSheet("QLineEdit { background: rgb(0, 255, 255); selection-background-color: rgb(233, 99, 0); }");
Your code is almost correct. Only QLine edit uses the Base color. So if you do not want to replace existing stylesheet which can contain borders padding and margin and you want to change background only, use QPalette:
QPalette palette = _ui->lnSearch->palette();
palette.setColor(QPalette::Base, Qt::green);
_ui->lnSearch->setPalette(palette);
Thanks to: https://forum.qt.io/topic/64568/why-setting-background-color-of-qlineedit-has-no-effect
I had to use background-color from standard css like this:
QLineEdit* edit = new QLineEdit();
edit->setStyleSheet("QLineEdit {background-color: black;}");
I am using Qt 5.4
Please excuse my english.
I'm trying to change the background color of a GtkButton using a css file but I can't.
I tried a few examples I found on the web, but none work.
I post two examples. One in Python 3.2.3 and the other in C
I'm using Gtk+ 3.6 and Kubuntu 12.10.
This is the code of one of them:
from gi.repository import Gtk, Gdk
class MainWindow(Gtk.Window):
def __init__(self):
super().__init__()
vbox = Gtk.Box(spacing=10,orientation=Gtk.Orientation.VERTICAL)
self.add(vbox)
self.entries = [ Gtk.Entry() for i in range(3) ]
for e in self.entries:
vbox.pack_start(e, True, True, 0)
e.connect("changed", self.on_entry_changed)
e.set_text('123')
button=Gtk.Button(label='ok')
vbox.pack_end(button,True,True,0)
def on_entry_changed(self,entry):
ctx = entry.get_style_context()
if not entry.get_text().isnumeric():
ctx.add_class('invalid')
else:
ctx.remove_class('invalid')
cssProvider = Gtk.CssProvider()
cssProvider.load_from_path('style.css')
screen = Gdk.Screen.get_default()
styleContext = Gtk.StyleContext()
styleContext.add_provider_for_screen(screen, cssProvider,
Gtk.STYLE_PROVIDER_PRIORITY_USER) # With the others GTK_STYLE_PROVIDER_PRIORITY values get the same result.
window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
and the style.css
GtkEntry.invalid {
background-color: #ffaaaa;
background: #ffaaaa;
}
GtkButton {
engine: oxygen-gtk; /*tried also with 'none' and without setting engine*/
background-color: green;
background: green;
}
The entries works well... the bg color change. But the Button no, and theres no error messages.
EDIT3: (Deleted previews edits and change some tags)
Summarizing... I tried to change the button color with all the Python, C, and C++ codes I found in the web unsuccessfully. I read all the tutorials I found and the GTK+ 3 reference manual.
All that I know after that is that the problem is about Kubuntu themes: If I change the GTK theme from 'oxygen-gtk' to 'default' (in GTK Configuration), is the only way I found that the code works well, but this is not the idea and the button looks horrible.
So, the questions are:
Why I can't change the background color of the button?
Why I having this problem only with buttons? (Works well with other widgets)
I get answers here and in GTK forums saying that is not a good practice to change button colors, but... What if I want a menu like the one in this image (link) (see red box buttons)? Wich is the best practis for that?
Thanks and greetings!
I know this is quite old, but popped up in the first few google results, so I thought I'd share my experience.
Gtk.Button has an inline Gtk.Label for the button text, that doesn't inherit from the button by default, so you have to explicitly tell it to (or just specify the colour in it):
GtkButton GtkLabel {
color: #fff; /* This changes the text color in the button */
}
As far as the answer from #sciamp, the GTK theme sets an image for the background and the borders as well, so you have to manually remove that with background-image: none; border-image: none; Hope this saves someone the struggle.
This should work (I mean it's working for me!):
GtkButton {
border-image: none;
background-image: none;
background-color: green;
}
This is complicated, but I don't think it can be done, directly.
I believe the core reason is because the button doesn't render the background. All it does is rendera frame around its area, and then render any children inside. Remember that GtkButton is a container, it typically holds a GtkLabel for a textual label but can hold any widgetry.
I've managed to change the background color of textual labels, but then only the much tigher box around the text itself is affected, which is not what you want.
The indirect solution is to subclass the GtkButton to create a variant which actually does render its background. This is, to be sure, pretty rude towards themes and should be avoided.
I'm trying to customize my UI. I don't know how to make a style sheet entry pertain to anything except the parent and ALL of the children. For example I run this line:
self.lnchTab.setStyleSheet('QWidget { background-color: #1d1d1d ; color: #f8f8f8}')
And I change ALL of the elements beneath self.lnchTab to be darkish grey. I want only the self.lnchTab to be dark grey, and not the text, inputs, and buttons within it.
How do I accomplish this?
Thanks for any help. Google is giving nothing useful or even near what I'm trying to find.
You can give self.lnchTab object some name/id and then you can use id-selector in style-sheet:
self.lnchTab.setObjectName("myParentWidget");
self.lnchTab.setStyleSheet('QWidget#myParentWidget { background-color: #1d1d1d ; color: #f8f8f8}')