1

disclaimer, i only just downloaded Qt today and have NO experience with it. so i'm sorry if this is a bit stupid. gotta start somewhere :).

i'll use [thing1] and [thing2], 1 being a qpolygon in a GraphicsWidget , 2 being a Widget.

[thing1] = scene->addPolygon([pathname],Pen,Brush)
ui->[thing2]->hide();
connect([thing1],SIGNAL(hovered()),ui->[thing2],SLOT(show()));

i'm trying to hide/show on a mouseover event, but i get the error

D:\Documents\Test\GUI\mainwindow.cpp:61: error: no matching function for call to 'MainWindow::connect(QGraphicsPolygonItem*&, const char*, MainWindow*, QTextEdit*&, const char*)'
 connect([thing1],SIGNAL(hovered()),this,ui->[thing2],SLOT(show()));
                                                                  ^
Seth
  • 11
  • 1
  • 3
    read the documentation and tutorials first when you have no experience. For signals/slots: the documentation of the classes tell you what signal/slot each class has. QPolygon e.g. has no signal called hovered(). But stackoverflow won't replace a tutorial for you. You should have some basic understanding of the things first. – Hayt Sep 16 '16 at 09:32
  • The error message doesn't match the code you posted. The error is about a call to connect with 5 parameters passed, your code passes 4. – Mat Sep 16 '16 at 09:39
  • Related question: http://stackoverflow.com/questions/2940392/qgraphicsitem-doesnt-receive-mouse-hover-events – frogatto Sep 16 '16 at 09:40

2 Answers2

0

NO!!

FYI:Signals and slots can be used by any qt objects, which a QPolygon is!

bool QObject::connect(const QObject * sender, const char * signal, const QObject * receiver, const char * method, Qt::ConnectionType type = Qt::AutoConnection)

the connect we use is actually QObject::connect(const QObject* sender_object, const char* signal, const QObject receiver_object, const char ), so it works on every QObject whether it is sending and receiving.

And in your case, as mentioned in the comments by hayt, QPolygon doesnt have hovered() signal, that's why it won't work. You should go to QPolygon documentation in qt official site and read it.

As far as i know there's no signal for QPolygon so it cannot be used in signals and slots :)

Anubhav Sahu
  • 71
  • 1
  • 9
0

Not always. In Qt 5 you can certainly connect a signal to "anything", e.g. a method on a non-qobject, or to a functor. But you can't connect a non-signal to anything, and there's no hovered signal on a QGraphicsPolygonItem because it's not a QObject, so it can't have any signals.

Instead, you need to create a filter object that will convert QGraphicsSceneEvent to a signal. E.g.:

// https://github.com/KubaO/stackoverflown/tree/master/questions/polygon-sigslot-39528030
#include <QtWidgets>

class HoverFilter : public QGraphicsObject {
    Q_OBJECT
    bool sceneEventFilter(QGraphicsItem * watched, QEvent *event) override {
        if (event->type() == QEvent::GraphicsSceneHoverEnter)
            emit hoverEnter(watched);
        else if (event->type() == QEvent::GraphicsSceneHoverLeave)
            emit hoverLeave(watched);
        return false;
    }
    QRectF boundingRect() const override { return QRectF{}; }
    void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *) override {}
public:
    Q_SIGNAL void hoverEnter(QGraphicsItem *);
    Q_SIGNAL void hoverLeave(QGraphicsItem *);
};

QPolygonF equilateralTriangle(qreal size) {
    return QPolygonF{{{0.,0.}, {size/2., -size*sqrt(3.)/2.}, {size,0.}, {0.,0.}}};
}

int main(int argc, char ** argv) {
    QApplication app{argc, argv};
    QWidget ui;
    QVBoxLayout layout{&ui};
    QGraphicsView view;
    QLabel label{"Hovering"};
    layout.addWidget(&view);
    layout.addWidget(&label);
    label.hide();
    ui.show();

    QGraphicsScene scene;
    view.setScene(&scene);
    HoverFilter filter;
    QGraphicsPolygonItem triangle{equilateralTriangle(100.)};
    scene.addItem(&filter);
    scene.addItem(&triangle);
    triangle.setAcceptHoverEvents(true);
    triangle.installSceneEventFilter(&filter);
    QObject::connect(&filter, &HoverFilter::hoverEnter, [&](QGraphicsItem * item) {
        if (item == &triangle) label.show();
    });
    QObject::connect(&filter, &HoverFilter::hoverLeave, [&](QGraphicsItem * item) {
        if (item == &triangle) label.hide();
    });
    return app.exec();
}
#include "main.moc"
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313