VTK integration in PyQt (wrong layout)
-
I need to create a simple QT application that allows the user to view meshes using VTK. So basically the program is a window with a frame and a button (for now). My layout is such that the button must fill half the length of the window, and the frame (that'll display the mesh) will fill the other half. For
experimentation i tried the 3D ball shown here: https://stackoverflow.com/questions/48105646/embedding-vtk-object-in-pyqt5-window.Note: This is different the example above because i just want part of the window to be filled by the VTK widget, instead of the whole window.
Here's my code for
foo.py
:from PyQt5 import QtCore, QtGui, QtWidgets class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(743, 430) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget) self.horizontalLayout.setObjectName("horizontalLayout") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setObjectName("pushButton") self.horizontalLayout.addWidget(self.pushButton) self.frame = QtWidgets.QFrame(self.centralwidget) self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel) self.frame.setFrameShadow(QtWidgets.QFrame.Raised) self.frame.setObjectName("frame") self.horizontalLayout.addWidget(self.frame) MainWindow.setCentralWidget(self.centralwidget) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.pushButton.setText(_translate("MainWindow", "PushButton"))
And here is my code for
init.py
:import vtk import sys from PyQt5 import QtCore, QtGui from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor from PyQt5.QtWidgets import QMainWindow, QApplication, QDialog, QFileDialog from foo import Ui_MainWindow from PyQt5 import Qt class MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) self.pushButton.clicked.connect(self.OpenVTK) def OpenVTK(self): self.vtkWidget = QVTKRenderWindowInteractor(self.frame) self.vl = Qt.QVBoxLayout() #I think the mistake might be here.. self.vl.addWidget(self.vtkWidget) self.ren = vtk.vtkRenderer() self.vtkWidget.GetRenderWindow().AddRenderer(self.ren) self.iren = self.vtkWidget.GetRenderWindow().GetInteractor() # Create source source = vtk.vtkSphereSource() source.SetCenter(0, 0, 0) source.SetRadius(5.0) # Create a mapper mapper = vtk.vtkPolyDataMapper() mapper.SetInputConnection(source.GetOutputPort()) # Create an actor actor = vtk.vtkActor() actor.SetMapper(mapper) self.ren.AddActor(actor) self.ren.ResetCamera() self.frame.setLayout(self.vl) self.setCentralWidget(self.frame) self.show() self.iren.Initialize() self.iren.Start() if __name__ == "__main__": app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_())
I would be very appreciated if someone could help me out. Thanks in advance!
-
Hi,
You are replacing the central widget in OpenVTK. You should rather add your widget to the current central widget's layout.