How to "get" an attribute of a QLabel widget - python

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...

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.

PyQt5: Variables available and modifiable by all children widgets?

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.

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)

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.

Drawing my own object on a layout: problem with subclassing gtk.drawable

I want to draw my own object on a layout, so I'm trying to subclass gdk.drawable,
class link(gtk.gdk.Drawable):
def __init__(self,comp1,comp2,layout):
super(link, self).__init__()
self.x1=comp1.xpos
self.y1=comp1.ypos
self.x2=comp2.xpos
self.y2=comp2.ypos
self.layout=layout
error:
cannot create instance of abstract (non-instantiable) type `GdkDrawable'
I can do it without subclassing drawable using layout.bin_window.draw_line() in a method drawlink() of my link object, but I'm not able to create a custom graphic context gdk.gc for each object and I have to use the layout.get_style() which will be the same for all my links!
def drawlink(self):
gc = self.layout.get_style().fg_gc[gtk.STATE_NORMAL]
gc.line_style=gtk.gdk.LINE_ON_OFF_DASH
gc.line_width=6
self.layout.bin_window.draw_line(gc,self.x1, self.y1, self.x2, self.y2)
this is the reason I want to subclass drawable. if I can use a custom gc without subclassing drawable or(window, pixmap) it would be great.
Thanks
any alternative?
If I understand you correctly, what you want to do is not subclassing gtk.gdk.Drawable but populating a gtk.DrawingArea widget with your own content. The description of that widget is:
The gtk.DrawingArea widget is used for creating custom user interface elements. It's essentially a blank widget containing a gtk.gdk.Window that you can draw on.

Categories

Resources