Accessing QGraphicsItem's parentItem causes crash - python

Why when I try to access the qgraphics item's parentItem does it cause the program to crash unexpectedly? I've tried to make a super simple example of the issue and it's certainly reproducible. The only article i found online relating to this crash is here: https://www.qtcentre.org/threads/63345-Occasional-crash-related-to-QGraphicsItem-parentItem which talks about prepareGeometryChange() which I've added but does not fix the issue.
To recreate the crash just uncomment this line of code print self.parentItem()
import os, sys, uuid
from Qt import QtWidgets, QtGui, QtCore
class KnotNodeItem(QtWidgets.QGraphicsEllipseItem):
def __init__(self, parent=None):
super(KnotNodeItem, self).__init__(parent=parent)
self.setAcceptHoverEvents(True)
self.setFlags(self.ItemSendsScenePositionChanges | self.ItemIsSelectable | self.ItemIsMovable)
self.setCursor(QtCore.Qt.PointingHandCursor)
# call initial update to draw text with correct visuals from base class
self.update()
# Overrides
def boundingRect(self):
rect = self.rect()
rect.adjust(-1,-1,1,1)
return rect
def itemChange(self, change, value):
if change == self.ItemScenePositionHasChanged:
pass
# print self.parentItem()
return super(KnotNodeItem, self).itemChange(change, value)
def update(self):
self.prepareGeometryChange()
self.setRect(-12, -12, 12*2, 12*2)
super(KnotNodeItem, self).update()
def paint(self, painter, option, widget=None):
painter.save()
painter.setRenderHint(QtGui.QPainter.Antialiasing)
if self.isSelected():
painter.setPen(QtGui.QPen(QtGui.QColor(255,255,255), 4, QtCore.Qt.SolidLine))
else:
painter.setPen(QtGui.QPen(QtGui.QColor(30,30,30), 4, QtCore.Qt.SolidLine))
painter.setBrush(QtGui.QColor(128,128,255))
painter.drawEllipse(self.rect())
painter.restore()
class MainWindowUi(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(*args, **kwargs)
self.resize(400,400)
self.scene = QtWidgets.QGraphicsScene(self)
self.scene.setBackgroundBrush(QtGui.QBrush(QtGui.QColor(45,45,45), QtCore.Qt.SolidPattern));
self.view = QtWidgets.QGraphicsView(self)
self.setCentralWidget(self.view)
self.view.setScene(self.scene)
# Create various KnotNodeItems
item = KnotNodeItem()
item.setPos(300,20)
self.scene.addItem(item)
if __name__ == '__main__':
a = QtWidgets.QApplication(sys.argv)
m = MainWindowUi()
m.show()
sys.exit(a.exec_())

Related

How to paint() with QStyledItemDelegate

I am using PySide2 and I cant find any documentation on how to use the paint() function in a QStyledItemDelegate subclass. I am rather new to classes but is so far understandable but having trouble with PySide2.
I would like to replace my QtWidgets.QListWidgetItem with my own ListWidgetItem and display them correctly, like this:
So on the left of the ListWidgetItem an icon a bit to the right the name of the ListWidgetItem and underneath the description.
Here is the code:
from PySide2 import QtWidgets, QtCore, QtGui
from PySide2.QtGui import *
import sys
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setWindowTitle('Test Window')
self.setStyleSheet("background-color: rgb(65, 65, 65);")
mainWidget = QtWidgets.QWidget(self)
self.setCentralWidget(mainWidget)
self.boxLayout = QtWidgets.QVBoxLayout()
mainWidget.setLayout(self.boxLayout)
# Add Widgets
self.textField = QtWidgets.QLineEdit()
self.listView = QtWidgets.QListWidget()
self.textField.textChanged.connect(self.onTextChanged)
self.boxLayout.addWidget(self.textField)
self.boxLayout.addWidget(self.listView)
self.textField.setFocus()
def onTextChanged(self, ):
titles = ['Monkey', 'Giraffe', 'Dragon', 'Bull']
descriptions = ['Almost a homo sapiens sapiens', 'I am a Giraffe!', 'Can fly and is hot on spices', 'Horny...']
if self.textField.text() == '' or self.textField.text().isspace() or self.textField.text() == ' ':
if self.listView.count() > 0:
self.listView.clear()
else:
if self.listView.count() > 0:
self.listView.clear()
for x in range(len(titles)):
if self.textField.text() in titles[x]:
item = ListWidgetItem(titles[x])
self.listView.addItem(item)
self.listView.setCurrentRow(0)
continue
class ListWidgetItem(QtWidgets.QListWidgetItem):
def __init__(self, title = '', description = '', icon = QtGui.QIcon()):
super(ListWidgetItem, self).__init__()
self.title = title
self.description = description
self.icon = icon
class ListViewStyle(QtWidgets.QStyledItemDelegate):
def __init__(self, parent, itemModel):
super(ListViewStyle, self).__init__(parent)
self.itemModel = itemModel
def sizeHint(self, option, index):
if index:
return QtCore.QSize(40, 40)
def paint(self, painter, option, index):
super(ListViewStyle, self).paint(painter, option, index)
if __name__ == '__main__':
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
#sys.exit(app.exec_())
Info: In onTextChanged() the ListWidgetItem will be added to the QListWidget but not drawn correctly, basically empty.
Does QListWidgetItem have any notable difference to QListView?
The delegate only paints and is not interested in what element provides the information since that class uses the QModelIndex and the same model to obtain the information, so in my previous solution I used a QStandardItemModel that uses QStandardItem and in your current case a QListWidget with QListWidgetItem is indifferent. My delegate expects only that the information of the title, description and icon are related to TitleRole, DescriptionRole and IconRole, respectively.
On the other hand it is not good to delete the items but it is better to hide or make them visible when necessary.
Considering the above, the solution with QListWidget is as follows:
import sys
from PySide2 import QtWidgets, QtCore, QtGui
TitleRole = QtCore.Qt.UserRole + 1000
DescriptionRole = QtCore.Qt.UserRole + 1001
IconRole = QtCore.Qt.UserRole + 1002
class ListWidgetItem(QtWidgets.QListWidgetItem):
def __init__(self, title="", description="", icon=QtGui.QIcon()):
super(ListWidgetItem, self).__init__()
self.title = title
self.description = description
self.icon = icon
#property
def title(self):
return self.data(TitleRole)
#title.setter
def title(self, title):
self.setData(TitleRole, title)
#property
def description(self):
return self.data(DescriptionRole)
#description.setter
def description(self, description):
self.setData(DescriptionRole, description)
#property
def icon(self):
return self.data(IconRole)
#icon.setter
def icon(self, icon):
self.setData(IconRole, icon)
class StyledItemDelegate(QtWidgets.QStyledItemDelegate):
def sizeHint(self, option, index):
return QtCore.QSize(50, 50)
def paint(self, painter, option, index):
super(StyledItemDelegate, self).paint(painter, option, index)
title = index.data(TitleRole)
description = index.data(DescriptionRole)
icon = index.data(IconRole)
mode = QtGui.QIcon.Normal
if not (option.state & QtWidgets.QStyle.State_Enabled):
mode = QtGui.QIcon.Disabled
elif option.state & QtWidgets.QStyle.State_Selected:
mode = QtGui.QIcon.Selected
state = (
QtGui.QIcon.On
if option.state & QtWidgets.QStyle.State_Open
else QtGui.QIcon.Off
)
iconRect = QtCore.QRect(option.rect)
iconRect.setSize(QtCore.QSize(40, 40))
icon.paint(
painter, iconRect, QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter, mode, state
)
titleFont = QtGui.QFont(option.font)
titleFont.setPixelSize(20)
fm = QtGui.QFontMetrics(titleFont)
titleRect = QtCore.QRect(option.rect)
titleRect.setLeft(iconRect.right())
titleRect.setHeight(fm.height())
color = (
option.palette.color(QtGui.QPalette.BrightText)
if option.state & QtWidgets.QStyle.State_Selected
else option.palette.color(QtGui.QPalette.WindowText)
)
painter.save()
painter.setFont(titleFont)
pen = painter.pen()
pen.setColor(color)
painter.setPen(pen)
painter.drawText(titleRect, title)
painter.restore()
descriptionFont = QtGui.QFont(option.font)
descriptionFont.setPixelSize(15)
fm = QtGui.QFontMetrics(descriptionFont)
descriptionRect = QtCore.QRect(option.rect)
descriptionRect.setTopLeft(titleRect.bottomLeft())
descriptionRect.setHeight(fm.height())
painter.save()
painter.setFont(descriptionFont)
pen = painter.pen()
pen.setColor(color)
painter.setPen(pen)
painter.drawText(
descriptionRect,
fm.elidedText(description, QtCore.Qt.ElideRight, descriptionRect.width()),
)
painter.restore()
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__()
self.setWindowTitle("Test Window")
self.setStyleSheet("background-color: rgb(65, 65, 65);")
mainWidget = QtWidgets.QWidget(self)
self.setCentralWidget(mainWidget)
self.boxLayout = QtWidgets.QVBoxLayout()
mainWidget.setLayout(self.boxLayout)
# Add Widgets
self.textField = QtWidgets.QLineEdit()
self.listView = QtWidgets.QListWidget()
self.textField.textChanged.connect(self.onTextChanged)
self.boxLayout.addWidget(self.textField)
self.boxLayout.addWidget(self.listView)
self.fill_model()
self.textField.setFocus()
self.listView.setItemDelegate(StyledItemDelegate(self))
def fill_model(self):
titles = ["Monkey", "Giraffe", "Dragon", "Bull"]
descriptions = [
"Almost a homo sapiens sapiens",
"I am a Giraffe!",
"Can fly and is hot on spices",
"Horny...",
]
for title, description in zip(titles, descriptions):
it = ListWidgetItem(title=title, description=description)
self.listView.addItem(it)
#QtCore.Slot(str)
def onTextChanged(self, text):
text = text.strip()
if text:
for i in range(self.listView.count()):
it = self.listView.item(i)
if it is not None:
it.setHidden(text.lower() not in it.title.lower())
else:
for i in range(self.listView.count()):
it = self.listView.item(i)
if it is not None:
it.setHidden(False)
if __name__ == "__main__":
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

Adjust GraphicView according to any image geometry

My QGraphicsView should show an image of a large resolution. The size should fit inside a resizable window. Currently, the image is viewed in a way that I want it to but only by providing some manually adjusted values to the initial view geometry. This doe not look neat. I also tried to refer to the solutions posted here: Graphics View and Pixmap Size
My current Window looks like this:
class ImageCheck(Ui_ImageCheck.Ui_MainWindow, QMainWindow):
def __init__(self, parent=None):
super(ImageCheck, self).__init__()
self.setupUi(self)
self.setWindowTitle("Image Analyzer")
self.crop_ratio_w = 1
self.crop_ratio_h = 1
self.path = None
self.scene = QGraphicsScene()
self.scene.clear()
self.image_item = QGraphicsPixmapItem()
# This is the approximate shift in coordinates of my initial view from the window
self.view.setGeometry(self.geometry().x()+ 10, self.geometry().y()+ 39,
self.geometry().width()- 55, self.geometry().height()- 110)
self.view.setAlignment(Qt.AlignCenter)
self.view.setFrameShape(QFrame.NoFrame)
def setImage(self, path):
self.path = path
self.crop_ratio_w = self.pixmap.width() / self.view.width()
self.crop_ratio_h = self.pixmap.height() / self.view.height()
pixmap = QPixmap(path)
smaller_pixmap = pixmap.scaled(self.view.width(), self.view.height(),
Qt.IgnoreAspectRatio, t.FastTransformation)
self.image_item.setPixmap(smaller_pixmap)
self.scene.addItem(self.image_item)
self.scene.setSceneRect(0, 0, self.view.width(), self.view.height())
self.view.setGeometry(0, 0, self.view.width(), self.view.height())
self.view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.view.setScene(self.scene)
self.view.setSceneSize()
def resizeEvent(self, event):
self.view.setGeometry(self.geometry().x()+ 10, self.geometry().y()+ 39,
self.geometry().width()- 55, self.geometry().height()- 110)
self.setImage(self.path)
My manual override was probably not a good idea when I tried to determine distances between two points. Even the scaled distance gives me a slightly wrong value.
I can not use your code because there are many hidden things so I will propose the next solution that is to rescale the view based on the scene each time the window changes its size. I have also implemented a signal that transports the clicked information in the image based on the coordinates of the image.
from PyQt5 import QtCore, QtGui, QtWidgets
class ClickableGraphicsView(QtWidgets.QGraphicsView):
clicked = QtCore.pyqtSignal(QtCore.QPoint)
def __init__(self, parent=None):
super(ClickableGraphicsView, self).__init__(parent)
scene = QtWidgets.QGraphicsScene(self)
self.setScene(scene)
self.pixmap_item = None
def setImage(self, path):
pixmap = QtGui.QPixmap(path)
self.pixmap_item = self.scene().addPixmap(pixmap)
self.pixmap_item.setShapeMode(
QtWidgets.QGraphicsPixmapItem.BoundingRectShape
)
def mousePressEvent(self, event):
if self.pixmap_item is not None:
if self.pixmap_item == self.itemAt(event.pos()):
sp = self.mapToScene(event.pos())
lp = self.pixmap_item.mapToItem(self.pixmap_item, sp)
p = lp.toPoint()
if self.pixmap_item.pixmap().rect().contains(p):
self.clicked.emit(p)
super(ClickableGraphicsView, self).mousePressEvent(event)
def resizeEvent(self, event):
self.fitInView(self.sceneRect(), QtCore.Qt.IgnoreAspectRatio)
super(ClickableGraphicsView, self).resizeEvent(event)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setWindowTitle("Image Analyzer")
view = ClickableGraphicsView()
view.clicked.connect(print)
view.setImage("image.jpg")
label = QtWidgets.QLabel("Distance")
display = QtWidgets.QLCDNumber()
buttonbox = QtWidgets.QDialogButtonBox(
QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel
)
widget = QtWidgets.QWidget()
self.setCentralWidget(widget)
lay = QtWidgets.QGridLayout(widget)
lay.addWidget(view, 0, 0, 1, 2)
hlay = QtWidgets.QHBoxLayout()
hlay.addWidget(label)
hlay.addWidget(display)
hlay.addStretch()
lay.addLayout(hlay, 1, 0)
lay.addWidget(buttonbox, 1, 1)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())

