在 Qt 槽中使用枚举

Using Enums in Qt slots

在 class QCustomPlot 中有我想在 QWidget class 的构造函数中使用的枚举,它使用 class QCustomPlot。

#include "qcustomplot.h"

SignalViewerDialog::SignalViewerDialog(QVector<double> x_1,
                                       QVector<double> y_1,
                                       QCPScatterStyle::ScatterProperty ScatterProp,
                                       QCPScatterStyle::ScatterShape ScatterShp,
                                       QCPGraph::LineStyle LineSt,
                                       QWidget *parent) : QDialog(parent)

错误

/Users/konstantin/Desktop/SVMGLEP/signalviewerdialog.cpp:72: ошибка: reference to type 'const QCPScatterStyle' could not bind to an lvalue of type 'QCPScatterStyle::ScatterProperty' ui.widgetGraph->graph()->setScatterStyle(ScatterProp); ^~~~~~~~~~~

这与在信号槽连接中传递枚举的问题无关,您需要在 Qt 元类型系统中注册枚举。这是普通 C++ 中的简单类型不匹配。

引用reference

Specifying a scatter style

You can set all these configurations either by calling the respective functions on an instance:

QCPScatterStyle myScatter;  
myScatter.setShape(QCPScatterStyle::ssCircle);  
myScatter.setPen(QPen(Qt::blue));   myScatter.setBrush(Qt::white);  
myScatter.setSize(5);  
customPlot->graph(0)->setScatterStyle(myScatter);

Or you can use one of the various constructors that take different parameter combinations, making it easy to specify a scatter style in a single call, like so:

customPlot->graph(0)->setScatterStyle(
   QCPScatterStyle(QCPScatterStyle::ssCircle, Qt::blue, Qt::white, 5)
);

您传递的是 QCPScatterStyle::ScatterProperty 类型的枚举,而不是 class QCPScatterStyle 的对象。

编辑 1:因此,您需要使用

ui.widgetGraph->graph()->setScatterStyle(QCPScatterStyle(ScatterProp));

编辑 2:我还想指出,您使用 CamelCase 作为 enum 类型的函数参数的名称。也许您这样做是因为它们是枚举,但我建议您再次这样做,因为稍后在代码中它们似乎是实际的枚举值,而不是变量名。