Pyqt: How to change QGroupBox title color? - python

I have a QGroupBox which objectName is "of_esq". How do I change the color of its title?
Is it something like: self.of_esq.setStyleSheet("of_esq.title {color: green}")?

If you're setting the stylesheet directly on the of_esq widget, you could just do (assuming it has no child QGroupBoxes
self.of_esq.setStyleSheet('QGroupBox {color: green;}')
You could also set the stylesheet on of_esq's parent and reference the widget by name using the css id selector (#)
self.setStyleSheet('#of_esq {color: green;}')

Late to the party, but here's how I did this:
First, I created my UI in Qt Designer, which contained a QGroupBox. I spent an inordinate amount of time trying to find a color property within Qt Designer which would ONLY affect the QGroupBox title. Finally, I resorted to modifying the converted ui code (heeding the "WARNING! All changes made in this file will be lost!" given by the conversion utility)
Next, I located the code where the stylesheet was being set on the groupBox widget:
self.groupBox.setStyleSheet("")
Next, I modified this code, "drilling down" to the title to set the color:
self.groupBox.setStyleSheet('QGroupBox:title {color: rgb(1, 130, 153);}')
This seems to work on other widgets as well using Brendan Abel's first example. Just be sure you specifically specify the exact widget.
To verify, I added three radio buttons to my layout (within the QGroupBox). After saving, I regenerated the Python ui code. Then I modified just ONE of the radio buttons:
self.radioButton_2.setStyleSheet('QRadioButton {color: rgb(1, 130, 153);}')
This ONLY changed the text color of the radioButton_2 widget.
It would be MUCH more convenient having the widget color properties available in Qt Designer, but this seems to be a viable workaround. Just be aware that regenerating the ui code after making changes in Qt Designer will overwrite your modified code.

Related

Rescaling the absolute coordinate x,y values of self items in order to resize items fit into different resolutions [duplicate]

I want to have a small QFormLayout that grows to fill its parent widget.
I created a new .ui file using the QWidget template in Qt Designer. I put a QFormLayout inside that 'window', then put some controls inside that QFormLayout.
This all works reasonably well, but the QFormLayout always stays at the size I set in Qt Designer. I would like the QFormLayout to fill its parent widget and grow/shrink with it.
How can I accomplish that?
In Designer, activate the centralWidget and assign a layout, e.g. horizontal or vertical layout.
Then your QFormLayout will automatically resize.
Always make sure, that all widgets have a layout! Otherwise, automatic resizing will break with that widget!
See also
Controls insist on being too large, and won't resize, in QtDesigner
I found it was impossible to assign a layout to the centralwidget until I had added at least one child beneath it. Then I could highlight the tiny icon with the red 'disabled' mark and then click on a layout in the Designer toolbar at top.
The accepted answer (its image) is wrong, at least now in QT5. Instead you should assign a layout to the root object/widget (pointing to the aforementioned image, it should be the MainWindow instead of centralWidget). Also note that you must have at least one QObject created beneath it for this to work. Do this and your ui will become responsive to window resizing.
You need to change the default layout type of top level QWidget object from Break layout type to other layout types (Vertical Layout, Horizontal Layout, Grid Layout, Form Layout).
For example:
To something like this:

PyQt: display QPushButton that looks like the down-arrow button in QComboBox

In PyQt4, I want to present a QPushButton that looks like the down-arrow button in QComboBox. Feasible, and if so, how?
I don't need help getting my new widget-combination acting like a QComboBox (see below). I only want the QPushButton display/graphic to look like the down-arrow button in a QComboBox - and tips/code on how to overlay that graphic (especially if said graphic comes via a file) onto my own QPushButton.
More details, context:
I'm seeking to replace a QComboBox widget with a QLineEdit + QCalendarWidget, because QDateEdit isn't as customizable as I need (I think...). The thought is to place a QPushButton immediately adjacent (on the right-side) of the QLineEdit to make it things look like regular QComboBox as much as possible. Then said button will .exec_() the QCalendarWidget (which is technically wrapped by a QDialog).
Let me know if this doesn't make sense, and I can provide further or clarified context.
You can try a QToolButton with no text and the arrowType property set to Qt.DownArrow. eg: myqtoolbutton.setArrowType(Qt.DownArrow).

PySide using Style Sheets to change QtoolBox

I'm Wondering if somebody knows a better reference than the QT one for style sheets.
I try to change the background of the content from a QToolBox but background-color only changes the header of the toolbox and the background of the content it reveals is left in a standard color.
I then tried to change the bg of QWidget which worked but left me with problems now at Radiobuttons and Checkboxes, i also think changing QWidget isn't a good solution.
The first picture is with Qt standard colors (this is a maya tool)
the second when i change QWidget
http://i.imgur.com/MNlSedx.png?1
http://i.imgur.com/wuplpmw.png?1
I then tried to change the bg of QWidget which worked but left me with
problems now at Radiobuttons and Checkboxes, i also think changing
QWidget isn't a good solution.
No man, It is petty good solution. In your QToolBox stylesheet, your have use QWidget {} it's equivalent to *[class~="QWidget "] so this problem is in subclass has been hanged. To fix it use access specified class like this *[class="QWidget"] {}. Example;
*[class="QWidget"] {
background-color: rgb(0, 0, 255);
}
Useful Qt Stylesheet Reference

Keep system theme with stylesheet

I try to make disappear the borders of a QTableView, with a stylesheet:
self.tableau.setStyleSheet("border: 0px solid transparent")
But when I do that, I loose the system theme on my widget, especially for the scroll bar. I'm on Linux. Any idea why this is happening ? Do I have to add another css option ?
If you need to keep the original style of the QTableView you can use setFrameShape(QFrame::NoFrame) or setLineWidth(0). QTableView ingerits those methods from QFrame. If you want to do it with stylesheet I think there is no way to keep the original OS theme.

pyqt: controlling a border widget

In pyqt: how to put a border frame around a widget I created a border around a widget. I'd now like to be able to control that border from the underlying program.
1) The base widget has a class name in Qt Designer and uses that class name in the program. How do I give a class name to my border widget?
2) I've set background color and margins in Qt Designer for the border widget. How do I set these in the program (overriding the Qt Designer settings)?
The name of the widget can be set in the Property Editor with the objectName property. The class name (e.g. QWidget) cannot be changed.
The background colour can be set using the setStyleSheet method.
Tip: A lot of these kinds of questions can answered by using pyuic4 to convert the ui file to a python module, so you can "see how its done". To do this, run the following command in a console:
pyuic4 -o output.py source.ui
The generated code is usually a lot more verbose than it needs to be, but it should give you a good idea of how to proceed.

Categories

Resources