0

I'm trying to get my MyGraphicsRectItem (which is a subclass of QGraphicsRectItem and QObject) turn a slightly different color when I hover over it, so I've created a signal in the header file:

class MyGraphicsRectItem : public QObject, public QGraphicsRectItem
{
    Q_OBJECT
    ...
signals:
     void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
}

In my MyMainWindow, I'm connecting the hoverEnterEvent in this way (edit, clarification: a1 is a MyGraphicsRectItem):

connect(a1, &MyGraphicsRectItem::hoverEnterEvent, [this, i](QGraphicsSceneHoverEvent *event) {
            hoverRect(event, i);
});

Unfortunately, I get a QObject::connect: signal not found in MyGraphicsRectItem error message even though I clearly defined it in the header file. Any ideas?

Jimmy Kim
  • 93
  • 1
  • 8
  • The same approach as in the accepted answer should help you as well: https://stackoverflow.com/questions/16794695/connecting-overloaded-signals-and-slots-in-qt-5 – Alexander V Jun 19 '17 at 19:06
  • Hi, I don't think hoverEnterEvent is overloaded, so I'm not quite sure it applies. I did try implementing the solution, but it didn't do anything for me. – Jimmy Kim Jun 19 '17 at 20:09
  • Where/when do you see the error message -- at compile time or at run time? Also, what is the type of `a1` in the code shown? – G.M. Jun 19 '17 at 20:45
  • It is shown at runtime. a1 is of type MyGraphicsRectItem, which I personally defined. – Jimmy Kim Jun 19 '17 at 20:47
  • If it is runtime then the only guess is that the instance a1 is somehow not what expected. More complete code required then. – Alexander V Jun 19 '17 at 20:57
  • On the line before the connect statement, I have this: `MyGraphicsRectItem *a1 = new MyGraphicsRectItem(0, 0, colWidth, - (graphScene->height() - 70) * beaconTable->item(i, 1)->text().toFloat() / maxRSSI);` I don't see why a1 would be anything other than a MyGraphicsRectItem :/ – Jimmy Kim Jun 19 '17 at 21:01
  • Because it's a `MyGraphicsRectItem *` -- which is important. – G.M. Jun 19 '17 at 21:11
  • But doesn't connect need a `QObject*`? – Jimmy Kim Jun 19 '17 at 21:16
  • `QGraphicsItem` has a virtual method called [`hoverEnterEvent`](http://doc.qt.io/qt-5/qgraphicsitem.html#hoverEnterEvent). Change the name of your signal. Or even better, just paint on your item inside `MyGraphicsRectItem::paint` when the mouse is hovering over it. – thuga Jun 20 '17 at 08:51

1 Answers1

0

Your 3rd argument seems to be not correct.

There is an example for C++ lambdas in below example. (search for C++11 lambdas)

http://doc.qt.io/qt-5/signalsandslots.html

Based on that your third argument should be as shown below. Try around this

[=] (QGraphicsSceneHoverEvent *event,int i) { this->hoverRect(event, i);}

And over all the connect statement looks some what as shown below.

connect(a1, &MyGraphicsRectItem::hoverEnterEvent, [=] (QGraphicsSceneHoverEvent *event,int i) {
            this->hoverRect(event, i);
});
Pavan Chandaka
  • 8,891
  • 4
  • 17
  • 29
  • Hi, sorry for the late response, but the problem is hoverEnterEvent does not take in a second integer parameter, so I'm getting an incompatible slot and signal error using your solution, I tried editing your statement to [=, &i] to somehow pass in i as a variable, but it still doesn't find the signal. Let me know if you have any other suggestions. – Jimmy Kim Jun 19 '17 at 19:57
  • ok. Use QSignalmapper as shown in below example. https://stackoverflow.com/questions/21150890/qt-5-assign-slot-with-parameters-to-a-qpushbutton And then remove "I" from the connect statement in the example posted. – Pavan Chandaka Jun 19 '17 at 20:55