pyqt: controlling a border widget - python

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.

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:

In QT designer, can I set a text label that is text + a variable from my python script?

as from title, I am setting up a QLabel in QT designer.
I know I can set it up in the designer and then populate it via code.
I still would like to exploit the ease of managing the text via the designer and import a variable value in it.
For instance:
In the designer, I set a QLabel that will be: "Hello, [long text with formatting], this session will last" + $variable_from_Python_code "minutes [long text with formatting]".
Is it possible?
No, that's not what Designer is intended for, and Qt doesn't directly provide such support.
All properties are intended as "static", with the exception of properties directly linked with signals and slot created in the GUI.
A possible solution would be to use a promoted widget (which wouldn't provide a visual result while designing) or a custom widget plugin (which is a pretty complex procedure that would be rather pointless if you just need to update a string).

Pyqt: How to change QGroupBox title color?

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.

PySide - GUI inactive zone on the menu bar

I am coding a GUI in PySide for a couple of month now. This soft is half developed using Qt designer and half hard coded. It is a MainWindow soft with a central widget and a lot of docks.
Recently I realized that the menu bar (and a tiny bit of the task bar) had a zone which was 'inactive', ie the mouse can not access the menus directly. This zone is highlighted in green in the picture.
The menus are working, since when I click on the right half of the configure menu button, I can access the File or Edit menu as shown in the following picture
The code translating the Qt designer output into python code is
call pyside-uic mainWindow.ui -o uiMainWindow.py
call pyside-rcc -o fittingRessources_rc.py pathtoressources\fittingRessources.qrc
I have tried to remove the icon of the soft. Move the task bars (in which case the buttons of the task bar are entirely active again). I have tried to remove the icons of the task bar. Nothing made a difference.
I can provide code if necessary, but first I was wondering if any of you already encountered this issue? and how it solved it? since it seems a bit ackward...
Cheers
The error appeared only when I was adding certain docks to the MainWindow using the following code:
self.variablesDock = VariablesDock(self,self.dataCurve)
where self is mainWindow. The VariablesDock class is the following
class VariablesDock(QWidget):
'''
Allows to define variables
'''
def __init__(self, mainWindow, dataCurve):
'''
Constructor
'''
super(VariablesDock,self).__init__(mainWindow)
self.mainWindow = mainWindow
self.dataCurve = dataCurve
self.variablesDockWidget = QtGui.QDockWidget(mainWindow)
... skip some code ...
self.variablesDockWidget.setWidget(self.dockWidgetContents)
mainWindow.addDockWidget(QtCore.Qt.DockWidgetArea(1), self.variablesDockWidget)
self.variablesDockWidget.setWindowTitle("Variables")
if I change the line super(VariablesDock,self).__init__(mainWindow) into
super(VariablesDock,self).__init__(None)
the error disappeared.
Cheers

PySide : "Dynamic" positionning of buttons on GUI?

I'm using python 2.7.5 on OSX 10.8. I'm learning PySide and trying to build a simple GUI.
I managed to use buttons (WOAAA!) used to chose a path or execute functions :
pathBtn = QtGui.QPushButton("FITS file path", self)
pathBtn.setToolTip('Choose the <b>path</b> to your FITS file')
pathBtn.clicked.connect(essai)
pathBtn.resize(pathBtn.sizeHint())
pathBtn.move(200, 100)
My problem is, when the program is running and I change the size of the window with the mouse cursor, the buttons don't move, don't adapt to the size variation.
I tried to find some answer (hell yeah google) and I understand that "QVBoxLayout" should do what I want (some kind of "dynamic" positionning, don't know if there's a specific name for that), but I didn't understand its syntax nor how to use it...
Any help appreciated!
In Qt widgets, layouts and the widget's size hints determine how things resize. The general procedure to layout a widget would be (for example):
dialog = QDialog()
layout = QVBoxLayout()
label = QLabel('This is a label')
edit = QLineEdit('This is a line edit box')
layout.addWidget(label)
layout.addWidget(edit)
dialog.setLayout(layout)
(*I cannot test this code here at work (no Qt/PySide), so consider this "pseudo code" :-)
This results in a dialog widget with a label and an edit box. If you resize the dialog widget, the layout and the resize properties of the widgets ensure that the label and edit box resize appropriately: horizontally both expand maximally, but vertically the edit will keep the same size while the label takes all the remaining space. This is because the resize hint of the edit box says it wants to keep its height (namely, one line of text).
If you do not specify a layout, your widgets (buttons, labels) don't do anything whenr resizing their parent widget, which is what you are observing. Hence, the solution is indeed the QVBoxLayout, use it as I described above.
By the way: for more complicated layouts, you probably want to use the Designer tool provided with Qt: this tool lets you see and test your GUI a priori.

Categories

Resources