QT无法通过上下文菜单添加新图(QCustomPlot)
QT Can't add new plot (QCustomPlot) by context menu
我正在尝试使用上下文菜单向现有 qcustomplot
小部件添加新的 sub-axes
图形。如果我在 class 构造函数中调用我的 _addGraph
函数,它会按预期工作。但是如果我将它用作 slot
,则不会将图形添加到绘图中。
在调试中,我看到函数调用并且按预期工作,但未添加新图表。
#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;
};
怎么了?
情节是,您可以通过修改小部件的大小来检查,以便它们可见而无需这样做,您必须调用 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(); // <---
}
我正在尝试使用上下文菜单向现有 qcustomplot
小部件添加新的 sub-axes
图形。如果我在 class 构造函数中调用我的 _addGraph
函数,它会按预期工作。但是如果我将它用作 slot
,则不会将图形添加到绘图中。
在调试中,我看到函数调用并且按预期工作,但未添加新图表。
#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;
};
怎么了?
情节是,您可以通过修改小部件的大小来检查,以便它们可见而无需这样做,您必须调用 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(); // <---
}