C++ Qt对象信号未连接到方法(处理程序)
我在处理Qt中的点击时遇到了一个问题。我有以下课程:C++ Qt对象信号未连接到方法(处理程序),c++,qt,qgraphicsscene,C++,Qt,Qgraphicsscene,我在处理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 te
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()));
当我运行此操作时,圆圈显示在画布上
,但当我单击圆圈时,什么也没有发生,控制台显示以下内容:
Object::connect: No such signal QGraphicsScene::selectionChanged(bool)
有什么建议吗?控制台消息就是您的答案。由于您还没有指定所使用的Qt版本,所以我希望假设4.8是最新的稳定版本。从中可以看出,确实没有这样的信号
selectionChanged(bool)
然而,有一个信号
selectionChanged()
你已经试过了吗:
QObject::connect(&canvas, SIGNAL(selectionChanged()), this, SLOT(test()));
据我所知,信号选择从QGraphicscene更改,没有任何参数:
这里您试图将QGraphicscene的信号连接到插槽“test”,而不是您在MyRectItem中定义的信号。如果要连接MyRectItem的信号,应执行以下操作:
QObject::connect(i, SIGNAL(selectionChanged(bool)), this, SLOT(test()));
第一个参数是信号的源(发送方)
杰拉尔德