Note that there are some explanatory texts on larger screens.

plurals
  1. POEmbedding a matplotlib chart into Qt/C++ application
    text
    copied!<p>I am developing an math-oriented GUI application in Qt/C++ and would like to embed a Python scripting, including NumPy and Matplotlib. Using Python C API, I finally managed to run a script, retrieve the values from the Python variables, including NumPy arrays etc. But I failed at drawing Matplotlib charts into my Qt/C++ application.</p> <p>It is better to say, I have managed to save the chart's RGBA buffer to a variable using Python script, then get the value of the variable as a PyObject of buffer type, get the buffer and transform it to QImage then to QPixmap and finally put it into QLabel and display it.</p> <p>But I am still missing its interactive behaviour, resizing etc. though it seems that it can be done by forwarding Qt mouse events to figure.canvas.button_press_event but it is then getting overly complicated... So I concluded I do not understand the principles of Python embedding well enough. I am missing something obvious.</p> <p>I found some samples of embedding matplotlib charts into PyQt or PySide (i.e. written in Python) applications where I have seen something like QMainWindow.setCentralWidget(canvas) or layout.addWidget(canvas). Here canvas is FigureCanvasQTAgg object ( <a href="http://matplotlib.org/api/backend_qt4agg_api.html">http://matplotlib.org/api/backend_qt4agg_api.html</a> ).</p> <p>This suggests that canvas inherits from QWidget. But when I try mimic this in C++ code using Python's C API, I end with just a PyObject *canvas, not knowing how to trasform it to a QWidget. This is my snippet without the important step:</p> <pre><code>//draw a figure in Python script called from C++ app PyRun_SimpleString("import matplotlib\n" "matplotlib.use('Qt4agg')\n" //use Qt4 backend "import pylab\n" "pylab.plot(randn(10))\n" //plot something "fig = pylab.gcf()\n" //take current figure "canvas = fig.canvas" //canvas is of FigureCanvasQTAgg type //get canvas as PyObject PyObject* m = PyImport_AddModule("__main__"); PyObject* canvas = PyObject_GetAttrString(m, "canvas"); //from what I know, canvas is a PyObject wrapped around an object derived from QWidget //... //... here I do not know how to convert canvas (i.e. PyObject) into canvasWidget (i.e. QWidget)??? //... //show chart as a widget MyWindow.setCentralWidget(canvasWidget) </code></pre> <p>As I wrote, I must be missing something obvious. I googled everywhere but without success. Any help would be very appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload