FreeCAD raises exception on simple pyside app

Here's the place for discussion related to coding in FreeCAD, C++ or Python. Design, interfaces and structures.
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

FreeCAD raises exception on simple pyside app

Post by bernd »

trying to code some pyside2 app FreeCAD raised a exception. For sure this has been working in earlier versions ...

Code: Select all

import sys
from PySide2 import QtWidgets
app = QtWidgets.QApplication(sys.argv)

OS: Windows 10 Version 2009
Word size of FreeCAD: 64-bit
Version: 0.20.26155 (Git)
Build type: Release
Branch: master
Hash: 0926a4148bcff11249fd4f56bc30256102ffe105
Python version: 3.8.6+
Qt version: 5.15.2
Coin version: 4.0.1
OCC version: 7.5.0
Locale: German/Switzerland (de_CH)


screen.png
screen.png (48.88 KiB) Viewed 2930 times
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: FreeCAD raises exception on simple pyside app

Post by bernd »

no problem on linux ...

Code: Select all

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>
>>> 
>>> 
OS: Debian GNU/Linux 10 (buster) (KDE//usr/share/xsessions/plasma)
Word size of FreeCAD: 64-bit
Version: 0.20.26471 (Git)
Build type: Unknown
Branch: master
Hash: 263f2e91da962de890ddd4a08f6e321ab7f22f53
Python version: 3.7.3
Qt version: 5.11.3
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: German/Switzerland (de_CH)
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: FreeCAD raises exception on simple pyside app

Post by bernd »

tested on another windows machine but with the same windows version and the same FreeCAD ... same as in the first post a exception is raised.

I may try latest win builds available.
balrobs
Posts: 449
Joined: Fri Apr 24, 2020 8:58 pm

Re: FreeCAD raises exception on simple pyside app

Post by balrobs »

On my Windows machine with a slightly older version of Freecad 0.20

Code: Select all

OS: Windows 10 (10.0)
Word size of FreeCAD: 64-bit
Version: 0.20.25997 (Git)
Build type: Release
Branch: master
Hash: 77b198048a63f1e9ca15eef64c8042d599a14cf3
Python version: 3.8.12
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.5.2
Locale: German/Germany (de_DE)
no window popped up, but the Python console reports following:

Code: Select all

>>> Gui.runCommand('Std_About',0)
>>> import sys
>>> from PySide2 import QtWidgets
>>> app = QtWidgets.QApplication(sys.argv)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
RuntimeError: Please destroy the QCoreApplication singleton before creating a new QApplication instance.
Maybe this can be help?
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: FreeCAD raises exception on simple pyside app

Post by bernd »

balrobs wrote: Wed Nov 24, 2021 7:51 am no window popped up, but the Python console reports following:

Code: Select all

>>> Gui.runCommand('Std_About',0)
>>> import sys
>>> from PySide2 import QtWidgets
>>> app = QtWidgets.QApplication(sys.argv)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
RuntimeError: Please destroy the QCoreApplication singleton before creating a new QApplication instance.
Maybe this can be help?
this happens if you try it for a second time because the app object was not clean destroyed. Either restart FreeCAD or use

Code: Select all

import sys
from PySide2 import QtWidgets
QtWidgets.QApplication(sys.argv)
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: FreeCAD raises exception on simple pyside app

Post by openBrain »

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 :

Code: Select all

#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).

Bug has been raised to Qt and fixed already : https://bugreports.qt.io/browse/PYSIDE-1647

HTH
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: FreeCAD raises exception on simple pyside app

Post by bernd »

For sure it has been working around February 2021.

https://github.com/IfcOpenShell/IfcOpen ... ter.py#L24 this has been working inside FreeCAD in Spring 2021. Never mind. Thanks for all the information. :)

Is there a workaround to create a qt app without this code line or with some other code?
openBrain
Veteran
Posts: 9034
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: FreeCAD raises exception on simple pyside app

Post by openBrain »

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
heda
Veteran
Posts: 1348
Joined: Sat Dec 12, 2015 5:49 pm

Re: FreeCAD raises exception on simple pyside app

Post by heda »

Code: Select all

>>> 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.
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: FreeCAD raises exception on simple pyside app

Post by bernd »

the exception is gone with

OS: Windows 10 (10.0)
Word size of FreeCAD: 64-bit
Version: 0.20.26498 (Git)
Build type: Release
Python version: 3.8.12
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.5.3
Locale: German/Switzerland (de_CH)
Post Reply