⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

Plotting in new window

  • Hi All,

    So my project, which reads a text log file and plots the data on the mainWindow using QCustomPlot has been working well, so in the interest of continuing to learn (onwards and upwards!), I decided that I wanted my graph to be in a new separate window and have most of the functionality that is demonstrated in the QCustomPlot example 'interactions' (clickable, zoomable, etc). I have created a new QDialog form called graphWindowDialog.ui, with graphWindowDialog.cpp, and graphWindowDialog.h. In my mainWindow.cpp I changed my function that populated the graph widget, to now send a signal to a slot in graphWindowDialog.cpp.

    When I run it, the window will appear and there is a graph squished up in the corner of the window. I think this is because my signal is not sending over the data correctly...?

    Here is my code (snipped for brevity):

    in MainWindow.cpp:

    /********************* populate the graph ***********************/
    void MainWindow::graphSelected(QStringList _graphingData, QString sensorName) {
        graphWindowDialog = new GraphWindowDialog(this);
        connect(ui->btnChartData, SIGNAL(clicked()), this, SLOT(redirectData));
                connect(this, SIGNAL(redirectData(_graphingData, sensorName)), graphWindowDialog, SLOT(addGraph(QStringList, QString)));

    in MainWindow.h:

        void redirectData(QStringList _graphingData, QString sensorName);
    private slots:
        void handleFileButton();
        void on_btnChartData_clicked();
        void on_btnClearChart_clicked();
        void graphSelected(QStringList, QString);
        //void addGraph(QStringList, QString);
        Ui::MainWindow *ui;
        GraphWindowDialog *graphWindowDialog;
        QPushButton *m_button;
        //QCustomPlot *customPlot;

    in graphWindowDialog.cpp:

    void GraphWindowDialog::addGraph(QStringList _graphingData, QString sensorName)
            QList<QStringList> list;
            QList<double> dataPoints;
            foreach (const QString &s, _graphingData) {
                const QStringList parts = s.split(",");
                Q_ASSERT(parts.size() >= 3);
            QVector<double> y = QVector<double>::fromList(dataPoints);
            if (Debug) {
            qDebug() << list;
            qDebug() << dataPoints;
            int j = dataPoints.size();
            QVector<double> x(j);
            for (int i=0; i<j; i++) {
      customPlot->graph()->setData(x, y);
      if (rand()%100 > 50)
      QPen graphPen;
      graphPen.setColor(QColor(rand()%245+10, rand()%245+10, rand()%245+10));

    and graphWindowDialog.h:

        explicit GraphWindowDialog(QWidget *parent = 0);
        bool Debug;
    private slots:
        void addGraph(QStringList _graphingData, QString sensorName);
          Ui::GraphWindowDialog *ui;
          QCustomPlot *customPlot;
          void titleDoubleClick(QMouseEvent *event);
          void axisLabelDoubleClick(QCPAxis* axis, QCPAxis::SelectablePart part);
          void legendDoubleClick(QCPLegend* legend, QCPAbstractLegendItem* item);
          void selectionChanged();
          void mousePress();
          void mouseWheel();
          void addGraph();
          void removeSelectedGraph();
          void removeAllGraphs();
          void contextMenuRequest(QPoint pos);
          void moveLegend();
          //void graphClicked(QCPAbstractPlottable *plottable, int dataIndex);

    When I run it I also get these runtime messages:

    QObject::connect: No such slot GraphWindowDialog::selectionChanged() in ..\NewLogReader\graphwindowdialog.cpp:37
    QObject::connect:  (receiver name: 'GraphWindowDialog')
    QObject::connect: No such slot GraphWindowDialog::mousePress() in ..\NewLogReader\graphwindowdialog.cpp:39
    QObject::connect:  (receiver name: 'GraphWindowDialog')
    QObject::connect: No such slot GraphWindowDialog::mouseWheel() in ..\NewLogReader\graphwindowdialog.cpp:40
    QObject::connect:  (receiver name: 'GraphWindowDialog')
    QObject::connect: No such slot GraphWindowDialog::axisLabelDoubleClick(QCPAxis*,QCPAxis::SelectablePart) in ..\NewLogReader\graphwindowdialog.cpp:47
    QObject::connect:  (receiver name: 'GraphWindowDialog')
    QObject::connect: No such slot GraphWindowDialog::legendDoubleClick(QCPLegend*,QCPAbstractLegendItem*) in ..\NewLogReader\graphwindowdialog.cpp:48
    QObject::connect:  (receiver name: 'GraphWindowDialog')
    QObject::connect: No such slot GraphWindowDialog::titleDoubleClick(QMouseEvent*) in ..\NewLogReader\graphwindowdialog.cpp:49
    QObject::connect:  (receiver name: 'GraphWindowDialog')
    QObject::connect: No such slot GraphWindowDialog::graphClicked(QCPAbstractPlottable*,int) in ..\NewLogReader\graphwindowdialog.cpp:52
    QObject::connect:  (receiver name: 'GraphWindowDialog')
    QObject::connect: No such slot GraphWindowDialog::contextMenuRequest(QPoint) in ..\NewLogReader\graphwindowdialog.cpp:56
    QObject::connect:  (receiver name: 'GraphWindowDialog')
    QObject::connect: Parentheses expected, slot MainWindow::redirectData in ..\NewLogReader\mainwindow.cpp:104
    QObject::connect:  (sender name:   'btnChartData')
    QObject::connect:  (receiver name: 'MainWindow')
    QObject::connect: No such signal MainWindow::redirectData(_graphingData, sensorName) in ..\NewLogReader\mainwindow.cpp:105
    QObject::connect:  (sender name:   'MainWindow')
    QObject::connect:  (receiver name: 'GraphWindowDialog')

    By the way, this is the first time I have really tried to use signals and slots...
    thanks for any hints!!


  • Lifetime Qt Champion


    Don't pass parameters in the connect statement. That's not how it works. You should also consider using the new syntax using function pointers. That will avoid that kind of mistake.

    See the Signals & Slots chapter of Qt's documentation for more information.

Log in to reply