PyQt5: Variables available and modifiable by all children widgets? - python

I have the current flow,
QMainWindow -> QMdiArea -> QMdiSubWindow -> QTabWidget -> QWidget -> QGraphicsScene/View
Now I want to maintain a count of a certain item being added to the scene, in a mdi sub window, Now since there can be multiple subwindows, and each subwindow can have multiple tabs. I was interested if qt had a system of given child widgets access to variables in the parent class, instead of using a self.parent().parent().... chain.

You could maybe set your QObject names with setObjectName. Then using a reference to one of the upper object like QMainWindow, you can get references to certain objects using findChild or findChildren with some expected names.

Related

pyqt what's different QWidget(self) vs QWidget(class name)

I'm little confusing meaning of widgets instance creation in PyQt.
Seems QWidget(self) can create a widget.
But what's the meaning of QWidget(instance name)?
For exmaple,
grid_layout = QGridLayout(self)
test_label = QWidget(grid_layout)
I know test_label created inherited from gridlayout.
But what's the meaning during program meaning?
Is that meaning test_label widget is under grid_layout?
For many of the widgets when the signature is just a single parameter like in both of your examples, the argument given sets the created widget/layouts parent.
So in your first example, using self as the argument means that the parent of the QGridLayout and the widget that the layout will be applied to is whatever widget self is.
In the second example you are setting the parent of QWidget to be the widget represented by it's argument. A QWidget with no parent becomes a new window, when set with a parent then the widget becomes a child window within the parent widget.
Neither one are inheriting anything. Inheritance only occurs during Subclassing.

How to "get" an attribute of a QLabel widget

I understand that QLabel attributes such as frameGeometry, pixmap and text can be recovered using their respective commands. But is it possible to get the value of "frame shadow" around each of these label widgets?
I have 3 Labels placed inside a Frame (inside a Window) using qt-designer. I assigned shadows for each of these labels by calling self.label_1.setFrameShadow(QFrame.Raised) or self.label_1.setFrameShadow(QFrame.Plain) within the QMainWindow class.
Now I wish to update their shadow attributes after checking to see if one of them is Raised or Plain. The error says that: 'QLabel' object has no attribute 'FrameShadow'. But why so if I was able to set it?
QLabel inherits from QFrame and therefore has an accessor frameShadow() for that property.
Unlike in other frameworks, Qt accessors don't start with get...

Add a QGraphicsView to a QGraphicsWidget

I am trying to display a QGraphicsView inside a QGraphicsWidget, but I can't seem to get it to work.
This is what I want to achieve, where the QGraphicsWidgets are all added to a QGraphicsScene, and each widget has their own QGraphicsScene and QGraphicsView.
The large box represents the main view, and the smaller boxes represent widgets, each with their own view and scenes.
My current implementation doesn't work, due to the following error:
TypeError: QGraphicsLinearLayout.addItem(QGraphicsLayoutItem):
argument 1 has unexpected type 'QGraphicsView'
My implemetation in a subclass of the QGraphicsWidget:
self.scene = QtGui.QGraphicsScene(self)
self.view = QtGui.QGraphicsView(self.scene)
self.view.setRenderHint(QtGui.QPainter.Antialiasing)
self.setLayout(QtGui.QGraphicsLinearLayout(Qt.Vertical))
self.layout().addItem(self.view)
I've tried to implement this with a QGraphicsLayoutItem, but I couldn't find anywhere to stick the QGraphicsView in.
Any help would be appreciated.
The function QGraphicsLinearLayout.addItem expects a QGraphicsWidget parameter and you are passing a normal QWidget (QGraphicsView).
You can use the class QGraphicsProxyWidget which is specially designed for wrapping QWidget into QGraphicsWidget.
I do not have Qt + Python on hands but I think it should look as follows:
proxy = parentView.addWidget(nestedView) # QWidget -> QGraphicsWidget
self.layout().addItem(proxy)

How can I set the width of a QTreeWidgetItem?

Does it exist an equivalent function of QTreeView.setColumnWidth() for a QTreeWidgetItem ?
QTreeWidget inherits QTreeView, so you can use any function of QTreeView in a QTreeWidget.

attributes of Mainwindow from childWindow

How can I make a childWindow access and/or modify an attribute from his MainWindow?
I have a MainWindow that opens different childWindows, dependeing on the pressed button on the MainWindow.
I would like any of the childWindows to be able to modify some attributes of the MainWindow, but I cannot get the good way to access them.
When defining your widget or window (your window is possibly just a QWidget), specify it's parent in the init-Method (just pass the parent-widget).
After that, you could use the parentWdiget-Method or simply set a "link" to the parent-widget in an attribute of the child-window.
See http://srinikom.github.io/pyside-docs/PySide/QtGui/QWidget.html
You could pass a reference (self) to your childwindow.

Categories

Resources