我正面臨處理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)
有什麼建議嗎?