connecting context menu won't work
-
This post is deleted!
-
@hobbyProgrammer said in connecting context menu won't work:
connect(this, SIGNAL(triggered()), this, SLOT(removeItem(0,pos)));
This is not how signals/slots work.
You cannot pass parameters when do the connect().
You can use a lambda as slot where you capture the parameters.Also, the warning already tells you what is wrong: QGraphicsView has no signal triggered().
And I'm wondering: why do you want to connect signals and slots in the same instance of your class? You can simply call the slots directly instead.
One more thing: you actually want to connect signals from the actions to the slots:
QAction *alterPoly = new QAction("Alter Poly"); connect(alterPoly, SIGNAL(triggered()), this, SLOT(alterPoly()));
-
@hobbyProgrammer said in connecting context menu won't work:
What should I use in stead of trigger?
triggered() is correct but it comes from the action not this:
QAction *alterPoly = new QAction("Alter Poly"); connect(alterPoly, SIGNAL(triggered()), this, SLOT(alterPoly()));
-
graphicsView.h
private slots: void removeItem(bool typeItem, QPointF position); void showContextMenu(const QPoint &pos); void alterPoly(QPolygonF poly);
graphicsview.cpp
void graphicsView::showContextMenu(const QPoint &pos) { qDebug() << "showContextMenu was opened.. " << polygonOrPoint; QMenu menu; QAction *deletePoly = new QAction("Delete Poly"); connect(deletePoly, SIGNAL(triggered()), this, SLOT(removeItem(0,pos))); QAction *alterPoly = new QAction("Alter Poly"); connect(alterPoly, SIGNAL(triggered()), this, SLOT(alterPoly())); menu.addAction(deletePoly); menu.addAction(alterPoly); } void graphicsView::alterRoom(QPolygonF room) { QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Save | QDialogButtonBox::Cancel); if(buttonBox->button(QDialogButtonBox::Ok)) { qDebug() << "OK pressed"; } else if(buttonBox->button(QDialogButtonBox::Save)) { qDebug() << "Save pressed"; } else if(buttonBox->button(QDialogButtonBox::Cancel)) { qDebug() << "Cancel pressed"; } qDebug() << room; }
still gives me this error:
QObject::connect: No such slot graphicsView::removeItem(0,pos) in ..\scrollGraphicsView\graphicsview.cpp:391 QObject::connect: (receiver name: 'graphicsView') QObject::connect: No such slot graphicsView::alterRoom() in ..\scrollGraphicsView\graphicsview.cpp:393 QObject::connect: (receiver name: 'graphicsView')
-
@hobbyProgrammer said in connecting context menu won't work:
QObject::connect: No such slot graphicsView::removeItem(0,pos) in ..\scrollGraphicsView\graphicsview.cpp:391
I already told you why this can't work.
QObject::connect: No such slot graphicsView::alterRoom() in ..\scrollGraphicsView\graphicsview.cpp:393 - I don't see such a slot in the code you posted.
-
@jsulm
Oh I thought that changing it into :QAction *alterPoly = new QAction("Alter Poly"); connect(alterPoly, SIGNAL(triggered()), this, SLOT(alterPoly()));
would result into getting it working since it should call alterPoly->triggered().
I really don't know much about the connect function and the triggered function so maybe some explanation would be nice.Making code work is nice, but understanding it makes it so much easier.
@jsulm said in connecting context menu won't work:
QObject::connect: No such slot graphicsView::alterRoom() in ..\scrollGraphicsView\graphicsview.cpp:393 - I don't see such a slot in the code you posted.
sorry I updated the code, there are two exactly the same functions (alterPoly and alterRoom, but they both exists).
-
@hobbyProgrammer said in connecting context menu won't work:
connect(alterPoly, SIGNAL(triggered()), this, SLOT(alterPoly()));
In this line you connect triggered() signal from alterPoly to alterPoly() slot in graphicsView (as "this" is graphicsView here). That means that alterPoly() slot must exist in graphicsView class.
-
@hobbyProgrammer said in connecting context menu won't work:
sorry I updated the code, there are two exactly the same functions (alterPoly and alterRoom, but they both exists).
Is alterRoom delared as slot?
Can you show the class declaration? -
#ifndef GRAPHICSVIEW_H #define GRAPHICSVIEW_H #include <QWidget> #include <QGraphicsView> #include <QWheelEvent> #include <QMouseEvent> #include <QGraphicsScene> #include <QDebug> #include <QGraphicsPixmapItem> #include <QGraphicsItem> #include <QInputDialog> #include <QMessageBox> #include <QDir> #include <QMenu> #include <QDialogButtonBox> class graphicsView : public QGraphicsView { Q_OBJECT public: explicit graphicsView(QWidget *parent = nullptr); void openImage(QString); QGraphicsScene *scene; protected: virtual void wheelEvent(QWheelEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseReleaseEvent(QMouseEvent *event) override; public slots: void undoAction(); void redoAction(); private slots: void removeItem(bool typeItem, QPointF position); void showContextMenu(const QPoint &pos); void alterRoom(QPolygonF room); void alterPoly(QPolyF poly); private: int polygonOrPoint; }; #endif // GRAPHICSVIEW_H
-
@hobbyProgrammer said in connecting context menu won't work:
void alterRoom(QPolygonF room);
it has a parameter which you did not mention in your connect() call. But since the triggered() signal does not provide such a parameter it will not works anyway.
Do it with a lambda:QPolyF poly; connect(alterPoly, &QAction::triggered, [poly, this]() { alterPoly(poly); });