Menu

QCustomPlot object wrapper

Help
Poep Hoofd
2015-10-12
2015-10-16
  • Poep Hoofd

    Poep Hoofd - 2015-10-12

    Hi,

    After getting my PythonQt to work ,thanks to Sergey, I am having troubles adding QCustomPlot to pythonQt.

    I would like to use the QCustomPlot functions inside my script.
    Currently I use:

    QCustomPlot *graph = new QCustomPlot;
    mainContext.addObject("graph",graph);
    

    Which works a bit, I can use functions like graph.newgraph();. However, functions like graph.xAxis.setLabel("test"); don't exist, 'xAxis' is unknown. I tried to do it like the example 'PyCPPWrapperExample' but I am unable of creating a wrapper for QCustomPlot.

    Help is much appreciated :)
    Thanks!

     
  • Poep Hoofd

    Poep Hoofd - 2015-10-16

    To answer my own question:

    Add this to QCustomPlot.h:

    class CustomObjectWrapper : public QObject {
      Q_OBJECT
    public Q_SLOTS:
      // add a constructor
      QCustomPlot* new_QCustomPlot()
      {
          return new QCustomPlot();
      }
      //wrapper functions
      void delete_QCustomPlot(QCustomPlot* o) //delete QCustomPlot object
      {
          delete o;
      }
      void addGraph(QCustomPlot* o) //add a graph inside the QCustomPlot object
      {
          o->addGraph();
      }
      void setData (QCustomPlot* o,int graphnum, const QVector<double> &keys, const QVector<double> &values) //set data to a certain graph. Deletes existing data inside the graph
      {
          o->graph(graphnum)->setData(keys,values);
      }
      void replot (QCustomPlot* o) //replot object to visualise new data
      {
          o->replot();
      }
      void show (QCustomPlot* o) //open new window with graph
      {
          o->show();
      }
      void setWindowTitle(QCustomPlot* o,QString title) //set title of window of graph
      {
          o->setWindowTitle(title);
      }
      void rescaleAxes(QCustomPlot* o, int graphnum) //rescale axis automatically if data does not fit
      {
          o->graph(graphnum)->rescaleAxes();
      }
      void plotLayout_insertRow(QCustomPlot* o, int row) //insert row above graph
      {
          o->plotLayout()->insertRow(row);
      }
      void plotLayout_insertColumn(QCustomPlot* o, int column) //insert column above graph
      {
          o->plotLayout()->insertColumn(column);
      }
      void plotLayout_addElement(QCustomPlot* o, int row, int column, QCPLayoutElement *element) //add text to graph at row,column
      {
          o->plotLayout()->addElement(row,column,element);
      }
    //------------------------------------------------------- Axis adjustment stuff------------------------------------------------
      //xAxis stuff
      void xAxis_setLabel(QCustomPlot* o, const QString text)
      {
          o->xAxis->setLabel(text);
      }
      void xAxis_setVisible(QCustomPlot* o, bool visible)
      {
          o->setVisible(visible);
      }
      void xAxis_setRange(QCustomPlot* o, double position, double size)
      {
          o->xAxis->setRange(position, size);
      }
      void xAxis_setAutoTicks(QCustomPlot* o, bool on)
      {
          o->xAxis->setAutoSubTicks(on);
      }
      void xAxis_setAutoTickLabels(QCustomPlot* o, bool on)
      {
          o->xAxis->setAutoTickLabels(on);
      }
      void xAxis_setTickVector(QCustomPlot* o, const QVector<double> &ticks)
      {
          o->xAxis->setTickVector(ticks);
      }
      void xAxis_setTickVectorLabels(QCustomPlot* o, const QVector<QString> &labels)
      {
          o->xAxis->setTickVectorLabels(labels);
      }
      void xAxis_setTickLength(QCustomPlot* o, int inside, int outside)
      {
          o->xAxis->setTickLength(inside,outside);
      }
      void xAxis_setSubTickLength(QCustomPlot* o, int inside, int outside)
      {
          o->xAxis->setSubTickLength(inside, outside);
      }
      //xAxis2 stuff
      void xAxis2_setLabel(QCustomPlot* o, const QString text)
      {
          o->xAxis2->setLabel(text);
      }
      void xAxis2_setVisible(QCustomPlot* o, bool visible)
      {
          o->setVisible(visible);
      }
      void xAxis2_setRange(QCustomPlot* o, double position, double size)
      {
          o->xAxis2->setRange(position, size);
      }
      void xAxis2_setAutoTicks(QCustomPlot* o, bool on)
      {
          o->xAxis2->setAutoSubTicks(on);
      }
      void xAxis2_setAutoTickLabels(QCustomPlot* o, bool on)
      {
          o->xAxis2->setAutoTickLabels(on);
      }
      void xAxis2_setTickVector(QCustomPlot* o, const QVector<double> &ticks)
      {
          o->xAxis2->setTickVector(ticks);
      }
      void xAxis2_setTickVectorLabels(QCustomPlot* o, const QVector<QString> &labels)
      {
          o->xAxis2->setTickVectorLabels(labels);
      }
      void xAxis2_setTickLength(QCustomPlot* o, int inside, int outside)
      {
          o->xAxis2->setTickLength(inside,outside);
      }
      void xAxis2_setSubTickLength(QCustomPlot* o, int inside, int outside)
      {
          o->xAxis2->setSubTickLength(inside, outside);
      }
      //yAxis stuff
      void yAxis_setLabel(QCustomPlot* o, const QString text)
      {
          o->yAxis->setLabel(text);
      }
      void yAxis_setVisible(QCustomPlot* o, bool visible)
      {
          o->setVisible(visible);
      }
      void yAxis_setRange(QCustomPlot* o, double position, double size)
      {
          o->yAxis->setRange(position, size);
      }
      void yAxis_setAutoTicks(QCustomPlot* o, bool on)
      {
          o->yAxis->setAutoSubTicks(on);
      }
      void yAxis_setAutoTickLabels(QCustomPlot* o, bool on)
      {
          o->yAxis->setAutoTickLabels(on);
      }
      void yAxis_setTickVector(QCustomPlot* o, const QVector<double> &ticks)
      {
          o->yAxis->setTickVector(ticks);
      }
      void yAxis_setTickVectorLabels(QCustomPlot* o, const QVector<QString> &labels)
      {
          o->yAxis->setTickVectorLabels(labels);
      }
      void yAxis_setTickLength(QCustomPlot* o, int inside, int outside)
      {
          o->yAxis->setTickLength(inside,outside);
      }
      void yAxis_setSubTickLength(QCustomPlot* o, int inside, int outside)
      {
          o->yAxis->setSubTickLength(inside, outside);
      }
      //yAxis2 stuff
      void yAxis2_setLabel(QCustomPlot* o, const QString text)
      {
          o->yAxis2->setLabel(text);
      }
      void yAxis2_setVisible(QCustomPlot* o, bool visible)
      {
          o->setVisible(visible);
      }
      void yAxis2_setRange(QCustomPlot* o, double position, double size)
      {
          o->yAxis2->setRange(position, size);
      }
      void yAxis2_setAutoTicks(QCustomPlot* o, bool on)
      {
          o->yAxis2->setAutoSubTicks(on);
      }
      void yAxis2_setAutoTickLabels(QCustomPlot* o, bool on)
      {
          o->yAxis2->setAutoTickLabels(on);
      }
      void yAxis2_setTickVector(QCustomPlot* o, const QVector<double> &ticks)
      {
          o->yAxis2->setTickVector(ticks);
      }
      void yAxis2_setTickVectorLabels(QCustomPlot* o, const QVector<QString> &labels)
      {
          o->yAxis2->setTickVectorLabels(labels);
      }
      void yAxis2_setTickLength(QCustomPlot* o, int inside, int outside)
      {
          o->yAxis2->setTickLength(inside,outside);
      }
      void yAxis2_setSubTickLength(QCustomPlot* o, int inside, int outside)
      {
          o->yAxis2->setSubTickLength(inside, outside);
      }
    };
    

    and use this in your main:

        PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
        PythonQt_QtAll::init();
        PythonQtObjectPtr  mainContext = PythonQt::self()->getMainModule();
        PythonQtScriptingConsole console(NULL, mainContext);
        PythonQt::self()->registerCPPClass("QCustomPlot","","script", PythonQtCreateObject<QCustomPlotWrapper>);
        console.appendCommandPrompt();
        console.show(); 
    

    Then, in your script/console :

    from PythonQt.script import *
    from PythonQt.QtGui import *
    print("script running")
    graph = QCustomPlot()
    x = [1,2,3,4,5]
    y = [0,1,2,3,4]
    graph.addGraph()
    graph.setData(0,x,y)
    graph.replot()
    graph.show()
    graph2 = QCustomPlot()
    x = [0,1,2,3,4]
    y = [0,1,2,3,3]
    graph2.addGraph()
    graph2.setData(0,x,y)
    graph2.replot()
    graph2.show()
    graph.addGraph()
    graph.setData(1,x,y)
    graph.replot()
    

    Please not that I did NOT wrap all of the functions of QCustomPlot. I think you can easily add them yourself if you need some other function I did not wrap.

    Also, I'm pretty sure that here are other, better ways to do the above but this was the only thing I could manage.The thing that I am primarily missing is the capability to do things like this:

    graph.level1.level2 I can't get past level1 so all my wrapped functions that are deeper than level1 have a different but similar name with underscores.

     

    Last edit: Poep Hoofd 2015-10-16

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.