Skip to content

Commit

Permalink
[skip ci] part Qt embedding test application to Py3 and implement wor…
Browse files Browse the repository at this point in the history
…karound for Qt5
  • Loading branch information
wwmayer committed Apr 30, 2020
1 parent f900bdf commit 52344f6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 21 deletions.
38 changes: 26 additions & 12 deletions src/Tools/embedded/Qt/Qt.pro
@@ -1,12 +1,26 @@
######################################################################
# Automatically generated by qmake (2.01a) Di 15. Mrz 12:13:51 2011
######################################################################

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += mainwindow.h
SOURCES += main.cpp mainwindow.cpp

TEMPLATE = app
TARGET = Qt
INCLUDEPATH += .

linux {
INCLUDEPATH += /usr/include/python3.6
LIBS += -lpython3.6m
}

QT += widgets

# The following define makes your compiler warn you if you use any
# feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

# Input
HEADERS += mainwindow.h
SOURCES += main.cpp mainwindow.cpp
14 changes: 10 additions & 4 deletions src/Tools/embedded/Qt/main.cpp
@@ -1,15 +1,21 @@

#include <Python.h>
#include <Python.h>
#include <QApplication>

#include "mainwindow.h"

int main(int argc, char *argv[])
{
char* name = "Qt example";
Py_SetProgramName(name);
const char* name = "Qt example";
Py_SetProgramName(Py_DecodeLocale(name,NULL));
Py_Initialize();
PySys_SetArgv(argc, argv);

size_t size = argc;
wchar_t **_argv = new wchar_t*[size];
for (int i = 0; i < argc; i++) {
_argv[i] = Py_DecodeLocale(argv[i],NULL);
}
PySys_SetArgv(argc, _argv);

QApplication app(argc, argv);
MainWindow mainWin;
Expand Down
33 changes: 28 additions & 5 deletions src/Tools/embedded/Qt/mainwindow.cpp
Expand Up @@ -3,6 +3,7 @@
#include <string>
#include <sstream>
#include <QtGui>
#include <QtWidgets>
#if defined(Q_WS_X11)
# include <QX11EmbedContainer>
#endif
Expand Down Expand Up @@ -87,7 +88,7 @@ void MainWindow::loadFreeCAD()
PyObject *ptype, *pvalue, *ptrace;
PyErr_Fetch(&ptype, &pvalue, &ptrace);
PyObject* pystring = PyObject_Str(pvalue);
const char* error = PyString_AsString(pystring);
const char* error = PyUnicode_AsUTF8(pystring);
QMessageBox::warning(this, "Error", error);
Py_DECREF(pystring);
}
Expand All @@ -110,7 +111,7 @@ void MainWindow::newDocument()
PyObject *ptype, *pvalue, *ptrace;
PyErr_Fetch(&ptype, &pvalue, &ptrace);
PyObject* pystring = PyObject_Str(pvalue);
const char* error = PyString_AsString(pystring);
const char* error = PyUnicode_AsUTF8(pystring);
QMessageBox::warning(this, "Error", error);
Py_DECREF(pystring);
}
Expand All @@ -119,7 +120,6 @@ void MainWindow::newDocument()

void MainWindow::embedWindow()
{
WId hwnd = this->centralWidget()->winId();
PyObject* main = PyImport_AddModule("__main__");
PyObject* dict = PyModule_GetDict(main);
std::stringstream cmd;
Expand All @@ -134,18 +134,41 @@ void MainWindow::embedWindow()
<< "\n"
<< "FreeCADGui.addWorkbench(BlankWorkbench)\n"
<< "FreeCADGui.activateWorkbench(\"BlankWorkbench\")\n"
<< "FreeCADGui.embedToWindow(\"" << hwnd << "\")\n"
<< "\n";

#if defined(Q_WS_X11) || defined(Q_OS_WIN)
WId hwnd = this->centralWidget()->winId();
cmd << "FreeCADGui.embedToWindow(\"" << hwnd << "\")\n"
<< "\n";
#endif

PyObject* result = PyRun_String(cmd.str().c_str(), Py_file_input, dict, dict);
if (result) {
Py_DECREF(result);

#if !defined(Q_WS_X11)
// This is a workaround for the lack of a replacement of QX11EmbedWidget with Qt5
QWidget* mw = nullptr;
for (auto it : qApp->topLevelWidgets()) {
if (it->inherits("Gui::MainWindow")) {
mw = it;
break;
}
}
if (mw) {
QVBoxLayout* vb = new QVBoxLayout();
centralWidget()->setLayout(vb);
vb->addWidget(mw);
}
#endif

embedAct->setDisabled(true);
}
else {
PyObject *ptype, *pvalue, *ptrace;
PyErr_Fetch(&ptype, &pvalue, &ptrace);
PyObject* pystring = PyObject_Str(pvalue);
const char* error = PyString_AsString(pystring);
const char* error = PyUnicode_AsUTF8(pystring);
QMessageBox::warning(this, "Error", error);
Py_DECREF(pystring);
}
Expand Down

0 comments on commit 52344f6

Please sign in to comment.