2

I'm faced with a problem to handle clicks in Qt. I have the following class:

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);
    }
};

Now I want to connect a slot to the signal, I do the following:

   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()));

When I run this, the circle is displayed on canvas but when I click on the circle nothing happens and console shows the following:

Object::connect: No such signal QGraphicsScene::selectionChanged(bool)

Any suggestions?

alexisdm
  • 29,448
  • 6
  • 64
  • 99
user1280078
  • 71
  • 1
  • 7

2 Answers2

4

Have you tried this already:

 QObject::connect(&canvas, SIGNAL(selectionChanged()), this, SLOT(test()));

As far as I know, the signal selectionChanged from QGraphicsScene, doesn't have any parameter: http://qt-project.org/doc/qt-4.8/qgraphicsscene.html#selectionChanged.

Here you are trying to connect the signal from QGRaphicsScene to the slot 'test', not the signal you have defined in MyRectItem. If you want to connect the signal from MyRectItem, you should do something like:

QObject::connect(i, SIGNAL(selectionChanged(bool)), this, SLOT(test()));

The first parameter is the source (sender) of the signal.

Gerald

Chris
  • 17,119
  • 5
  • 57
  • 60
nutrina
  • 1,002
  • 1
  • 12
  • 26
2

The console message is your answer. As you haven't specified the version of Qt you use, I'm agoing to assume 4.8 as the latest stable one. As can be seen from here, there really isn't such a signal as

selectionChanged(bool)

however, there IS a signal

selectionChanged()
Manjabes
  • 1,884
  • 3
  • 17
  • 34