Unable to successfully import files with mix use of PySide6 and PyQt6
-
I am trying to use the tools to show matplotlib plots in the Qt environment.
I am using a matplotlib canvas using the simple program:
import sys
import matplotlib
matplotlib.use('QtAgg')from PyQt6 import QtCore, QtWidgets
#from PySide6 import QtCore, QtWidgetsfrom matplotlib.backends.backend_qtagg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figureclass MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) self.axes = fig.add_subplot(111) super(MplCanvas, self).__init__(fig)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) sc = MplCanvas(self, width=5, height=4, dpi=100) sc.axes.plot([0,1,2,3,4], [10,1,20,3,40]) # Create toolbar, passing canvas as first parament, parent (self, the MainWindow) as second. toolbar = NavigationToolbar(sc, self) layout = QtWidgets.QVBoxLayout() layout.addWidget(toolbar) layout.addWidget(sc) # Create a placeholder widget to hold our toolbar and canvas. widget = QtWidgets.QWidget() widget.setLayout(layout) self.setCentralWidget(widget) self.show()
def show_a_static_plot():
app = QtWidgets.QApplication(sys.argv) w = MainWindow() app.exec()
and run it with:
import sys
#from PyQt6.QtWidgets import QApplication
from PySide6 import QtCore
from qt_matplotlib_canvas import show_a_static_plot
app = QApplication(sys.argv)
show_a_static_plot()when the import is from PySide:
the interpreter result is:from PyQt6 import QtCore, QtWidgets
ImportError: DLL load failed while importing QtCore: The specified procedure could not be found.
-
I am trying to use the tools to show matplotlib plots in the Qt environment.
I am using a matplotlib canvas using the simple program:
import sys
import matplotlib
matplotlib.use('QtAgg')from PyQt6 import QtCore, QtWidgets
#from PySide6 import QtCore, QtWidgetsfrom matplotlib.backends.backend_qtagg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figureclass MplCanvas(FigureCanvasQTAgg):
def __init__(self, parent=None, width=5, height=4, dpi=100): fig = Figure(figsize=(width, height), dpi=dpi) self.axes = fig.add_subplot(111) super(MplCanvas, self).__init__(fig)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) sc = MplCanvas(self, width=5, height=4, dpi=100) sc.axes.plot([0,1,2,3,4], [10,1,20,3,40]) # Create toolbar, passing canvas as first parament, parent (self, the MainWindow) as second. toolbar = NavigationToolbar(sc, self) layout = QtWidgets.QVBoxLayout() layout.addWidget(toolbar) layout.addWidget(sc) # Create a placeholder widget to hold our toolbar and canvas. widget = QtWidgets.QWidget() widget.setLayout(layout) self.setCentralWidget(widget) self.show()
def show_a_static_plot():
app = QtWidgets.QApplication(sys.argv) w = MainWindow() app.exec()
and run it with:
import sys
#from PyQt6.QtWidgets import QApplication
from PySide6 import QtCore
from qt_matplotlib_canvas import show_a_static_plot
app = QApplication(sys.argv)
show_a_static_plot()when the import is from PySide:
the interpreter result is:from PyQt6 import QtCore, QtWidgets
ImportError: DLL load failed while importing QtCore: The specified procedure could not be found.
The issue is:
The utility is using PyQt6 imports while PySide generated ui. files are using Pyside imports.
-
Why don't you use PyQt then?
-
@jsulm I guess that might be the way to go. Except I have lots of pre-built plots already in matplotlib and seaborn. I was hoping to capitalize on that work.
I am also using other libraries that generate plots in matplotlib.
Any recommendations to try?
-
@jsulm I guess that might be the way to go. Except I have lots of pre-built plots already in matplotlib and seaborn. I was hoping to capitalize on that work.
I am also using other libraries that generate plots in matplotlib.
Any recommendations to try?
@explorer100 said in Unable to successfully import files with mix use of PySide6 and PyQt6:
Any recommendations to try?
Use PyQt to generate code from the ui file.
You can't mix PySide and PyQt. -
@explorer100 said in Unable to successfully import files with mix use of PySide6 and PyQt6:
Any recommendations to try?
Use PyQt to generate code from the ui file.
You can't mix PySide and PyQt.@jsulm Thats great!! didnt know you can do that!!
-
@jsulm Thats great!! didnt know you can do that!!
@explorer100 so pyside6 and pyqt6 cannot co-exist?
-
@explorer100 so pyside6 and pyqt6 cannot co-exist?
@explorer100 No, they are two different implementations of Qt for Python.
-
@explorer100 so pyside6 and pyqt6 cannot co-exist?
@explorer100 said in Unable to successfully import files with mix use of PySide6 and PyQt6:
so pyside6 and pyqt6 cannot co-exist?
Not in the same application.
-
@explorer100 No, they are two different implementations of Qt for Python.
@JonB after switching, a number of things are not working the same. I was capturing the accept() to do verifications. after verifying i was issuing self.done(QDialog.Accepted) which used to work well in pyside6. with PyQt6 it just crashes.
I guess my question is .. whats a better way to do verifications in a custom dialog with ok, cancel buttons.
-
@JonB after switching, a number of things are not working the same. I was capturing the accept() to do verifications. after verifying i was issuing self.done(QDialog.Accepted) which used to work well in pyside6. with PyQt6 it just crashes.
I guess my question is .. whats a better way to do verifications in a custom dialog with ok, cancel buttons.
@explorer100
I cannot say what the effects of matplot might be. Remove that. UI stuff should work same across PySide or PyQt. No "crashes". So get that working first. If not show simple code.self.done(QDialog.Accepted)
(or justself.accept()
) should be fine across both. Don't seek to change code at this stage, that might only lead to other issues, find out what your problem is. -
This post is deleted!
-
This post is deleted!
@explorer100
What "tracebacks" and what "verbose"? You have Python debugger,print()
statements, and Python lets you print a stack trace if that is what you mean. But debugger is most flexible. What is your "crash", how do you know it has "crashed", what message do you get? Have you run it under Python debugger? If Python itself is "crashing" that is different from your application "crashing". -
@explorer100
What "tracebacks" and what "verbose"? You have Python debugger,print()
statements, and Python lets you print a stack trace if that is what you mean. But debugger is most flexible. What is your "crash", how do you know it has "crashed", what message do you get? Have you run it under Python debugger? If Python itself is "crashing" that is different from your application "crashing".@JonB just the application is crashing..
I have a custom dialog that captures the accept and reject
on reject i just issue self.done(QDialog.Rejected)the custom dialog is invoked as such:
def newFile(self): from PyQt6.QtWidgets import QDialog dialog = NewProjectDialog() ret = dialog.exec() if (ret == QDialog.Accepted): print("User accepted.") elif ret == QDialog.Rejected: print("User rejected.") else : print("unknown return")
it actually never returns from the exec()
the NewProjectDialog has the below reject() function.
def reject(self): from PyQt6.QtWidgets import QDialog print("will do the reject processing here") self.done(QDialog.Rejected)
-
@JonB just the application is crashing..
I have a custom dialog that captures the accept and reject
on reject i just issue self.done(QDialog.Rejected)the custom dialog is invoked as such:
def newFile(self): from PyQt6.QtWidgets import QDialog dialog = NewProjectDialog() ret = dialog.exec() if (ret == QDialog.Accepted): print("User accepted.") elif ret == QDialog.Rejected: print("User rejected.") else : print("unknown return")
it actually never returns from the exec()
the NewProjectDialog has the below reject() function.
def reject(self): from PyQt6.QtWidgets import QDialog print("will do the reject processing here") self.done(QDialog.Rejected)
@explorer100 said in Unable to successfully import files with mix use of PySide6 and PyQt6:
@JonB just the application is crashing..
What does this mean? I asked how you know it is "crashing". If you get neither a message nor some particular behaviour you would not even know it had "crashed"....
You do not say whether you even get the message from your
reject()
or not. So we don't know whether it enters that but does not return from theexec()
or whether it never enters thereject()
in the first place. Please supply this sort of description with your questions, else we have to cross-examine you at every point because we cannot guess what you are/are not seeing.Replace your custom dialog with a plain
QDialog
and see if that works. Then subclassQDialog
for a custom dialog and just overridereject()
and see how that goes. Then work up to your actual custom dialog.There is nothing special here. I would just call this standard debugging techniques. I don't know why you are seeing different behaviour between PySide & PyQt, I'm not going to have a magic answer, you have to do some investigation.
-
sorry, not enough info.
it actually enters the reject.
with further checks.. if I remove the
if (ret == QDialog.Accepted):
and simply print ret, it actually prints the correct return!
after that.. if I even I try to print out the value of QDialog.Accepted, as in:
print("QDialog.Accepted",QDialog.Accepted)
the application crashes!
the window disappeared then this message is printed on the console
Process finished with exit code -1073740791 (0xC0000409) -
sorry, not enough info.
it actually enters the reject.
with further checks.. if I remove the
if (ret == QDialog.Accepted):
and simply print ret, it actually prints the correct return!
after that.. if I even I try to print out the value of QDialog.Accepted, as in:
print("QDialog.Accepted",QDialog.Accepted)
the application crashes!
the window disappeared then this message is printed on the console
Process finished with exit code -1073740791 (0xC0000409)@explorer100
I'm afraid I don't know. To be clear, are you saying you have "crash" behaviour with one of PyQt vs PySide but not the other on same program? Which exact versions of each? Are you quite sure that nowhere do you mix the two now? Have you removed the matplot stuff for now?I don't think anyone will be able to tell you from what we have so far. You would need a minimal, standalone example if you want others to test. As I said earlier, I think you need to start from a plain
QDialog
, or one sub-classed but nothing other thanreject()
inside it, and test behaviour from there. Then gradually add in whatever you have in your real sub-classed code till something goes wrong. -
@explorer100
I'm afraid I don't know. To be clear, are you saying you have "crash" behaviour with one of PyQt vs PySide but not the other on same program? Which exact versions of each? Are you quite sure that nowhere do you mix the two now? Have you removed the matplot stuff for now?I don't think anyone will be able to tell you from what we have so far. You would need a minimal, standalone example if you want others to test. As I said earlier, I think you need to start from a plain
QDialog
, or one sub-classed but nothing other thanreject()
inside it, and test behaviour from there. Then gradually add in whatever you have in your real sub-classed code till something goes wrong.@JonB thanks.. I will do that.
-
@explorer100
I'm afraid I don't know. To be clear, are you saying you have "crash" behaviour with one of PyQt vs PySide but not the other on same program? Which exact versions of each? Are you quite sure that nowhere do you mix the two now? Have you removed the matplot stuff for now?I don't think anyone will be able to tell you from what we have so far. You would need a minimal, standalone example if you want others to test. As I said earlier, I think you need to start from a plain
QDialog
, or one sub-classed but nothing other thanreject()
inside it, and test behaviour from there. Then gradually add in whatever you have in your real sub-classed code till something goes wrong.@JonB
wow ... just figured out the issue...
I needed to have the full path of:
QDialog.DialogCode.Accepted
and QDialog.DialogCode.Rejectedrather than just QDialog.Accepted/Rejected.
Is that a difference between the two environments of just a setting?
-
@JonB
wow ... just figured out the issue...
I needed to have the full path of:
QDialog.DialogCode.Accepted
and QDialog.DialogCode.Rejectedrather than just QDialog.Accepted/Rejected.
Is that a difference between the two environments of just a setting?
@explorer100
For Python at Qt5 all (most) Qt enumeration types were just defined at "the top level", be thatQt
or more specialized ones likeQDialog
. At Qt6 they were moved to lower-level, more specific areas dependent on their usage, likeQDialog.DialogCode
here, there are many other cases elsewhere in the Qt classes. Any code or examples you may come across which used such types/constants needs changing from 5 to 6. You are not the first person to fall foul of this.This ought affect PyQt6 and PySide6 equally, I'm not sure if you are finding a difference. Usually you get a runtime error since the old symbol is not defined. I don't know about your case. You were "unlucky" to come across this issue so early in your attempts. If you have old code or examples look out for such enumerations which need upgrading.