Python 3.7.3 (default, Jan 22 2021, 20:04:44)
[GCC 8.3.0] on linux
Type 'help', 'copyright', 'credits' or 'license' for more information.
>>> Gui.runCommand('Std_Workbench',18)
>>> Gui.runCommand('Std_ViewStatusBar',1)
>>> import sys
>>> from PySide2 import QtWidgets
>>> app = QtWidgets.QApplication(sys.argv)
>>>
>>> app
<PySide2.QtWidgets.QApplication object at 0x7f10489be5e0>
>>>
>>>
bernd wrote: ↑Tue Nov 23, 2021 5:11 pm
trying to code some pyside2 app FreeCAD raised a exception. For sure this has been working in earlier versions ...
I'm actually surprised you may have it really working in a previous version because QApplication should AFAIK be a singleton.
Anyway, I can reproduce (it actually causes a segfault hard crash). Here is the call stack :
#0 MakeQAppWrapper() at /lib/x86_64-linux-gnu/libshiboken2.cpython-39-x86_64-linux-gnu.so.5.15
#1 SbkQAppTpNew() at /lib/x86_64-linux-gnu/libshiboken2.cpython-39-x86_64-linux-gnu.so.5.15
#2 ??() at /lib/x86_64-linux-gnu/libpython3.9.so.1.0
#3 _PyObject_MakeTpCall() at /lib/x86_64-linux-gnu/libpython3.9.so.1.0
#4 _PyEval_EvalFrameDefault() at /lib/x86_64-linux-gnu/libpython3.9.so.1.0
#5 ??() at /lib/x86_64-linux-gnu/libpython3.9.so.1.0
#6 _PyEval_EvalCodeWithName() at /lib/x86_64-linux-gnu/libpython3.9.so.1.0
#7 PyEval_EvalCodeEx() at /lib/x86_64-linux-gnu/libpython3.9.so.1.0
#8 PyEval_EvalCode() at /lib/x86_64-linux-gnu/libpython3.9.so.1.0
#9 Gui::InteractiveInterpreter::runCode() at /home/OB/dev/FreeCAD/src/Gui/PythonConsole.cpp:318
#10 Gui::InteractiveInterpreter::runSource() at /home/OB/dev/FreeCAD/src/Gui/PythonConsole.cpp:297
#11 Gui::InteractiveInterpreter::push() at /home/OB/dev/FreeCAD/src/Gui/PythonConsole.cpp:371
#12 Gui::PythonConsole::runSource() at /home/OB/dev/FreeCAD/src/Gui/PythonConsole.cpp:813
#13 Gui::PythonConsole::keyPressEvent() at /home/OB/dev/FreeCAD/src/Gui/PythonConsole.cpp:607
#14 QWidget::event(QEvent*)() at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#15 QFrame::event(QEvent*)() at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#16 QApplicationPrivate::notify_helper(QObject*, QEvent*)() at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#17 QApplication::notify(QObject*, QEvent*)() at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
#18 Gui::GUIApplication::notify() at /home/OB/dev/FreeCAD/src/Gui/GuiApplication.cpp:89
#19 QCoreApplication::notifyInternal2(QObject*, QEvent*)() at /lib/x86_64-linux-gnu/libQt5Core.so.5
#20 ??() at /lib/x86_64-linux-gnu/libQt5Widgets.so.5
The crash is a regression in PySide "5.15", previous versions used to return an error saying user to destroy the existing singleton (as shown by @balrobs).
bernd wrote: ↑Wed Nov 24, 2021 3:40 pm
Is there a workaround to create a qt app without this code line or with some other code?
Actually it's important IMO to understand some details.
QApplication shall be a singleton, and then there is no chance to create another instance of it "inside" FreeCAD, as FreeCAD is itself the running QApplication.
Where it could work is if you run this "from" FreeCAD but "outside" of it, such as for example using 'os.system' or 'os.popen' or any other mean that will run in a different context from main application.
So :
* Either you ensure that the new QApplication is created in a different context from FreeCAD
* Or you use a QMainWindow (that will roughly run a second event loop inside the FreeCAD application) instead of the QApplication
>>> import PySide
>>> PySide.QtCore.QCoreApplication()
Traceback (most recent call last):
File "<input>", line 1, in <module>
RuntimeError: Please destroy the QCoreApplication singleton before creating a new QCoreApplication instance.
>>> PySide.QtCore.QCoreApplication.instance()
<PySide2.QtCore.QCoreApplication(0x7ffd794f35a0) at 0x7f37b03520c0>
>>>
from fc if you want to get hold of the running root, there is .show() and .exec_ also, don't ask me exactly what they do though, i just use trial and error until i get it working.