How to avoid Mayavi pipeline pollution? - python

Below is some minimal code that fully demonstrates what I call "pipeline pollution". Each time you press the 'Draw' button, the MayaviScene editor (accessed via the top-left button on the figure) will update the figure, but also create a new scene's "shell" that lingers in the pipeline (as seen in the attached image).
I'm worried that in my more complex project, this pileup will have adverse effects.
Can someone please guide me on how to best set up this Mayavi scene to simply be updated without excess accumulation? I've read through tons of online materials, but still don't understand the developer's logic.
import sys, os
import numpy as np
from pyface.qt import QtGui, QtCore
os.environ['ETS_TOOLKIT'] = 'qt4'
from traits.api import HasTraits,Instance,on_trait_change
from traitsui.api import View,Item
from mayavi import mlab
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, SceneEditor
class Mayavi_Scene(HasTraits):
scene = Instance(MlabSceneModel, ())
def update_scene(self):
Mayavi_Scene.fig1 = mlab.figure(1, bgcolor=(.5,.5,.5))
self.scene.mlab.clf(figure=Mayavi_Scene.fig1)
splot = mlab.points3d(P1.x, P1.y, P1.z,
scale_factor=0.05, figure=Mayavi_Scene.fig1)
view = View(Item('scene', editor = SceneEditor(scene_class=MayaviScene),
height=300, width=300, show_label=False),
resizable=True,
)
class P1(QtGui.QWidget):
# data starts out empty, wait for user input (below, via 'draw()'):
x = []
y = []
z = []
def __init__(self, parent=None):
super(P1, self).__init__(parent)
layout = QtGui.QGridLayout(self)
layout.setContentsMargins(20,20,20,20)
layout.setSpacing(10)
self.viz1 = Mayavi_Scene()
self.ui1 = self.viz1.edit_traits(parent=self, kind='subpanel').control
layout.addWidget(self.ui1, 0, 0, 1, 1)
def draw(): #a sample user input, could have been a custom data file, etc.
P1.x = np.random.random((100,))
P1.y = np.random.random((100,))
P1.z = np.random.random((100,))
Mayavi_Scene().update_scene()
#repeated presses pollute MayaviScene pipeline
# button to draw data:
self.btn1 = QtGui.QPushButton('Draw',self)
self.connect(self.btn1, QtCore.SIGNAL('clicked()'), draw)
layout.addWidget(self.btn1, 1, 0, 1, 1)
self.btn1.show()
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.window = P1(self)
self.setCentralWidget(self.window)
self.show()
if __name__ == '__main__':
app = QtGui.QApplication.instance()
w = MainWindow()
sys.exit(app.exec_())

The cause is likely the line containing Mayavi_Scene().update_scene() in the draw internal function. Every time draw is called, it creates a new Mayavi_Scene. The following P1 class instead defines draw as a method that accesses self.viz1 directly. I've also replaced the reference to draw with a reference to self.draw
class P1(QtGui.QWidget):
# data starts out empty, wait for user input (below, via 'draw()'):
x = []
y = []
z = []
def __init__(self, parent=None):
super(P1, self).__init__(parent)
layout = QtGui.QGridLayout(self)
layout.setContentsMargins(20,20,20,20)
layout.setSpacing(10)
self.viz1 = Mayavi_Scene()
self.ui1 = self.viz1.edit_traits(parent=self, kind='subpanel').control
layout.addWidget(self.ui1, 0, 0, 1, 1)
# button to draw data:
self.btn1 = QtGui.QPushButton('Draw',self)
# Connect the widget's draw method and the button
self.connect(self.btn1, QtCore.SIGNAL('clicked()'), self.draw)
layout.addWidget(self.btn1, 1, 0, 1, 1)
self.btn1.show()
def draw(self): #a sample user input, could have been a custom data file, etc.
P1.x = np.random.random((100,))
P1.y = np.random.random((100,))
P1.z = np.random.random((100,))
# Update the current scene without creating a new one.
self.viz1.update_scene()

Related

vtkOrientationMarkerWidget embedded in QTWidget

I have embedded the vtkOrientationMarkerWidget to the QTWidget as an axes indicator, but I met a problem there arises a bug:
ERROR: In C:\Dev\Soft\vtk\source\Rendering\OpenGL2\vtkWin32OpenGLRenderWindow.cxx, line 217
vtkWin32OpenGLRenderWindow (00000278F82D1AD0): wglMakeCurrent failed in MakeCurrent(), error: Handle
Invalid
when I multiply generate QTWidget instances by clicking the button.
Because I want to add some buttons in the 3D view, I use QTWidget as a container, which is the root cause of the problem. Are there some solutions for this?
My environment is Python 3.8+PyQT5.15.0+VTK9.0.1
The code is:
The main window:
from PyQt5.QtWidgets import QMainWindow, QDesktopWidget, QFrame, QVBoxLayout, QApplication, QPushButton
from axes_widget import VTKWidget2
from axes import Axes
from axes2 import Axes2
class Widget:
def __init__(self):
self.window = QMainWindow()
self.window.resize(500, 400)
self.window.move(300, 310)
self.button = QPushButton('3D', self.window)
self.button.clicked.connect(self.click_3d)
self.button.move(380, 80)
def click_3d(self):
self._win_vtk = VTKWidget2()
self._win_vtk.show()
if __name__ == '__main__':
app = QApplication([])
widget_ins=Widget()
widget_ins.window.show()
app.exec_()
The VTK window:
from PyQt5.QtWidgets import QMainWindow, QDesktopWidget, QFrame, QVBoxLayout, QApplication
import sys
import vtk
import vtkmodules.qt
vtkmodules.qt.QVTKRWIBase = "QGLWidget"
# QGLWidget as the base class of the QVTKRenderWindowInteractor, instead of QWidget.
# This change is because it is reported that sometimes QWidget can cause rendering problems.
from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from serum import dependency, singleton, inject
# #singleton
class VTKWidget2(QMainWindow):
def __init__(self, parent=None):
super(VTKWidget2, self).__init__(parent)
self.camera_focal_point = [0, 0, 0]
self.camera_position = [-4500, 0, 0]
self.camera_view_up = [0, -1, 0] # default, [0,1,0]
self.camera_azimuth = 0 # -20
self.camera_zoom = 1.8 # 1.75
self.z_angle = 0
self.x_angle = 0
self.y_angle = 0
self.x_transform = 200
self.y_transform = -900 # -820
self.z_transform = 1000
# vtk.vtkOutputWindow.SetGlobalWarningDisplay(0)
self.initial()
def initial(self):
self.setWindowTitle("3D_Model")
self.resize(1000, 800)
screen = QDesktopWidget().geometry()
self.self_size = self.geometry()
self.move(int((screen.width() - self.self_size.width()) / 2),
int((screen.height() - self.self_size.height()) / 2)) #
self.colors = vtk.vtkNamedColors()
# Create an actor
# self.actor = vtk.vtkActor()
self.left_text_actor = vtk.vtkTextActor()
self.right_text_actor = vtk.vtkTextActor()
# A renderer and render window
self.renderer = vtk.vtkRenderer()
# renderWindow = vtk.vtkRenderWindow()
# renderWindow.SetWindowName("Display Coordinate Axes")
# renderWindow.AddRenderer(renderer)
# An interactor
# renderWindowInteractor = vtk.vtkRenderWindowInteractor()
# renderWindowInteractor.SetRenderWindow(renderWindow)
self.renderWindowInteractor = QVTKRenderWindowInteractor()
self.renderWindow = self.renderWindowInteractor.GetRenderWindow()
self.renderWindow.AddRenderer(self.renderer)
# Add the actors to the scene
# self.renderer.AddActor(self.actor)
self.renderer.SetBackground(self.colors.GetColor3d("SlateGray"))
# add mouse interaction mode
vtkStyle = vtk.vtkInteractorStyleTrackballCamera()
# vtkStyle = MyInteractorStyle(self.renderWindow)
# vtkStyle = vtk.vtkInteractorStyleSwitch()
# vtkStyle = vtk.vtkInteractorStyleTrackballActor()
self.renderWindowInteractor.SetInteractorStyle(vtkStyle)
# self.renderWindow.GetInteractor().SetInteractorStyle(vtkStyle)
self.transform = vtk.vtkTransform()
self.transform.Translate(self.x_transform, self.y_transform, self.z_transform)
# set axes
self.add_axes()
self.vtkCamera = vtk.vtkCamera()
self.update_camera()
frame = QFrame()
self.setCentralWidget(frame) # QMainWindow's property
vl = QVBoxLayout()
vl.addWidget(self.renderWindowInteractor)
# vl.addWidget(self.widget) # problem: wrong: no widget for QT
frame.setLayout(vl)
# Begin mouse interaction
self.renderWindowInteractor.Initialize()
self.renderWindowInteractor.Start()
def add_axes(self):
# set axes
### important: widget must be set as field, otherwise it doesn't show.
# problem: no widget for QT
self.widget = vtk.vtkOrientationMarkerWidget()
self.axis = vtk.vtkAxesActor()
rgba = [0] * 4
self.colors.GetColor("Carrot", rgba)
self.widget.SetOutlineColor(rgba[0], rgba[1], rgba[2])
self.widget.SetOrientationMarker(self.axis) ### important
self.widget.SetInteractor(self.renderWindowInteractor)
self.widget.SetViewport(0.0, 0.0, 0.4, 0.4)
self.widget.SetEnabled(1)
self.widget.InteractiveOn()
def update_camera(self):
self.renderer.ResetCamera()
self.renderer.SetActiveCamera(self.vtkCamera)
self.vtkCamera.SetFocalPoint(self.camera_focal_point)
self.vtkCamera.SetPosition(self.camera_position)
self.vtkCamera.SetViewUp(self.camera_view_up)
self.vtkCamera.Azimuth(self.camera_azimuth)
self.vtkCamera.Zoom(self.camera_zoom)
self.renderWindow.Render()
The problem lies in Widget.click_3d(self). Every time you click the button you reassign a new VTKWidget2 to self._win_vtk. Since self._win_vtk is the only reference in your program to any of the VTKWidget2 windows, as soon as you assign a new value to self._win_vtk, the previous VTKWidget2 window will be deleted by the garbage collector (which apparently causes problems with the underlying QGLWidget). One way around this is to make a persistent reference to all the windows, for example by putting them in a list, e.g.
class Widget:
def __init__(self):
....
self.vtk_windows = []
def click_3d(self):
win = VTKWidget2()
self.vtk_windows.append(win)
win.show()
This still causes a bunch of errors when the last window is closed an the program exits which seems to be cause by using a QGLWidget as the base of your QVTKRenderWindowInteractor.

Print items to pdf

I have a window with a QGraphicsScene as painter, and i want to render its elements to a pdf file on press of a button.
def generateReport(self):
lineList = {}
for i in self.circleList:
for j,k in i.lineItems:
if j not in lineList:
lineList[j] = [i, k]
printed = QPdfWriter("Output.pdf")
printed.setPageSize(QPagedPaintDevice.A4)
printer = QPainter(printed)
self.painter.render(printer)
for i,j in enumerate(lineList):
# j.scene().render(printer)
# lineList[j][0].scene().render(printer)
# lineList[j][1].scene().render(printer)
printer.drawText(0, self.painter.height() + i*200, f'{j.nameItem.toPlainText()}: {lineList[j][0].m_items[4].toPlainText()}, {lineList[j][1].m_items[4].toPlainText()}')
printer.end()
nameItem on j is the name label for the line, m_items[4] is the name label for each circle.
My issue is that i cant seem to get the exact height of the rendered scene, moreover I have zero clue as to how i could overflow the text to the next page should the contents not fit in one.
it would be lovely if i could somehow render every line and its corresponding circles seperately for each connection, stored in lineList
note: the line is a child of every circle , and the names of every line and circle are children of theirs, implemented much in the same way as the answer to my previous question where in lies my final issue of the grip items also being rendered.
I have discovered that I can create a new scene, move each item one by one and render it out to the pdf but this raises two separate issues
I cant add a line break and avoid overdrawing the new render over the previous one, and
I cant position the text as addText doesnt take positional arguments.
MRE:
import random
from fbs_runtime.application_context.PyQt5 import ApplicationContext
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPainter, QPdfWriter, QBrush, QPagedPaintDevice
from PyQt5.QtWidgets import (QDialog, QGraphicsScene,
QGraphicsView, QGridLayout,
QPushButton, QGraphicsEllipseItem)
class gui(QDialog):
def __init__(self, parent=None):
super(gui, self).__init__(parent)
self.resize(1280, 720)
self.painter = QGraphicsScene(0, 0, self.width() - 50, self.height() - 70)
self.painter.setBackgroundBrush(QBrush(Qt.white))
self.canvas = QGraphicsView(self.painter)
mainLayout = QGridLayout()
mainLayout.addWidget(self.canvas, 0, 0, -1, -1)
self.setLayout(mainLayout)
#property
def circleList(self):
return [item for item in self.painter.items() if isinstance(item, QGraphicsEllipseItem)]
def newCircle(self):
self.painter.addEllipse( random.randint(100, 400), random.randint(100, 400), 50 + random.random() * 200, 50 + random.random() * 200)
def generateReport(self):
printed = QPdfWriter("Output.pdf")
printed.setPageSize(QPagedPaintDevice.A4)
printer = QPainter(printed)
self.painter.render(printer)
for i,j in enumerate(self.circleList):
printer.drawText(0, printer.viewport().height() + i*200, 'test')
printer.end()
if __name__ == "__main__":
app = ApplicationContext()
test = gui()
test.newCircle()
test.newCircle()
test.newCircle()
test.generateReport()
test.show()
exit(app.app.exec_())
if possible , the ability to print, test then circle for all circles would be decent enough for me.
Incorrect output example:
To understand what painting is like, you have to understand how QGraphicsScene::render() method works:
void QGraphicsScene::render(QPainter *painter, const QRectF &target = QRectF(), const QRectF &source = QRectF(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio)
Renders the source rect from scene into target, using painter. This
function is useful for capturing the contents of the scene onto a
paint device, such as a QImage (e.g., to take a screenshot), or for
printing with QPrinter. For example:
QGraphicsScene scene;
scene.addItem(...
...
QPrinter printer(QPrinter::HighResolution);
printer.setPaperSize(QPrinter::A4);
QPainter painter(&printer);
scene.render(&painter);
If source is a null rect, this function will use sceneRect() to
determine what to render. If target is a null rect, the dimensions of
painter's paint device will be used.
The source rect contents will be transformed according to
aspectRatioMode to fit into the target rect. By default, the aspect
ratio is kept, and source is scaled to fit in target.
See also QGraphicsView::render().
In your case, if the source is not passed, the entire sceneRect (0, 0, 1230, 650) will be copied and painted on the pdf page, if the sizes do not match, the sizes will be scaled. So from the above it follows that if you want to print an item then you must pass as source the space it occupies in the scene and hide the other items, and the target is the place where you want to paint, which involves calculating the new position based on where the previous item was printed.
Considering the above, a possible solution is the following:
import random
from PyQt5 import QtCore, QtGui, QtWidgets
class Gui(QtWidgets.QDialog):
def __init__(self, parent=None):
super(Gui, self).__init__(parent)
self.resize(1280, 720)
self.scene = QtWidgets.QGraphicsScene(
0, 0, self.width() - 50, self.height() - 70
)
self.scene.setBackgroundBrush(QtGui.QBrush(QtCore.Qt.white))
self.canvas = QtWidgets.QGraphicsView(self.scene)
mainLayout = QtWidgets.QGridLayout(self)
mainLayout.addWidget(self.canvas)
#property
def circleList(self):
return [
item
for item in self.scene.items()
if isinstance(item, QtWidgets.QGraphicsEllipseItem)
]
def newCircle(self):
self.scene.addEllipse(
random.randint(100, 400),
random.randint(100, 400),
50 + random.random() * 200,
50 + random.random() * 200,
)
def generateReport(self):
printer = QtGui.QPdfWriter("Output.pdf")
printer.setPageSize(QtGui.QPagedPaintDevice.A4)
printer.setResolution(100)
painter = QtGui.QPainter(printer)
delta = 20
f = painter.font()
f.setPixelSize(delta)
painter.setFont(f)
# hide all items
last_states = []
for item in self.scene.items():
last_states.append(item.isVisible())
item.setVisible(False)
target = QtCore.QRectF(0, 0, printer.width(), 0)
for i, item in enumerate(self.circleList):
item.setVisible(True)
source = item.mapToScene(item.boundingRect()).boundingRect()
target.setHeight(source.height())
if target.bottom() > printer.height():
printer.newPage()
target.moveTop(0)
self.scene.render(painter, target, source)
f = painter.font()
f.setPixelSize(delta)
painter.drawText(
QtCore.QRectF(
target.bottomLeft(), QtCore.QSizeF(printer.width(), delta + 5)
),
"test",
)
item.setVisible(False)
target.setTop(target.bottom() + delta + 20)
# restore visibility
for item, state in zip(self.scene.items(), last_states):
item.setVisible(state)
painter.end()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Gui()
for _ in range(200):
w.newCircle()
w.generateReport()
w.show()
sys.exit(app.exec_())

Mayavi: Customize toolbar

Is there a way to customize the default toolbar of a mayavi scene? I would like to delete some buttons as I don't need them (e.g. the save button). Here you can see which toolbar I am talking about:
The code is just an example code:
import os
os.environ['ETS_TOOLKIT'] = 'qt4'
from pyface.qt import QtGui, QtCore
from traits.api import HasTraits, Instance, on_trait_change
from traitsui.api import View, Item
from mayavi.core.ui.api import MayaviScene, MlabSceneModel, SceneEditor
from tvtk.pyface.api import DecoratedScene
from pyface.api import ImageResource
from pyface.action.api import Action
class MyCustomScene(DecoratedScene):
def _actions_default(self):
actions = [
Action(
image = ImageResource("path to image",
search_path = [self._get_image_path()],
),
tooltip = "blabla",
on_perform = self._save_snapshot,
)
]
actions.extend(DecoratedScene._actions_default(self))
return actions
#The actual visualization
class Visualization(HasTraits):
scene = Instance(MlabSceneModel, ())
#on_trait_change('scene.activated')
def update_plot(self):
# We can do normal mlab calls on the embedded scene.
self.scene.mlab.test_points3d()
# the layout of the dialog screated
view = View(Item('scene', editor=SceneEditor(scene_class=MyCustomScene),
height=250, width=300, show_label=False),
resizable=True # We need this to resize with the parent widget
)
################################################################################
# The QWidget containing the visualization, this is pure PyQt4 code.
class MayaviQWidget(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
layout = QtGui.QVBoxLayout(self)
layout.setContentsMargins(0,0,0,0)
layout.setSpacing(0)
self.visualization = Visualization()
# The edit_traits call will generate the widget to embed.
self.ui = self.visualization.edit_traits(parent=self,
kind='subpanel').control
layout.addWidget(self.ui)
self.ui.setParent(self)
if __name__ == "__main__":
# Don't create a new QApplication, it would unhook the Events
# set by Traits on the existing QApplication. Simply use the
# '.instance()' method to retrieve the existing one.
app = QtGui.QApplication.instance()
container = QtGui.QWidget()
container.setWindowTitle("Embedding Mayavi in a PyQt4 Application")
# define a "complex" layout to test the behaviour
layout = QtGui.QGridLayout(container)
# put some stuff around mayavi
label_list = []
for i in range(3):
for j in range(3):
if (i==1) and (j==1):continue
label = QtGui.QLabel(container)
label.setText("Your QWidget at (%d, %d)" % (i,j))
label.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignVCenter)
layout.addWidget(label, i, j)
label_list.append(label)
mayavi_widget = MayaviQWidget(container)
layout.addWidget(mayavi_widget, 1, 1)
container.show()
window = QtGui.QMainWindow()
window.setCentralWidget(container)
window.show()
# Start the main event loop.
app.exec_()
I think it is somewhere hidden in the MayaviScene. Maybe it is necessary to create a new class with a new scene or something like this?
You should check _actions_default for MayaviScene and for DecoratedScene to see how to create one of your own. The second one shows how to create a toolbar from scratch while the first one shows how your toolbar code interfaces with other components.
class MyCustomScene(DecoratedScene):
# …
def _actions_default(self):
actions = [
# add icons here
# …
]
return actions

PySide QtGui.QGraphicsWidget child-parent transform

I have a question about using QtGui.QGraphicsWidget. In a given example I will try to describe my problem. There are two QGraphicsWidget instances inside the QtGui.QtGraphicsScene, of which one is a parent (app_widget.main_widget - blue one when running), and other one is its child widget (app_widget.subwidget - red one when running). If you run the program, with a Plus key on your keyboard you can transform widgets circularly through the states.
# --coding: utf-8 --
# window.py
import sys
from PySide import QtGui, QtCore
class WidgetBase(QtGui.QGraphicsWidget):
def __init__(self, parent = None):
super(WidgetBase, self).__init__(parent)
def set_background(self, color_hex):
palette = QtGui.QPalette()
color = QtGui.QColor()
color.setRgba(color_hex)
palette.setColor(QtGui.QPalette.Background, color)
self.setPalette(palette)
self.setAutoFillBackground(True)
class AppWidget(WidgetBase):
def __init__(self):
super(AppWidget, self).__init__()
self.main_widget = WidgetBase(self)
self.subwidget = WidgetBase(self.main_widget)
class Window(QtGui.QMainWindow):
change_app_state = QtCore.Signal()
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self, parent)
SCREEN_DIM = QtGui.QDesktopWidget().screenGeometry()
self.app_scene = QtGui.QGraphicsScene(0, 0, SCREEN_DIM.width(), SCREEN_DIM.height())
app_view = QtGui.QGraphicsView(self.app_scene)
app_view.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
app_view.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.setCentralWidget(app_view)
app_widget = AppWidget()
app_widget.main_widget.set_background(0x50449558)
app_widget.main_widget.resize(500, 500)
app_widget.subwidget.set_background(0x50ff3300)
app_widget.subwidget.resize(500 * 0.5, 500)
self.app_scene.addItem(app_widget)
self.machine = QtCore.QStateMachine()
state1 = QtCore.QState(self.machine)
state2 = QtCore.QState(self.machine)
state3 = QtCore.QState(self.machine)
state4 = QtCore.QState(self.machine)
state1.assignProperty(app_widget.main_widget, 'geometry', QtCore.QRectF(0, 0, 500, 500))
state2.assignProperty(app_widget.main_widget, 'scale', 0.5)
state3.assignProperty(app_widget.main_widget, 'scale', 1)
state4.assignProperty(app_widget.main_widget, 'geometry', QtCore.QRectF(0, 0, 1000, 500))
trans1 = state1.addTransition(self.change_app_state, state2)
trans2 = state2.addTransition(self.change_app_state, state3)
trans3 = state3.addTransition(self.change_app_state, state4)
trans4 = state4.addTransition(self.change_app_state, state1)
trans1.addAnimation(QtCore.QPropertyAnimation(app_widget.main_widget, 'scale', state1))
trans2.addAnimation(QtCore.QPropertyAnimation(app_widget.main_widget, 'scale', state2))
trans3.addAnimation(QtCore.QPropertyAnimation(app_widget.main_widget, 'geometry', state3))
trans4.addAnimation(QtCore.QPropertyAnimation(app_widget.main_widget, 'geometry', state4))
self.machine.setInitialState(state1)
self.machine.start()
def keyPressEvent(self, e):
if e.key() == QtCore.Qt.Key_Plus:
self.change_app_state.emit()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Window()
window.showFullScreen()
sys.exit(app.exec_())
When scaling parent widget (changing 'scale' property - QtGui.QGraphicsWidget property inherited from QtGui.QGraphicsObject), a child widget also get scaled. But, when I change geometry of parent widget (changing 'geometry' property - QtGui.QGraphicsWidget property), child widget geometry remains unchanged.
I am running Python 2.7.6, PySide version 1.2.1 and QtCore version 4.8.6.
Why isn't a child widget always following parents transformations? Is there any way to scale only one axis of parent widget and get all children widgets to get scaled proportionally?
Answered on qt forum.
"It will only follow it's parent widget if you put it in a layout that you set on the said parent widget. Otherwise it's up to you to handle the positioning and size of the child widget."
Link on qt forum post:
https://forum.qt.io/topic/59958/pyside-qtgui-qgraphicswidget-child-parent-transformations

Deleting an item from a scene

I currently am having an issue with a delete button I created that is linked to a QGraphicsScene class. The button is created in the Window class not the MyView class. I am trying to have the user be able to delete marks that were made on a scene but right now it is only deleting the last ellipse item I create and nothing else. The error that pops up usually says that the other objects you are trying to delete are in a different scene. Also the location of the circle object that wants to be deleted is important. So if the user has the cursor over a particular circle that circle item should delete and nothing else. Here's my code:
import sys
from PyQt4 import QtGui, QtCore
#this sets the scene for drawing and the microscope image
class MyView(QtGui.QGraphicsView):
def __init__(self,window):
QtGui.QGraphicsView.__init__(self)
self.window = window
self.scene = QtGui.QGraphicsScene(self)
self.item = QtGui.QGraphicsRectItem(400, 400, 400, 400)
self.scene.addItem(self.item)
self.setScene(self.scene)
def paintMarkers(self,event):
##self.cursor = QtGui.QCursor()
#self.cursor.setShape(2)
p = self.mapToScene(event.x(),event.y())
self.circleItem = QtGui.QGraphicsEllipseItem(p.x(),p.y(),5,5)
self.scene.addItem(self.circleItem)
self.circleItem.setPen(QtGui.QPen(QtCore.Qt.red, 1.5))
#self.setScene(self.scene)
def deleteMarkers(self):
self.scene.removeItem(self.circleItem)
#print "Hello world"
#def mousePressEvent(self,QMouseEvent):
#self.paintMarkers()
def mousePressEvent(self,event):
if self.window.btnPaintDot.isChecked():
self.paintMarkers(event)
if self.window.btnDeleteMarks.isChecked():
self.deleteMarkers()
return QtGui.QGraphicsView.mousePressEvent(self,event)
class Window(QtGui.QMainWindow):
def __init__(self):
#This initializes the main window or form
super(Window,self).__init__()
self.setGeometry(50,50,1000,1000)
self.setWindowTitle("Pre-Alignment system")
self.view = MyView()
self.setCentralWidget(self.view)
#makes deletemarks button checked when pressed
def paintDeleteMarks(self):
if self.btnDeleteMarks.isChecked():
self.btnPaintDot.setChecked(False)
self.btnPaintPolygon.setChecked(False)
self.btnPaintPolygon.setChecked(False)
self.btnDeleteMarks.setChecked(True)
else:
self.btnDeleteMarks.setChecked(False)
Much thanks please ask questions if my explanation needs more...well explaining.
If you carefully read over your code, you will see that you are deleting the item stored in self.circleItem. The item stored in that variable is always only the last one created (you overwrite the variable each time you create a new item).
You need to modify your code so that it finds items based on the current x-y coordinate of the mouse event. Use QGraphicsScene.itemAt() to find the item at a particular x-y coordinate (remember to correctly transform the coordinates relative to the scene before looking up the item at that location).
Here is the code that fixed the issue thanks to three_pineapples!
import sys
from PyQt4 import QtGui, QtCore
#this sets the scene for drawing and the microscope image
class MyView(QtGui.QGraphicsView):
def __init__(self,window):
QtGui.QGraphicsView.__init__(self)
self.window = window
self.scene = QtGui.QGraphicsScene(self)
self.item = QtGui.QGraphicsRectItem(400, 400, 400, 400)
self.scene.addItem(self.item)
self.setScene(self.scene)
def paintMarkers(self,event):
##self.cursor = QtGui.QCursor()
#self.cursor.setShape(2)
p = self.mapToScene(event.x(),event.y())
if (p.x() > 400 and p.x() < 800) and (p.y() > 400 and p.y() < 800):
self.circleItem = QtGui.QGraphicsEllipseItem(p.x(),p.y(),5,5)
self.scene.addItem(self.circleItem)
self.circleItem.setPen(QtGui.QPen(QtCore.Qt.red, 1.5))
#self.setScene(self.scene)
def deleteMarkers(self,event):
p = self.mapToScene(event.x(),event.y())
if self.scene.itemAt(p.x(),p.y()) != self.item:
self.scene.removeItem(self.scene.itemAt(p.x(),p.y()))
#print "Hello world"
#def mousePressEvent(self,QMouseEvent):
#self.paintMarkers()
def mousePressEvent(self,event):
if self.window.btnPaintDot.isChecked():
self.paintMarkers(event)
if self.window.btnDeleteMarks.isChecked():
self.deleteMarkers(event)
return QtGui.QGraphicsView.mousePressEvent(self,event)

Categories

Resources