我在处理 Qt 中的点击时遇到了问题。我有以下课程:
class MyRectItem : public QObject, public QGraphicsEllipseItem{
Q_OBJECT
public:
MyRectItem(double x,double y, double w, double h)
: QGraphicsEllipseItem(x,y,w,h)
{}
public slots:
void test() {
QMessageBox::information(0, "This", "Is working");
printf("asd");
}
signals:
void selectionChanged(bool newState);
protected:
QVariant itemChange(GraphicsItemChange change, const QVariant &value) {
if (change == QGraphicsItem::ItemSelectedChange){
bool newState = value.toBool();
emit selectionChanged(newState);
}
return QGraphicsItem::itemChange(change, value);
}
};
现在我想将一个插槽连接到信号,我执行以下操作:
MyRectItem *i = new MyRectItem(-d, -d, d, d);
i->setPen(QPen(Qt::darkBlue));
i->setPos(150,150);
// canvas is a QGraphicsScene
canvas.addItem(i);
i->setFlags(QGraphicsItem::ItemIsSelectable | QGraphicsItem::ItemIsMovable);
QObject::connect(&canvas, SIGNAL(selectionChanged(bool)), this, SLOT(test()));
当我运行它时,会显示圆圈,canvas
但是当我单击圆圈时没有任何反应,控制台显示以下内容:
Object::connect: No such signal QGraphicsScene::selectionChanged(bool)
有什么建议么?