connect qaction from menubar to a slot in a different class
-
I would like to connect a qAction to a slot that exists in a different class, but I don't seem to get it right.
I get the error QObject::connect: No such slot QGraphicsView::zoomIn() in ..This is my code:
GraphicsView *graphicsview = new GraphicsView; connect(ui->actionZoom_in, SIGNAL(triggered()), graphicsview, SLOT(zoomIn())); connect(ui->actionZoom_ou, SIGNAL(triggered()), graphicsview, SLOT(zoomOut()));
As you can see, I created my own QGraphicsView class called GraphicsView and this is the code for the zoomIn() and zoomOut() functions:
void GraphicsView::zoomIn() { qDebug() << "Zoom in"; } void GraphicsView::zoomOut() { qDebug() << "Zoom out"; }
-
@J-Hilk
#include <QGraphicsView> #include <QDebug> class GraphicsView : public QGraphicsView { public: GraphicsView(QWidget *parent = 0); public slots: void zoomIn(); void zoomOut(); }
@hobbyProgrammer
yep, Q_OBJECT is missingclass GraphicsView : public QGraphicsView { Q_OBJECT public: GraphicsView(QWidget *parent = 0); public slots: void zoomIn(); void zoomOut(); }
it is essential for Signal & Slots to work
-
I would like to connect a qAction to a slot that exists in a different class, but I don't seem to get it right.
I get the error QObject::connect: No such slot QGraphicsView::zoomIn() in ..This is my code:
GraphicsView *graphicsview = new GraphicsView; connect(ui->actionZoom_in, SIGNAL(triggered()), graphicsview, SLOT(zoomIn())); connect(ui->actionZoom_ou, SIGNAL(triggered()), graphicsview, SLOT(zoomOut()));
As you can see, I created my own QGraphicsView class called GraphicsView and this is the code for the zoomIn() and zoomOut() functions:
void GraphicsView::zoomIn() { qDebug() << "Zoom in"; } void GraphicsView::zoomOut() { qDebug() << "Zoom out"; }
@hobbyProgrammer
can you show the header of GraphicsView class?Probably a macro error/missing
-
@hobbyProgrammer
can you show the header of GraphicsView class?Probably a macro error/missing
@J-Hilk
#include <QGraphicsView> #include <QDebug> class GraphicsView : public QGraphicsView { public: GraphicsView(QWidget *parent = 0); public slots: void zoomIn(); void zoomOut(); }
-
@J-Hilk
#include <QGraphicsView> #include <QDebug> class GraphicsView : public QGraphicsView { public: GraphicsView(QWidget *parent = 0); public slots: void zoomIn(); void zoomOut(); }
@hobbyProgrammer
yep, Q_OBJECT is missingclass GraphicsView : public QGraphicsView { Q_OBJECT public: GraphicsView(QWidget *parent = 0); public slots: void zoomIn(); void zoomOut(); }
it is essential for Signal & Slots to work
-
@hobbyProgrammer
yep, Q_OBJECT is missingclass GraphicsView : public QGraphicsView { Q_OBJECT public: GraphicsView(QWidget *parent = 0); public slots: void zoomIn(); void zoomOut(); }
it is essential for Signal & Slots to work
@J-Hilk okay but now I have a LNK2001 error. Saying "unresolved external symbol : public: virtual struct QMetaObject const*............"
-
@J-Hilk okay but now I have a LNK2001 error. Saying "unresolved external symbol : public: virtual struct QMetaObject const*............"
@hobbyProgrammer clean the build, and rerun qmake
-
@hobbyProgrammer clean the build, and rerun qmake
@J-Hilk thank you so much for your time and effort! It works now :)
-
No problem 😀
Make sure to set the topic to solved, by using the topic tools, thanks!