2

I'm trying to use context menu to add new sub-axes graph to existing qcustomplot widget. If I call my _addGraph function in class constructor, it works as expected. But if I use it as slot, graph is not added to plot.

In debug I see that function calls and it works as expected, but new graph is not added.

#pragma once
#include <QWidget>
#include <QVBoxLayout>
#include "qcustomplot.h"
#include "menu_NewGraph.h"

class w_SignalTab : public QWidget
{
  Q_OBJECT

private:
  struct t_Plot
  {
    QCPLayoutGrid* p_layout;
    QCPAxisRect *p_axisrect;
  };

public:
  w_SignalTab(unsigned int msg, unsigned int dev, QString name)
  {
    setLayout(&_wl);
    _cplot.plotLayout()->clear();
    _cplot.setContextMenuPolicy(Qt::CustomContextMenu);
    connect(&_cplot, SIGNAL(customContextMenuRequested(QPoint)), 
            this, SLOT(_contextMenuRequest(QPoint)));
    _wl.addWidget(&_cplot);

    setContextMenuPolicy(Qt::CustomContextMenu);
    connect(this, SIGNAL(customContextMenuRequested(QPoint)), 
            this, SLOT(_contextMenuRequest(QPoint)));

    //!!! this slot is not work
    connect(&_menuNewGraph, SIGNAL(addGraph(QString, int, int)), 
            this, SLOT(_addGraph(QString, int, int)));

    _addGraph(" ", 0, 0);   // this works fine
};

~w_SignalTab(){};

private slots:
  void _contextMenuRequest(QPoint pos)
  {
    QWidget* obj_sender = qobject_cast<QWidget*>(sender());
    QMenu menu;
    menu.addAction("Add Plot", this, SLOT(_addGraphMenu()));
    menu.exec(obj_sender->mapToGlobal(pos));
  };

  void _addGraphMenu() 
  { 
    _menuNewGraph.show(); 
  }

  // this work only in constructor
  void _addGraph(QString name, int bits_from, int bits_to)
  {
    t_Plot pl;
    pl.p_layout = new QCPLayoutGrid;
    pl.p_axisrect = new QCPAxisRect(&_cplot/*, false*/);
    pl.p_layout->addElement(0, 0, pl.p_axisrect);
    _cplot.plotLayout()->addElement(_plots.count(), 0, pl.p_layout);
    _plots.append(pl);
  }

private:
  QVBoxLayout _wl;
  QCustomPlot _cplot;
  menu_NewGraph _menuNewGraph;
  QList < t_Plot > _plots;
};

What's going wrong?

4
  • what is _menuNewGraph? Commented Jun 29, 2018 at 5:52
  • This is QDialog class, has signal addGraph(QString name, int bits_from, int bits_to) Commented Jun 29, 2018 at 6:00
  • Obviously, the signal not working properly, not the slot. The _menuNewGraph has not been assigned a value from this code. Guess that's why its SIGNAL wouldn't work. And provide some stack trace would be helpful maybe. Commented Jun 29, 2018 at 6:22
  • As I write in question, signal worked. I place breakpoint and program stops on it. Commented Jun 29, 2018 at 6:31

1 Answer 1

1

The plots are, you can check by modifying the size of the widget, so that they are visible without needing to do so you must call replot().

void _addGraph(const QString & name, int bits_from, int bits_to)
{
    t_Plot pl;
    pl.p_layout = new QCPLayoutGrid;
    pl.p_axisrect = new QCPAxisRect(&_cplot/*, false*/);
    pl.p_layout->addElement(0, 0, pl.p_axisrect);
    _cplot.plotLayout()->addElement(_plots.count(), 0, pl.p_layout);
    _plots.append(pl);
    _cplot.replot(); // <---
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.