QObject::connect: 没有这样的信号错误 C++

QObject::connect: No such signal eror C++

我遇到了一个错误,我不知道哪里出了问题.我在头文件中将 SLOT 声明为 public 插槽。

信号声明:

void editPolygon(QVector <QPointF> points, QPolygonF &);

这是我的连接:

  connect(this, SIGNAL(editPolygon(QVector <QPointF> &, QPolygonF &)), parent, SLOT(editPolygon(QVector <QPointF> points, CustomPolygon *poly)));

这是我发出信号的插槽:

void CustomPolygon::editPolygons()
{
    QVector<QPointF> points;
    QPolygonF poly = mapToScene(polygon());

    emit editPolygon(points,poly);
}

这是mainwindow.cpp中的空白:

void MainWindow::editPolygon(QVector<QPointF> &points, CustomPolygon *poly)
{
}

提前感谢您的帮助!

第一个 我会做的是非常仔细地检查你的 signal/slot 匹配的第二个参数的差异。

一个是QPolygon&,另一个是CustomPolygon*。我的理解是这些必须匹配。即使一个是另一个的适当子 class,我认为您不能像那样混合引用和指针。


而且,关于您的(略有转述的)评论:

My slot has to stay as a pointer because it points to an item that is being deleted from a QGraphicsScene.

My signal cant be a pointer because there is no conversion from a QPolygonF to a QPolygonF *, and I require QPolygonF to mapToScene.

认为我明白你在说什么,但有几个问题。

如果你的槽接受一个将被释放的指针,你必须给它一个指针,并放弃对它后面对象的控制(所以要么从一个动态分配的开始复制你愿意放弃的,或者制作一个你不想想要放弃的东西的动态分配副本)。 =24=]

而且我不确定您为什么认为没有从对象到指向该对象的指针的转换。我认为 技术上 是真的,因为 class 本身 不提供转换,但这确实是 基础 语言:

Type thing;
Type *pointerToThing = &thing;

这两个问题都可以通过以下方式解决:

QVector<QPointF> points;
QPolygonF thisIsMyPoly = mapToScene(polygon());
QploygonF *thisIsPolyForFreeing = new QPolygonF(thisIsMyPoly);
emit editPolygon(points, thisIsPolyForFreeing);

您的信号仍然需要更改为使用指针而不是引用,希望这会减轻不匹配。


如果您的指针是 实际 指向无法复制的 QPolygonF 的指针(因为它是指向 [=15= 中保存的实际项目的指针) ] 因此你需要原始指针来删除它),我认为你应该能够按原样传递该指针,假设 mapToScene() 返回对它的引用而不是副本:

QPolygonF poly = mapToScene(polygon()); // get ref.
emit editPolygon(points, &poly);        // pass ref's address.