Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved connect qaction from menubar to a slot in a different class

    General and Desktop
    2
    8
    224
    Loading More Posts
    • 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
      hobbyProgrammer last edited by

      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 1 Reply Last reply Reply Quote 0
      • J.Hilk
        J.Hilk Moderators @hobbyProgrammer last edited by

        @hobbyProgrammer
        yep, Q_OBJECT is missing

        class GraphicsView : public QGraphicsView
        {
            Q_OBJECT
        public:
            GraphicsView(QWidget *parent = 0);
        
        public slots:
            void zoomIn();
            void zoomOut();
        }
        

        it is essential for Signal & Slots to work

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

        Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        H 1 Reply Last reply Reply Quote 2
        • J.Hilk
          J.Hilk Moderators @hobbyProgrammer last edited by

          @hobbyProgrammer
          can you show the header of GraphicsView class?

          Probably a macro error/missing

          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

          Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          H 1 Reply Last reply Reply Quote 0
          • H
            hobbyProgrammer @J.Hilk last edited by

            @J-Hilk

            #include <QGraphicsView>
            #include <QDebug>
            
            class GraphicsView : public QGraphicsView
            {
            public:
                GraphicsView(QWidget *parent = 0);
            
            public slots:
                void zoomIn();
                void zoomOut();
            }
            
            J.Hilk 1 Reply Last reply Reply Quote 0
            • J.Hilk
              J.Hilk Moderators @hobbyProgrammer last edited by

              @hobbyProgrammer
              yep, Q_OBJECT is missing

              class GraphicsView : public QGraphicsView
              {
                  Q_OBJECT
              public:
                  GraphicsView(QWidget *parent = 0);
              
              public slots:
                  void zoomIn();
                  void zoomOut();
              }
              

              it is essential for Signal & Slots to work

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

              Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              H 1 Reply Last reply Reply Quote 2
              • H
                hobbyProgrammer @J.Hilk last edited by

                @J-Hilk okay but now I have a LNK2001 error. Saying "unresolved external symbol : public: virtual struct QMetaObject const*............"

                J.Hilk 1 Reply Last reply Reply Quote 0
                • J.Hilk
                  J.Hilk Moderators @hobbyProgrammer last edited by

                  @hobbyProgrammer clean the build, and rerun qmake

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  H 1 Reply Last reply Reply Quote 1
                  • H
                    hobbyProgrammer @J.Hilk last edited by

                    @J-Hilk thank you so much for your time and effort! It works now :)

                    1 Reply Last reply Reply Quote 1
                    • J.Hilk
                      J.Hilk Moderators last edited by

                      No problem 😀

                      Make sure to set the topic to solved, by using the topic tools, thanks!

                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct

                      Qt Needs YOUR vote: https://bugreports.qt.io/browse/QTQAINFRA-4121


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply Reply Quote 1
                      • First post
                        Last post