Skip to content

Instantly share code, notes, and snippets.

@yudhastyawan
Last active September 13, 2021 09:11
Show Gist options
  • Save yudhastyawan/fd46b797ca21021c3f125d15cfa1ad22 to your computer and use it in GitHub Desktop.
Save yudhastyawan/fd46b797ca21021c3f125d15cfa1ad22 to your computer and use it in GitHub Desktop.
Embedded matplotlib inside Qt5 C++
######################################################################
# Automatically generated by qmake (3.1) Mon Sep 13 15:29:11 2021
######################################################################
TEMPLATE = app
TARGET = embedded
INCLUDEPATH += .
INCLUDEPATH += /home/yudha/Miniconda/envs/pyqt5/include/python3.9
# 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
SOURCES += main.cpp
QT += widgets
LIBS += -Wl,-rpath,/home/yudha/Miniconda/envs/pyqt5/lib/python3.9/lib-dynload -Wl,-rpath,/usr/lib/x86_64-linux-gnu -Wl,-rpath,/home/yudha/Miniconda/envs/pyqt5/lib -L/home/yudha/Miniconda/envs/pyqt5/lib -lpython3.9
CONFIG += no_keywords
#include <QApplication>
#include <QMainWindow>
#include <iostream>
#include <Python.h>
class PythonQt
{
public:
PythonQt()
{
wchar_t const name[] = L"test";
Py_SetProgramName(name);
Py_Initialize();
std::string code =
"import sys\n"
"sys.path += [\"/home/yudha/Miniconda/envs/pyqt5/lib/python3.9/lib-dynload\"]\n"
// "print('\\n'.join(sys.path))\n";
"import matplotlib\n"
"matplotlib.use('Qt5Agg')\n"
""
"from PyQt5 import QtCore, QtWidgets\n"
""
"from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg\n"
"from matplotlib.figure import Figure\n"
""
""
"class MplCanvas(FigureCanvasQTAgg):\n"
" def __init__(self, parent=None, width=5, height=4, dpi=100):\n"
" fig = Figure(figsize=(width, height), dpi=dpi)\n"
" self.axes = fig.add_subplot(111)\n"
" super(MplCanvas, self).__init__(fig)\n"
""
"all_widgets = QtWidgets.QApplication.instance().allWidgets()\n"
"window = None\n"
"for widget in all_widgets:\n"
" if str(widget.objectName()) == \"main_window\":\n"
" window = widget\n"
" break\n"
""
"#def message_box():\n"
"# QtWidgets.QMessageBox.information(None, 'Test', 'Hello!', QtWidgets.QMessageBox.Ok)\n"
"# QtWidgets.QApplication.instance().quit()\n"
""
"#button = QtWidgets.QPushButton('Press Me')\n"
"#button.clicked.connect(message_box)\n"
"sc = MplCanvas(window, width=5, height=4, dpi=100)\n"
"sc.axes.plot([0,1,2,3,4], [10,1,20,3,40])\n"
"window.setCentralWidget(sc)\n"
"window.move(600, 300)";
PyRun_SimpleString(code.c_str());
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow win;
win.setObjectName("main_window");
PythonQt python_code;
win.show();
return a.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment