tomas-silveira
tomas-silveira

Reputation: 739

VTK integration in PyQt with a button

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: Embedding VTK object in PyQT5 window.

The problem i'm getting is that i only want the vtk widget to fill half of the window instead of the whole window. I created foo.py to create my MainWindow, frame and button. init.py will initialize the GUI itself.

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!

Upvotes: 2

Views: 2186

Answers (1)

user6764549
user6764549

Reputation:

You can specify the stretch factors for the frame and pushButton so that they resize proportionately. Also, in your OpenVTK(), don't make the vertical layout as the central widget because it will hide the pushButton.

Add the following lines in setupUi() in foo.py:

# Set the stretch factors for both the pushButton & the frame
self.horizontalLayout.setStretchFactor(self.frame,1)
self.horizontalLayout.setStretchFactor(self.pushButton,1)

Comment out or remove the following line from OpenVTK() in init.py:

#self.setCentralWidget(self.frame)

The result looks as follows:

Qt VTK

Upvotes: 2

Related Questions