Problem with embedding the FreeCADGui within a C++ application (FreeCAD_widget)

Need help, or want to share a macro? Post here!
Post Reply
sfk
Posts: 2
Joined: Fri Sep 17, 2021 2:18 pm

Problem with embedding the FreeCADGui within a C++ application (FreeCAD_widget)

Post by sfk »

I am interested in embedding the FreeCADGui within a C++ application. A forum post guided me to the FreeCAD_widget example which is part of the FreeCAD repository (https://github.com/FreeCAD/FreeCAD/tree ... dded/Win32).

To be able to run FreeCAD_widget in combination with the current FreeCAD (Release) version 0.19 (installed via the Windows installer and using the included Python 3.8) and compile it with Visual Studio 2019 using the x64 Solution Platform, I had to change the FreeCAD_widget.cpp source as documented at the end of this post. Additionally, the Additional Include Directories and Additional Library Directories paths had to be changed to C:\Program FilesFreeCAD 0.19\bin\include and C:\Program FilesFreeCAD 0.19\bin\libs, respectively, in the Visual Studio project Property Pages.

Running the compiled FreeCAD_widget via the Visual Studio debugger in Release mode and selecting C:\Program FilesFreeCAD 0.19\bin as the "FreeCAD module directory“ in the FreeCAD_widget GUI results in the following error message: "This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.“. Continuing results in an unhandled exception thrown by the Qt5Core.dll.

If the FreeCAD_widget.exe is copied to into the FreeCAD bin directory (C:\Program FilesFreeCAD 0.19\bin), started and the „FreeCAD module directory“ is selected as above, the widget does what it is supposed to do: it opens the FreeCADGui.

Some Googling hinted that the above error message might be connected to the dlls in the C:\Program FilesFreeCAD 0.19\bin\platforms directory. Copying this directory into the x64\Release directory of the Visual Studio workspace enabled the opening of the FreeCADGui. However, then the FreeCADGui console stated that modules like PySide2 cannot not be found.

It seems that some paths are not set correctly. I searched already within this subform for "embedding“ FreeCAD, but did not find a thread addressing a similar problem. Any help is appreciated.

Changes to FreeCAD_widget.cpp:
Line 239: Py_SetProgramName("CEmbed_FreeCADDlg") -> Py_SetProgramName(L"CEmbed_FreeCADDlg")
Line 242: static char* app = "CEmbedFreeCADDlg"; -> static wchar_t* app = L"CEmbedFreeCADDlg";
Line 243: static char *argv[2] = {app,0}; -> static wchar_t *argv[2] = {app,0};
Additionally all occurrences of PyString_AsString had to be replaced by PyUnicode_AsUTF8.
User avatar
Kunda1
Posts: 10973
Joined: Thu Jan 05, 2017 9:03 pm

Re: Problem with embedding the FreeCADGui within a C++ application (FreeCAD_widget)

Post by Kunda1 »

Maybe wmayer can weigh in on this? ;)
wmayer wrote: pinged by pinger macro
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
wmayer
Site Admin
Posts: 17522
Joined: Thu Feb 19, 2009 10:32 am

Re: Problem with embedding the FreeCADGui within a C++ application (FreeCAD_widget)

Post by wmayer »

sfk wrote: Sun Sep 19, 2021 12:53 pm "This application failed to start because no Qt platform plugin could be initialized. Reinstalling the application may fix this problem.“.
Somewhere in the Qt docs it's said that the platform plugin must be a sub-directory of where the executable is. So, the executable is the test application and this means Qt expects the platform plugin there.

If you don't want to copy the plugin directories the alternative is to define an environment variable: https://forum.freecadweb.org/viewtopic. ... 35#p218235
User avatar
Kunda1
Posts: 10973
Joined: Thu Jan 05, 2017 9:03 pm

Re: Problem with embedding the FreeCADGui within a C++ application (FreeCAD_widget)

Post by Kunda1 »

Thanks wmayer! :D
Alone you go faster. Together we go farther
Please mark thread [Solved]
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
sfk
Posts: 2
Joined: Fri Sep 17, 2021 2:18 pm

Re: Problem with embedding the FreeCADGui within a C++ application (FreeCAD_widget)

Post by sfk »

I tried to set the path, but without success. A colleague, who is familiar with Qt, will have a look.

Anyway, thanks.
Post Reply