Giter Site home page Giter Site logo

Pyside2 Example Error about pyvistaqt HOT 9 CLOSED

pyvista avatar pyvista commented on May 25, 2024
Pyside2 Example Error

from pyvistaqt.

Comments (9)

nicobako avatar nicobako commented on May 25, 2024

Hey, I would recommend reviewing #67 . I think you're having the same issue. If so, sorry, it's absolutely my fault.

from pyvistaqt.

lmarschall avatar lmarschall commented on May 25, 2024

thank you for the fast response, so i used the pip install command provided in the issue, but my version seem not be changed, and there fore the error still occurs...

from pyvistaqt.

nicobako avatar nicobako commented on May 25, 2024

I'll look into this (hopefully) tomorrow and see if I can figure out what's going on.

from pyvistaqt.

nicobako avatar nicobako commented on May 25, 2024

@lmarschall, change your code to the following and you should be all good to go:

import os
import sys

os.environ["QT_API"] = "pyside2"

# note import `qtpy` objects instead of `PySide2` objects
from qtpy import QtWidgets
from qtpy.QtWidgets import QMainWindow

# note placement of `import vtkmodules.qt after importing qtpy stuff
import vtkmodules.qt

import numpy as np

import pyvista as pv
from pyvistaqt import QtInteractor

class MainWindow(QMainWindow):

    def __init__(self, parent=None, show=True):
        QtWidgets.QMainWindow.__init__(self, parent)

        # create the frame
        self.frame = QtWidgets.QFrame()
        vlayout = QtWidgets.QVBoxLayout()

        # add the pyvista interactor object
        self.plotter = QtInteractor(self.frame)
        vlayout.addWidget(self.plotter.interactor)

        self.frame.setLayout(vlayout)
        self.setCentralWidget(self.frame)

        # simple menu to demo functions
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('File')
        exitButton = QtWidgets.QAction('Exit', self)
        exitButton.setShortcut('Ctrl+Q')
        exitButton.triggered.connect(self.close)
        fileMenu.addAction(exitButton)

        # allow adding a sphere
        meshMenu = mainMenu.addMenu('Mesh')
        self.add_sphere_action = QtWidgets.QAction('Add Sphere', self)
        self.add_sphere_action.triggered.connect(self.add_sphere)
        meshMenu.addAction(self.add_sphere_action)

        if show:
            self.show()

    def add_sphere(self):
        """ add a sphere to the pyqt frame """
        sphere = pv.Sphere()
        self.plotter.add_mesh(sphere, show_edges=True)
        self.plotter.reset_camera()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec_())

You'll see that the biggest difference is where I import vtkmodules.qt. This must be done after you have imported the qtpy stuff. This is because qtpy performs some really crazy stuff for making sure that everything is set up properly for the Qt bindings that you want to use. When you do this:

import vtkmodules.qt

os.environ["QT_API"] = "pyside2"

from qtpy import QtWidgets
from qtpy.QtWidgets import QMainWindow
...

you don't give qtpy time to setup the Qt bindings for PySide2 properly. Instead, you have handed that task to vtkmodules, which obviously has made assumptions about the Qt bindings that don't agree with what you are trying to do. For example, note that this works:

import vtkmodules.qt

os.environ["QT_API"] = "pyqt5"

from qtpy import QtWidgets
from qtpy.QtWidgets import QMainWindow

meaning that vtkmodules assumes pyqt5 as its default Qt Bindings, not PySide2...

The other really important thing to note is that I don't import PySide2 directly. Instead, I import qtpy objects. I may be completely wrong, but one of the benefits of using qtpy is that end users could decide to use pyqt5 or pyside2 at any point just by changing the QT_API environment variable... it's up to them. In this case, you are the end user. When you import PySide2 objects you circumvent qtpy's setup process, and make it impossible for end users to use qtpy to its fullest. I may be completely wrong here - I'm not a GUI developer nor a hardcore Qt user - but I think you should use the qtpy objects instead.

I hope this helps. Please let me know if you have any other questions, or if things are still not working for you.

from pyvistaqt.

lmarschall avatar lmarschall commented on May 25, 2024

hey nicobako,

i took your code and executed it with the following environment, unfortunately there's another error now :(

my environment:
python: 3.6.7
pyside2: 5.13.2
pyqt: 5.12.3
pyvista: 0.27.2
pyvistaqt: 0.2.0
qt: 5.12.9
qtpy: 1.9.0

my current error:

# add the pyvista interactor object
self.plotter = QtInteractor(self.frame)
QtInteractor cannot be converted to PyQt5.QtCore.QObject in this context

from pyvistaqt.

nicobako avatar nicobako commented on May 25, 2024

Your error message says it's trying to convert it to a pyqt5 object. Either you're not running the code as written, or you still have not pip installed the master branch of pyvistaqt. The qtpy functionality is currently only available from the master branch of the GitHub repository. Try uninstalling pyvistaqt, then reinstalling it from the master branch following the directions in #67.

from pyvistaqt.

lmarschall avatar lmarschall commented on May 25, 2024

It works!

Sorry for the last error, that was me getting confused with all my testing environments. Finally i can proceed, thank you so much!
Maybe one last question, is there a plan to integrate qml view support into pyvistaqt?

from pyvistaqt.

nicobako avatar nicobako commented on May 25, 2024

I don't know anything about qml view... That's a question for the core developpers of pyvistaqt.

from pyvistaqt.

nicobako avatar nicobako commented on May 25, 2024

@lmarschall , do you feel comfortable closing this issue? Is your initial question resolved? If so, you can go ahead and close it yourself. Thanks!

from pyvistaqt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.