我正面临处理Qt中的点击问题。我有以下类: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)
有什么建议吗?