Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. connecting context menu won't work

connecting context menu won't work

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 2 Posters 814 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hobbyProgrammer
    wrote on 30 Oct 2019, 11:16 last edited by
    #1
    This post is deleted!
    J 1 Reply Last reply 30 Oct 2019, 11:18
    0
    • H hobbyProgrammer
      30 Oct 2019, 11:16

      This post is deleted!

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 30 Oct 2019, 11:18 last edited by jsulm
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply 30 Oct 2019, 11:21
      1
      • J jsulm
        30 Oct 2019, 11:18

        @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()));
        
        H Offline
        H Offline
        hobbyProgrammer
        wrote on 30 Oct 2019, 11:21 last edited by
        #3

        @jsulm

        hi,
        thank you for your reply.

        I would like to create a context menu with 2 options and would like to connect different functions to these actions.
        What should I use in stead of trigger?

        J 1 Reply Last reply 30 Oct 2019, 11:23
        0
        • H hobbyProgrammer
          30 Oct 2019, 11:21

          @jsulm

          hi,
          thank you for your reply.

          I would like to create a context menu with 2 options and would like to connect different functions to these actions.
          What should I use in stead of trigger?

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 30 Oct 2019, 11:23 last edited by
          #4

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

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • H Offline
            H Offline
            hobbyProgrammer
            wrote on 30 Oct 2019, 11:32 last edited by hobbyProgrammer
            #5

            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')
            
            J 1 Reply Last reply 30 Oct 2019, 11:33
            0
            • H hobbyProgrammer
              30 Oct 2019, 11:32

              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')
              
              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 30 Oct 2019, 11:33 last edited by jsulm
              #6

              @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.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              H 1 Reply Last reply 30 Oct 2019, 11:36
              0
              • J jsulm
                30 Oct 2019, 11:33

                @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.

                H Offline
                H Offline
                hobbyProgrammer
                wrote on 30 Oct 2019, 11:36 last edited by hobbyProgrammer
                #7

                @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).

                J 2 Replies Last reply 30 Oct 2019, 11:41
                0
                • H hobbyProgrammer
                  30 Oct 2019, 11:36

                  @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).

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 30 Oct 2019, 11:41 last edited by
                  #8

                  @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.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • H hobbyProgrammer
                    30 Oct 2019, 11:36

                    @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).

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 30 Oct 2019, 11:42 last edited by
                    #9

                    @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?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    H 1 Reply Last reply 30 Oct 2019, 11:48
                    0
                    • J jsulm
                      30 Oct 2019, 11:42

                      @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?

                      H Offline
                      H Offline
                      hobbyProgrammer
                      wrote on 30 Oct 2019, 11:48 last edited by
                      #10

                      @jsulm

                      #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
                      
                      
                      J 1 Reply Last reply 30 Oct 2019, 11:52
                      0
                      • H hobbyProgrammer
                        30 Oct 2019, 11:48

                        @jsulm

                        #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
                        
                        
                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 30 Oct 2019, 11:52 last edited by
                        #11

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

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        1

                        1/11

                        30 Oct 2019, 11:16

                        • Login

                        • Login or register to search.
                        1 out of 11
                        • First post
                          1/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved