How can I avoid "Segmentation fault" error when plotting two Widgets with PyVista?
I have created a GUI with PyQt5 where there is an area in which I would like to plot some volumes. I sometime wish to slice the volumes for which I am using PyVista's "add_mesh_clip_plane" widget. It always works just fine the first time, but when I try to plot another volume with the same slicing function in the same QFrame, the program crashes and I get a " Segmentation fault (core dumped)" error,
does anybody know / have any ideas on how I could solve this??
I have created a small example (code to be found below) where a QFrame plots either a brain or Nefertiti's head by means of this slicing widget ad the user's will.
Before plotting each of them, I clear the QFrame of all actors and remove all their properties thanks to "clear()", the situation I would like to avoid would be:
1- click on plot Nefertiti 2- click on either plot Nefertiti or plot brain 3- when clicked within the QFrame area to move the slicing widget, the program crashes
I would like to complete delete the mesh before step 2), so that step 2) works just as step 1)
I have tried deleting the whole QFrame object and creating it again from scratch to no avail
PyQt5 GUI:
---------------------------------
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'double_plot.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtWidgets # QtGui
class Ui_Test(object):
def setupUi(self, Test):
Test.setObjectName("Test")
Test.resize(642, 574)
self.widget = QtWidgets.QWidget(Test)
self.widget.setGeometry(QtCore.QRect(110, 30, 402, 433))
self.widget.setObjectName("widget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.widget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.vtkFrame = QtWidgets.QFrame(self.widget)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred,
QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.vtkFrame.sizePolicy().
hasHeightForWidth())
self.vtkFrame.setSizePolicy(sizePolicy)
self.vtkFrame.setMinimumSize(QtCore.QSize(400, 400))
self.vtkFrame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.vtkFrame.setFrameShadow(QtWidgets.QFrame.Raised)
self.vtkFrame.setObjectName("vtkFrame")
self.verticalLayout.addWidget(self.vtkFrame)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.plotNef_button = QtWidgets.QPushButton(self.widget)
self.plotNef_button.setObjectName("plotNef_button")
self.horizontalLayout.addWidget(self.plotNef_button)
self.plotBrain_button = QtWidgets.QPushButton(self.widget)
self.plotBrain_button.setObjectName("plotBrain_button")
self.horizontalLayout.addWidget(self.plotBrain_button)
self.verticalLayout.addLayout(self.horizontalLayout)
self.retranslateUi(Test)
QtCore.QMetaObject.connectSlotsByName(Test)
def retranslateUi(self, Test):
_translate = QtCore.QCoreApplication.translate
Test.setWindowTitle(_translate("Test", "Test"))
self.plotNef_button.setText(_translate("Test", "Plot Nefertiti"))
self.plotBrain_button.setText(_translate("Test", "Plot Brain"))
Main file - executable (python main.py)
-----------------------------------------------
import pyvista as pv
from pyvista import examples
from PyQt5 import QtWidgets
from double_plot import Ui_Test
class MainWindow(QtWidgets.QMainWindow, Ui_Test):
def __init__(self, *args, **kwargs):
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
self.setupUi(self)
self.mesh = None
self.vtk_widget = pv.QtInteractor(self.vtkFrame)
self.plotNef_button.clicked.connect(self.plot_nefertiti)
self.plotBrain_button.clicked.connect(self.plot_brain)
def plot_nefertiti(self):
self.vtk_widget.clear() # Remove all actors and properties
self.mesh = examples.download_nefertiti()
self.vtk_widget.add_mesh_clip_plane(self.mesh)
self.vtk_widget.update()
def plot_brain(self):
self.vtk_widget.clear() # Remove all actors and properties
self.mesh = examples.download_brain()
self.vtk_widget.add_mesh_clip_plane(self.mesh)
self.vtk_widget.update()
if __name__ == "__main__":
app = QtWidgets.QApplication([])
window = MainWindow()
window.show()
app.exec_()
As stated above, step 2 makes the program crash, reminder:
1- click on plot Nefertiti 2- click on either plot Nefertiti or plot brain
when clicked within the QFrame area to move the slicing widget, the program crashes (ONLY AFTER STEP 2)
0 ответов
Для тех, кто читает это в будущем, этого крайнего случая можно избежать, позвонив
disable_plane_widget
до звонка
add_mesh_plane_widget
.