简体   繁体   English

c ++ / qt - 没有这样的插槽 - 继承

[英]c++/ qt - no such slot - Inheritance

This code: 这段代码:

MyAxis *ax;
ax = static_cast<MyAxis*>(ui->customPlot->axisRect()->addAxis(QCPAxis::atLeft));
connect(ui->customPlot->yAxis, SIGNAL(rangeChanged(QCPRange)),
        ax, SLOT(MyAxis::rescale(QCPRange)));

gives me this run-time error: 给我这个运行时错误:

QObject::connect: No such slot QCPAxis::MyAxis::rescale(QCPRange) in plotwindow.cpp:267 QObject :: connect: QCPAxis::MyAxis::rescale(QCPRange)没有这样的插槽QCPAxis::MyAxis::rescale(QCPRange) :267

Usually when I get errors like this, I add Q_OBJECT macro to class and run qmake to fix it, but that didn't work this time. 通常当我得到这样的错误时,我将Q_OBJECT宏添加到类并运行qmake来修复它,但这次没有用。

Here is the declaration of the class: 这是该类的声明:

class MyAxis : public QCPAxis
{
    Q_OBJECT
public:
    void setRefAxis(QCPAxis *refAxis);
    void setScale(double newScale);


public Q_SLOTS:
    virtual void rescale(const QCPRange &range);

private:
    double scale;
    QCPAxis *ref;
};

Changing the declaration to public slots: didn't make any difference. 将声明更改为public slots:没有任何区别。

I think you have a problem here in what you are trying to do: 我认为你在这里遇到的问题是:

ui->customPlot->axisRect()->addAxis(QCPAxis::atLeft)

returns a QCPAxis and not a MyAxis class. 返回QCPAxis而不是MyAxis类。 Lets say QCPAxiz takes 100 bytes of memory and MyAxis takes 110 bytes then you are trying to do this: 让我们说QCPAxiz占用100个字节的内存而MyAxis需要110个字节然后你试图这样做:

A_110_Byte_Type* p110Bytes = static_cast<110-bytes *> (<100-bytes>); // not real code!

I don't see how that can work. 我不明白这是怎么回事。 The function you are calling returns a QCPAxis and you can't just convert it into a MyAxis just because they share the same base class... its a bit like have a having a Ford-Fiesta and saying, "now it is a Ferrari" just because they have the same base type of "car". 你正在调用的函数返回一个QCPAxis,你不能只是因为它们共享相同的基类而将其转换为MyAxis ...它有点像拥有福特嘉年华并说,“现在它是法拉利“只是因为它们具有相同的”汽车“基础类型。

So at the moment I think you are into un-defined behavior... 所以目前我认为你进入了未明确的行为......

What is it that you are trying to do? 你想做什么? - you can copy the values of the QCPAxis into your MyAxis (with a copy constructor - I think you need one of those to do this) - 您可以将QCPAxis的值复制到MyAxis中(使用复制构造函数 - 我认为您需要其中一个来执行此操作)

You can't just cast the existing axis to a MyAxis* . 您不能只将现有轴转换为MyAxis* You need to instantiate a new MyAxis and assign that to the graph: 您需要实例化一个新的MyAxis并将其分配给图表:

QCPGraph *graph = new QCPGraph(this);
MyAxis *ax = new MyAxis(graph);
graph->setValueAxis(ax);
connect(...);

Tip for future: avoid static_cast<>() where there's a checking alternative (here, qobject_cast<>() , otherwise dynamic_cast<>() ). 未来提示:避免使用static_cast<>() ,其中有一个检查备选方案(此处为qobject_cast<>() ,否则为dynamic_cast<>() )。 That will help identify invalid assumptions. 这将有助于确定无效的假设。

Qt stores some meta information about classes that inherit from QObject . Qt存储一些关于从QObject继承的类的元信息。 One of these information is the slots of the class. 其中一个信息是该类的插槽。 By static casting an object these meta information doesn't update in cast, so your casted object's meta information doesn't contain the slot you implemented in your MyAxis class. 通过静态转换对象,这些元信息不会在强制转换中更新,因此您的转换对象的元信息不包含您在MyAxis类中实现的插槽。 that's why it can't connect the signal to the casted object at runtime. 这就是它无法在运行时将信号连接到铸造对象的原因。 Even if you reimplement a virtual slot in your MyAxis class, by static casting your object new casted object's slot points to the old slot and doesn't upgrade to point to the reimplemented one. 即使您重新实现MyAxis类中的虚拟插槽,通过静态转换对象,新的转换对象的插槽指向旧插槽,并且不会升级为指向重新实现的插槽。

UPDATE: By using qobject_cast (which is the safe cast way for Qt objects) you can see that it the casted object is NULL. 更新:通过使用qobject_cast (这是Qt对象的安全qobject_cast方式),您可以看到它的转换对象为NULL。

  1. The moc-generator of Qt is very restrictive: so write with the same signature: Qt的moc生成器非常严格:所以用相同的签名写:

     connect(..., SLOT(...(const QCPRange &))); 
  2. In one of the h-files a statement is missing: 在其中一个h文件中缺少一个语句:

      Q_DECLARE_METATYPE(QCPRange) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM