How can I set the width of a QTreeWidgetItem? - python

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.

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.

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 to setStyleSheet to QStandardItem

The code below creates a simple QComboBox. But instead of using a "traditional" .addItem('myItemName') method it creates QStandardItem first and then adds it via QComboBox's .model().appendRow(). Since now I can access each QStandardItem individually I wonder if there is a way to assign CSS to each of them (to each QStandardItem) individually. The goal is to customize each item displayed in ComboBox pulldown menu. So far I am only able to assign a single CSS style to entire ComboBox globally.
from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])
class Combo(QtGui.QComboBox):
def __init__(self, *args, **kwargs):
super(Combo, self).__init__()
for each in ['Item_1','Item_2','Item_3','Item_4','Item_5']:
item=QtGui.QStandardItem(each)
self.model().appendRow(item)
tree=Combo()
sys.exit(app.exec_())
It looks like this class has no setStyleSheet method, but you can use setBackground, setForeground and setTextAlignment methods. With QBrush you be able to customize elements. Of course it isn't so powerful as styleSheets but better than nothing.
http://pyqt.sourceforge.net/Docs/PyQt4/qstandarditem.html

Categories

Resources