PySide: Position of movable item resets on mouse event

I have problem with PySide (I'm still rookie in programming). I created custom QGraphicsItem called RBNode. It should stay in the position where mouseReleaseEvent has happend. I don't know why but when I'm trying to move the instance of RBNode class more than once, the position of this instance resets. How can I avoid reseting position?
Thanks in advance for your help.
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class RBNode(QGraphicsItem):
def __init__(self, factorView = None):
super(RBNode, self).__init__()
self.factor = factorView
self.pressed = False
self.x = self.pos().x()
self.y = self.pos().y()
self.setFlag(QGraphicsItem.ItemIsMovable)
def boundingRect(self):
return QRectF(-50,-50,100,100)
def paint(self, painter, option, widget):
rect = QRectF(-50,-50,100,100)
if self.pressed:
painter.setBrush(Qt.red)
else:
painter.setBrush(Qt.darkGray)
painter.drawEllipse(rect)
def mousePressEvent(self, event):
self.pressed = True
self.update()
QGraphicsItem.mousePressEvent(event)
def mouseReleaseEvent(self, event):
self.pressed = False
self.update()
QGraphicsItem.mouseReleaseEvent(event)
class RBGraphicView(QGraphicsView):
def __init__(self):
super(RBGraphicView, self).__init__()
self.factorView = 1
self.initScene()
self.initGui()
def initGui(self):
self.setWindowTitle("A Simple Animation")
self.show()
def initScene(self):
self.rbScene = QGraphicsScene(self)
self.rbAddItem(self.rbScene)
self.setScene(self.rbScene)
def rbAddItem(self, scene):
rbNode1 = RBNode(self.factorView)
rbNode1.setPos(100,100)
scene.addItem(rbNode1)
if __name__ == '__main__':
try:
myApp = QApplication(sys.argv)
myView = RBGraphicView()
myApp.exec_()
sys.exit(0)
except NameError:
print("Name Error:", sys.exc_info()[1])
except SystemExit:
print("Closing Window...")
except Exception:
print(sys.exc_info()[1])
You forgot self as parameter in the calls to mousePressEvent, mouseReleaseEvent of the super classes.
QGraphicsItem.mousePressEvent(self, event)
QGraphicsItem.mouseReleaseEvent(self, event)

"QStackedWidget.setCurrentIndex": It does not work or error mark

I'm doing a program with graphical interface using PyQt5 . I want to do is that when the user presses certain button, this change widget and show other options.
For this I decided to use QStackedWidget, and all my interface build it from the QT5 designer.
However, in my code, wanting to determine that my name button "btfr" show me "page_2" of my stackedWidget when pressed, using the QStackedWidget.setCurrentIndex method, this does nothing or make any error.
the code is as follows:
import sys
from PyQt5 import uic
from PyQt5.QtCore import QTimeLine
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
infz = uic.loadUiType("main.ui")[0]
class FaderWidget(QWidget):
def __init__(self, old_widget, new_widget):
QWidget.__init__(self, new_widget)
self.old_pixmap = QPixmap(new_widget.size())
old_widget.render(self.old_pixmap)
self.pixmap_opacity = 1.0
self.timeline = QTimeLine()
self.timeline.valueChanged.connect(self.animate)
self.timeline.finished.connect(self.close)
self.timeline.setDuration(333)
self.timeline.start()
self.resize(new_widget.size())
self.show()
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.setOpacity(self.pixmap_opacity)
painter.drawPixmap(0, 0, self.old_pixmap)
painter.end()
def animate(self, value):
self.pixmap_opacity = 1.0 - value
self.repaint()
class StackedWidget(QStackedWidget):
def __init__(self, parent=None):
QStackedWidget.__init__(self, parent)
def setCurrentIndex(self, index):
self.stack = MyWindowClass()
self.a = self.stack.stackedWidget.currentWidget()
self.b = self.stack.stackedWidget.widget(index)
self.fader_widget = FaderWidget(self.a, self.b)
QStackedWidget.setCurrentIndex(self, index)
print(self, index)
def setPage1(self):
self.setCurrentIndex(0)
def setPage2(self):
self.setCurrentIndex(1)
class MyWindowClass(QStackedWidget, infz):
def __init__(self, parent=None):
global pos, c, f
self.pos = 0
self.c = []
self.f = False
QStackedWidget.__init__(self, parent)
self.setupUi(self)
self.setWindowTitle('SkR')
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyWindowClass()
window.resize(788, 518)
stack = StackedWidget()
window.btfr.clicked.connect(stack.setPage2)
window.btnpx.clicked.connect(stack.setPage1)
window.show()
sys.exit(app.exec_())
What I intend with this code is that the change of widget does so with an effect: "fade out".
If I print the "self " and the "index " receiving QStackedWidget.setCurrentIndex shows the following:
<__main__.StackedWidget object at 0x7fc2eb6b5c18> 0
The number zero is index, and the other element is self
Thank you for your attention, I hope someone can help.
Your question isn't completely clear, but don't you just want:
def setIndex(self, index):
self.setCurrentIndex(index)
However, this is a little redundant as you should able to link the button directly to the setCurrentIndex method and use lambda to pass the index value:
btfr.clicked.connect(lambda: self.setCurrentIndex(2))

Removing a QGraphicsItem's parent

I have a QGraphicsItem called child_item that is a child of another QGraphicsItem called parent_item. I need to unparent child_item from parent_item so that child_item has no parent.
But when I try childItem.setItemParent(None) my script crashes.
Apparently this is because when you remove a QGraphicsItem's parent in this way the item is returned to Qt...
For now I've just created a global_parent QGraphicsItem so that if any item needs to be unparented I will simply parent it under the global_parent and if a QGraphicsItem has the parent global_parent my code will act like it doesn't have a parent but I would like a better solution.
Any ideas please?
Some of my code:
POINT_SIZE = 7
class Test_Box(QGraphicsItem):
# Constants
WIDTH = 50 * POINT_SIZE
HEIGHT = 13 * POINT_SIZE
RECT = QRectF(0, 0, WIDTH, HEIGHT)
CORNER_RADIUS = 1.5 * POINT_SIZE
def __init__(self, position, parent=None):
super(Test_Box, self).__init__(parent)
# Settings
self.setFlags( self.flags() |
QGraphicsItem.ItemIsSelectable |
QGraphicsItem.ItemIsMovable |
QGraphicsItem.ItemIsFocusable |
QGraphicsItem.ItemSendsScenePositionChanges )
self.setPos(position)
def boundingRect(self):
return Test_Box.RECT
def paint(self, painter, option, widget):
# Draw Box
brush = QBrush()
painter.setBrush(brush)
painter.drawRoundedRect(Test_Box.RECT, Test_Box.CORNER_RADIUS, Test_Box.CORNER_RADIUS)
def itemChange(self, change, variant):
super(Test_Box, self).itemChange(change, variant)
if change == QGraphicsItem.ItemScenePositionHasChanged:
self.setParentItem(None)
return QGraphicsItem.itemChange(self, change, variant)
Calling setItemParent and setting the item's parent to another item does work though so I'm using a generic parent in the meantime.
If that setParent is not a typo, then that is your issue. A QGraphicsItem has no setParent method and that should give you an error. You should rather use setParentItem:
children.setParentItem(None)
Edit
I created a test case based on your item:
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
POINT_SIZE = 7
class Test_Box(QGraphicsItem):
# Constants
WIDTH = 50 * POINT_SIZE
HEIGHT = 13 * POINT_SIZE
RECT = QRectF(0, 0, WIDTH, HEIGHT)
CORNER_RADIUS = 1.5 * POINT_SIZE
def __init__(self, position, parent=None):
super(Test_Box, self).__init__(parent)
# Settings
self.setFlags( self.flags() |
QGraphicsItem.ItemIsSelectable |
QGraphicsItem.ItemIsMovable |
QGraphicsItem.ItemIsFocusable |
QGraphicsItem.ItemSendsScenePositionChanges )
self.setPos(position)
def boundingRect(self):
return Test_Box.RECT
def paint(self, painter, option, widget):
# Draw Box
brush = QBrush()
painter.setBrush(brush)
painter.drawRoundedRect(Test_Box.RECT, Test_Box.CORNER_RADIUS, Test_Box.CORNER_RADIUS)
def itemChange(self, change, variant):
super(Test_Box, self).itemChange(change, variant)
if change == QGraphicsItem.ItemScenePositionHasChanged:
self.setParentItem(None)
return QGraphicsItem.itemChange(self, change, variant)
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.scene = QGraphicsScene()
self.item1 = Test_Box(QPointF(0, 0))
self.item2 = Test_Box(QPointF(20, 20))
self.item11 = Test_Box(QPointF(10, 5), self.item1)
self.scene.addItem(self.item1)
self.scene.addItem(self.item2)
self.view = QGraphicsView(self.scene)
self.listItems = QPushButton('list')
self.listItems.clicked.connect(self.printItems)
layout = QHBoxLayout()
layout.addWidget(self.view)
layout.addWidget(self.listItems)
self.setLayout(layout)
def printItems(self):
for item in self.scene.items():
print item, item.parentItem()
app = QApplication(sys.argv)
w = Window()
w.show()
sys.exit(app.exec_())
And it works as expected, but I did find some odd behavior. If I don't keep references for items in the Window class, namely if I do the following then the application crashes when I move items.
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.scene = QGraphicsScene()
item1 = Test_Box(QPointF(0, 0))
item2 = Test_Box(QPointF(20, 20))
item11 = Test_Box(QPointF(10, 5), item1)
self.scene.addItem(item1)
self.scene.addItem(item2)
self.view = QGraphicsView(self.scene)
self.listItems = QPushButton('list')
self.listItems.clicked.connect(self.printItems)
layout = QHBoxLayout()
layout.addWidget(self.view)
layout.addWidget(self.listItems)
self.setLayout(layout)
def printItems(self):
for item in self.scene.items():
print item, item.parentItem()
I don't actually know what happens but looks like the items are garbage collected, which shouldn't happen since addItem would give the ownership to the scene and that should keep the items 'alive'. This may be a bug in PyQt, but I'm not sure.

Categories