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. connect qaction from menubar to a slot in a different class

connect qaction from menubar to a slot in a different class

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 613 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 last edited by
    #1

    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.HilkJ 1 Reply Last reply
    0
    • H hobbyProgrammer

      @J-Hilk

      #include <QGraphicsView>
      #include <QDebug>
      
      class GraphicsView : public QGraphicsView
      {
      public:
          GraphicsView(QWidget *parent = 0);
      
      public slots:
          void zoomIn();
          void zoomOut();
      }
      
      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #4

      @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


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

      H 1 Reply Last reply
      2
      • H hobbyProgrammer

        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.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #2

        @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


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

        H 1 Reply Last reply
        0
        • J.HilkJ J.Hilk

          @hobbyProgrammer
          can you show the header of GraphicsView class?

          Probably a macro error/missing

          H Offline
          H Offline
          hobbyProgrammer
          wrote on last edited by
          #3

          @J-Hilk

          #include <QGraphicsView>
          #include <QDebug>
          
          class GraphicsView : public QGraphicsView
          {
          public:
              GraphicsView(QWidget *parent = 0);
          
          public slots:
              void zoomIn();
              void zoomOut();
          }
          
          J.HilkJ 1 Reply Last reply
          0
          • H hobbyProgrammer

            @J-Hilk

            #include <QGraphicsView>
            #include <QDebug>
            
            class GraphicsView : public QGraphicsView
            {
            public:
                GraphicsView(QWidget *parent = 0);
            
            public slots:
                void zoomIn();
                void zoomOut();
            }
            
            J.HilkJ Online
            J.HilkJ Online
            J.Hilk
            Moderators
            wrote on last edited by
            #4

            @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


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

            H 1 Reply Last reply
            2
            • J.HilkJ J.Hilk

              @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

              H Offline
              H Offline
              hobbyProgrammer
              wrote on last edited by
              #5

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

              J.HilkJ 1 Reply Last reply
              0
              • H hobbyProgrammer

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

                J.HilkJ Online
                J.HilkJ Online
                J.Hilk
                Moderators
                wrote on last edited by
                #6

                @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


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

                H 1 Reply Last reply
                1
                • J.HilkJ J.Hilk

                  @hobbyProgrammer clean the build, and rerun qmake

                  H Offline
                  H Offline
                  hobbyProgrammer
                  wrote on last edited by
                  #7

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

                  1 Reply Last reply
                  1
                  • J.HilkJ Online
                    J.HilkJ Online
                    J.Hilk
                    Moderators
                    wrote on last edited by
                    #8

                    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


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

                    1 Reply Last reply
                    1

                    • Login

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