[skip ci] part Qt embedding test application to Py3 and implement workaround for Qt5

This commit is contained in:
wmayer
2020-04-30 21:03:53 +02:00
parent 501b11a03d
commit f6e61ab410
3 changed files with 64 additions and 21 deletions

View File

@@ -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

View File

@@ -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;

View File

@@ -3,6 +3,7 @@
#include <string>
#include <sstream>
#include <QtGui>
#include <QtWidgets>
#if defined(Q_WS_X11)
# include <QX11EmbedContainer>
#endif
@@ -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);
}
@@ -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);
}
@@ -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;
@@ -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);
}