Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. VTK integration in PyQt (wrong layout)
QtWS25 Last Chance

VTK integration in PyQt (wrong layout)

Scheduled Pinned Locked Moved Unsolved General and Desktop
pythonvtkpyqtpyqt5
2 Posts 2 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MafiaSkafia
    wrote on 9 May 2018, 01:41 last edited by
    #1

    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!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 9 May 2018, 21:03 last edited by
      #2

      Hi,

      You are replacing the central widget in OpenVTK. You should rather add your widget to the current central widget's layout.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1

      1/2

      9 May 2018, 01:41

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved