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. How to use methods of an object from a different class?

How to use methods of an object from a different class?

Scheduled Pinned Locked Moved Solved General and Desktop
29 Posts 4 Posters 6.9k 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.
  • E Offline
    E Offline
    Eeli K
    wrote on last edited by
    #3

    The two classes are unnecessarily tightly coupled, as far as I can see the keyboard class doesn't have to know the main window. Just write a signal in it, emit it in on_button_clicked() and connect it in the mainwindow to the slot mainwindow::send (which of course must be a slot).

    B 1 Reply Last reply
    3
    • E Eeli K

      The two classes are unnecessarily tightly coupled, as far as I can see the keyboard class doesn't have to know the main window. Just write a signal in it, emit it in on_button_clicked() and connect it in the mainwindow to the slot mainwindow::send (which of course must be a slot).

      B Offline
      B Offline
      bask185
      wrote on last edited by bask185
      #4

      @Eeli-K said in How to use methods of an object from a different class?:

      Just write a signal in it, emit it in on_button_clicked() and connect it in the mainwindow to the slot mainwindow::send

      ?? and now in English??

      I have this in mainwindow

      void MainWindow::send(char c)
      {
        connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
        serial->write("c"); // I am aware that this does not send the value of variable c just "c"
        qDebug() << c;
      }
      

      and in Keyboard I do

      void Keyboard::on_a_clicked()
      {
         QString c;
         c = ui->a->text();
         MainWindow::send('a');
      }
      

      in main window I get 2 errors

      C:\Users\sknippels\Documents\GUI\mainwindow.cpp:65: error: cannot call member function 'void Keyboard::on_a_clicked()' without object
         connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
                                                   ^
      

      and

      C:\Users\sknippels\Documents\GUI\mainwindow.cpp:65: error: invalid operands of types 'MainWindow*' and 'void' to binary 'operator&'
         connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
                                                                              ^
      

      I understand less than nothing of this vague syntax. And I also never use pointers, I would only use a pointer for use with arrays but because there are those things called for-loops... I just don't do pointers.

      EDIT. I made some changes, I also noticed a typo. I move this line to the constructor (and removed the typo)

      connect(keyboard, &Keyboard::on_a_clicked, this, &MainWindow::send);
      

      and now I have 3 error messages in qobjectdefs_impl.h

      C:\Qt\5.8\mingw53_32\include\QtCore\qglobal.h:732: error: static assertion failed: The slot requires more arguments than the signal provides.
       #define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
                                                     ^
      
      C:\Qt\5.8\mingw53_32\include\QtCore\qglobal.h:732: error: static assertion failed: Signal and slot arguments are not compatible.
       #define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
                                                     ^
      
      C:\Qt\5.8\mingw53_32\include\QtCore\qobjectdefs_impl.h:76: error: no type named 'Car' in 'struct QtPrivate::List<>'
               typedef typename List_Append<List<typename L::Car>,typename List_Left<typename L::Cdr, N - 1>::Value>::Value Value;
                                                                                                                            ^
      

      For me this is total Chinese :(

      J.HilkJ 1 Reply Last reply
      0
      • B bask185

        @Eeli-K said in How to use methods of an object from a different class?:

        Just write a signal in it, emit it in on_button_clicked() and connect it in the mainwindow to the slot mainwindow::send

        ?? and now in English??

        I have this in mainwindow

        void MainWindow::send(char c)
        {
          connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
          serial->write("c"); // I am aware that this does not send the value of variable c just "c"
          qDebug() << c;
        }
        

        and in Keyboard I do

        void Keyboard::on_a_clicked()
        {
           QString c;
           c = ui->a->text();
           MainWindow::send('a');
        }
        

        in main window I get 2 errors

        C:\Users\sknippels\Documents\GUI\mainwindow.cpp:65: error: cannot call member function 'void Keyboard::on_a_clicked()' without object
           connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
                                                     ^
        

        and

        C:\Users\sknippels\Documents\GUI\mainwindow.cpp:65: error: invalid operands of types 'MainWindow*' and 'void' to binary 'operator&'
           connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
                                                                                ^
        

        I understand less than nothing of this vague syntax. And I also never use pointers, I would only use a pointer for use with arrays but because there are those things called for-loops... I just don't do pointers.

        EDIT. I made some changes, I also noticed a typo. I move this line to the constructor (and removed the typo)

        connect(keyboard, &Keyboard::on_a_clicked, this, &MainWindow::send);
        

        and now I have 3 error messages in qobjectdefs_impl.h

        C:\Qt\5.8\mingw53_32\include\QtCore\qglobal.h:732: error: static assertion failed: The slot requires more arguments than the signal provides.
         #define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
                                                       ^
        
        C:\Qt\5.8\mingw53_32\include\QtCore\qglobal.h:732: error: static assertion failed: Signal and slot arguments are not compatible.
         #define Q_STATIC_ASSERT_X(Condition, Message) static_assert(bool(Condition), Message)
                                                       ^
        
        C:\Qt\5.8\mingw53_32\include\QtCore\qobjectdefs_impl.h:76: error: no type named 'Car' in 'struct QtPrivate::List<>'
                 typedef typename List_Append<List<typename L::Car>,typename List_Left<typename L::Cdr, N - 1>::Value>::Value Value;
                                                                                                                              ^
        

        For me this is total Chinese :(

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #5

        @bask185
        hi,

        I'll try to help:

        Your custom Keyboard, as it is made in a new class, has to be initialized somewhere.

        I asume something like this in your MainWindow constructor:

        //Something like this
        Keyboard kBoard;
        
        // or
        Keyboard *kBoard = new Keyboard();
        
        // or 
        Keyboard *kBoard = new Keyboard(anyParent);
        

        kBoard is in this case the object.

        in your KeyBorad header you need a SIGNAL

        signals:
        void keyPressed(QString key);
        

        change your on_click function:

        void Keyboard::on_a_clicked()
        {
           QString c;
           c = ui->a->text();
           emit KeyPressed(c);
        }
        

        Move

        connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
        

        from the SLOT send(c) to right behind the object creation and change it to the following to remove the error:

        Keyboard *kBoard = new Keyboard();
        connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
        

        and you should be done :)


        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.

        B jsulmJ 2 Replies Last reply
        0
        • J.HilkJ J.Hilk

          @bask185
          hi,

          I'll try to help:

          Your custom Keyboard, as it is made in a new class, has to be initialized somewhere.

          I asume something like this in your MainWindow constructor:

          //Something like this
          Keyboard kBoard;
          
          // or
          Keyboard *kBoard = new Keyboard();
          
          // or 
          Keyboard *kBoard = new Keyboard(anyParent);
          

          kBoard is in this case the object.

          in your KeyBorad header you need a SIGNAL

          signals:
          void keyPressed(QString key);
          

          change your on_click function:

          void Keyboard::on_a_clicked()
          {
             QString c;
             c = ui->a->text();
             emit KeyPressed(c);
          }
          

          Move

          connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
          

          from the SLOT send(c) to right behind the object creation and change it to the following to remove the error:

          Keyboard *kBoard = new Keyboard();
          connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
          

          and you should be done :)

          B Offline
          B Offline
          bask185
          wrote on last edited by
          #6

          @J.Hilk said in How to use methods of an object from a different class?:

          @bask185
          hi,

          I'll try to help:

          Your custom Keyboard, as it is made in a new class, has to be initialized somewhere.

          I asume something like this in your MainWindow constructor:

          //Something like this
          Keyboard kBoard;
          
          // or
          Keyboard *kBoard = new Keyboard();
          
          // or 
          Keyboard *kBoard = new Keyboard(anyParent);
          

          kBoard is in this case the object.

          in your KeyBorad header you need a SIGNAL

          signals:
          void keyPressed(QString key);
          

          change your on_click function:

          void Keyboard::on_a_clicked()
          {
             QString c;
             c = ui->a->text();
             emit KeyPressed(c);
          }
          

          Move

          connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
          

          from the SLOT send(c) to right behind the object creation and change it to the following to remove the error:

          Keyboard *kBoard = new Keyboard();
          connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
          

          and you should be done :)

          Tnx I am closer but I got one error message left.
          in

          Keyboard *keyboard = new Keyboard();
             connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
          

          I get

          C:\Users\sknippels\Documents\GUI\mainwindow.cpp:52: error: invalid use of member (did you forget the '&' ?)
              connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                                                         ^
          

          it complains about the & eventhough it is there.. :/

          J.HilkJ jsulmJ 2 Replies Last reply
          0
          • B bask185

            @J.Hilk said in How to use methods of an object from a different class?:

            @bask185
            hi,

            I'll try to help:

            Your custom Keyboard, as it is made in a new class, has to be initialized somewhere.

            I asume something like this in your MainWindow constructor:

            //Something like this
            Keyboard kBoard;
            
            // or
            Keyboard *kBoard = new Keyboard();
            
            // or 
            Keyboard *kBoard = new Keyboard(anyParent);
            

            kBoard is in this case the object.

            in your KeyBorad header you need a SIGNAL

            signals:
            void keyPressed(QString key);
            

            change your on_click function:

            void Keyboard::on_a_clicked()
            {
               QString c;
               c = ui->a->text();
               emit KeyPressed(c);
            }
            

            Move

            connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
            

            from the SLOT send(c) to right behind the object creation and change it to the following to remove the error:

            Keyboard *kBoard = new Keyboard();
            connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
            

            and you should be done :)

            Tnx I am closer but I got one error message left.
            in

            Keyboard *keyboard = new Keyboard();
               connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
            

            I get

            C:\Users\sknippels\Documents\GUI\mainwindow.cpp:52: error: invalid use of member (did you forget the '&' ?)
                connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                                                           ^
            

            it complains about the & eventhough it is there.. :/

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #7

            @bask185

            mmh,
            did you declare send as a slot in your mainwindow cpp?

            private slots:
            //public slots:
                 void send(QString s);
            

            Edit:

            just noticed, your Send-Function wants a char and you send a QString that will most likly not work.

            change one or the other. I would suggest chaning the Mainwindow::Send to accept QString, makes it easier.


            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
            0
            • B bask185

              @J.Hilk said in How to use methods of an object from a different class?:

              @bask185
              hi,

              I'll try to help:

              Your custom Keyboard, as it is made in a new class, has to be initialized somewhere.

              I asume something like this in your MainWindow constructor:

              //Something like this
              Keyboard kBoard;
              
              // or
              Keyboard *kBoard = new Keyboard();
              
              // or 
              Keyboard *kBoard = new Keyboard(anyParent);
              

              kBoard is in this case the object.

              in your KeyBorad header you need a SIGNAL

              signals:
              void keyPressed(QString key);
              

              change your on_click function:

              void Keyboard::on_a_clicked()
              {
                 QString c;
                 c = ui->a->text();
                 emit KeyPressed(c);
              }
              

              Move

              connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
              

              from the SLOT send(c) to right behind the object creation and change it to the following to remove the error:

              Keyboard *kBoard = new Keyboard();
              connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
              

              and you should be done :)

              Tnx I am closer but I got one error message left.
              in

              Keyboard *keyboard = new Keyboard();
                 connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
              

              I get

              C:\Users\sknippels\Documents\GUI\mainwindow.cpp:52: error: invalid use of member (did you forget the '&' ?)
                  connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                                                             ^
              

              it complains about the & eventhough it is there.. :/

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #8

              @bask185 said in How to use methods of an object from a different class?:

              Keyboard *keyboard = new Keyboard();
              connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);

              Aren't you missing a comma?

              Keyboard *keyboard = new Keyboard();
                 connect(keyboard, &Keyboard::KeyPressed, this, &MainWindow::send);
              

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

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

                @bask185
                hi,

                I'll try to help:

                Your custom Keyboard, as it is made in a new class, has to be initialized somewhere.

                I asume something like this in your MainWindow constructor:

                //Something like this
                Keyboard kBoard;
                
                // or
                Keyboard *kBoard = new Keyboard();
                
                // or 
                Keyboard *kBoard = new Keyboard(anyParent);
                

                kBoard is in this case the object.

                in your KeyBorad header you need a SIGNAL

                signals:
                void keyPressed(QString key);
                

                change your on_click function:

                void Keyboard::on_a_clicked()
                {
                   QString c;
                   c = ui->a->text();
                   emit KeyPressed(c);
                }
                

                Move

                connect(keyboard, &Keyboard::on_a_clicked(), this &MainWindow::send(c));
                

                from the SLOT send(c) to right behind the object creation and change it to the following to remove the error:

                Keyboard *kBoard = new Keyboard();
                connect(keyboard, &Keyboard::KeyPressed, this &MainWindow::send);
                

                and you should be done :)

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #9

                @J.Hilk said in How to use methods of an object from a different class?:

                //Something like this
                Keyboard kBoard;

                Please do NOT do this in the constructor: kBoard would be a local variable and would disappear when the constructor finishes!

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

                J.HilkJ 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @J.Hilk said in How to use methods of an object from a different class?:

                  //Something like this
                  Keyboard kBoard;

                  Please do NOT do this in the constructor: kBoard would be a local variable and would disappear when the constructor finishes!

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #10

                  @jsulm @bask185
                  Oh, sorry about that.
                  ApparentlyI missed that when I wrote my answer.

                  I tried to cover all bases, just in case, but that's an error my from my part.


                  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
                  0
                  • B Offline
                    B Offline
                    bask185
                    wrote on last edited by
                    #11

                    @jsulm said in How to use methods of an object from a different class?:

                    Aren't you missing a comma?

                    That is what I thought as well, but no. Adding comma will give errors

                    @jsulm said in How to use methods of an object from a different class?:

                    Please do NOT do this in the constructor: kBoard would be a local variable and would disappear when the constructor finishes!

                    at first I used Keyboard *keyboard = new Keyboard(); with those strange pointer things. Now I declare keyboard with the rest of my variables and I initialize it in the constructor.

                    @J.Hilk said in How to use methods of an object from a different class?:

                    ust noticed, your Send-Function wants a char and you send a QString that will most likly not work

                    I also noticed that and changed it already.

                    There are a few more things I found out. I was using 'keyboard' at first, but that turns red so it might interfere. I swapped out 'keyboard' for Kboard. But my way of decalaring it in the variable section and initializing it in the constructor suddenly upset the compiler so I went back to the constructor and now I have:

                    Keyboard *Kboard = new Keyboard();
                       connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                    

                    also in keyboard.h I changed private by public because I was getting an error message there at first.

                    But I am still having this error in the connect function:

                    C:\Users\sknippels\Documents\GUI\mainwindow.cpp:51: error: invalid use of member (did you forget the '&' ?)
                        connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                                                                 ^
                    

                    how my code looks like atm:

                    Keyboard *Kboard = new Keyboard();  // mainwindow.cpp constructor
                       connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                    
                    void MainWindow::send(QString c)  // send function in mainwindow.ccp
                    {
                      serial->write("c"); // still does not send the value of c, just "c"
                      qDebug() << c;
                    }
                    
                    void Keyboard::on_a_clicked() // push button function in keyboard.cpp
                    {
                       QString c;
                       c = ui->a->text();
                       emit KeyPressed(c);
                       }
                    
                    public slots: // keyboard.h
                        void KeyPressed(QString c); // also tried KeyPressed(QString); and KeyPressed(); no effect
                    }
                    
                    private slots: //mainwindow.h
                        void serialReceived(); // one of many other functions
                        void send(QString c);
                    
                    

                    I think we have all the pieces of this strange C++ puzzle now

                    jsulmJ J.HilkJ 2 Replies Last reply
                    0
                    • B bask185

                      @jsulm said in How to use methods of an object from a different class?:

                      Aren't you missing a comma?

                      That is what I thought as well, but no. Adding comma will give errors

                      @jsulm said in How to use methods of an object from a different class?:

                      Please do NOT do this in the constructor: kBoard would be a local variable and would disappear when the constructor finishes!

                      at first I used Keyboard *keyboard = new Keyboard(); with those strange pointer things. Now I declare keyboard with the rest of my variables and I initialize it in the constructor.

                      @J.Hilk said in How to use methods of an object from a different class?:

                      ust noticed, your Send-Function wants a char and you send a QString that will most likly not work

                      I also noticed that and changed it already.

                      There are a few more things I found out. I was using 'keyboard' at first, but that turns red so it might interfere. I swapped out 'keyboard' for Kboard. But my way of decalaring it in the variable section and initializing it in the constructor suddenly upset the compiler so I went back to the constructor and now I have:

                      Keyboard *Kboard = new Keyboard();
                         connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                      

                      also in keyboard.h I changed private by public because I was getting an error message there at first.

                      But I am still having this error in the connect function:

                      C:\Users\sknippels\Documents\GUI\mainwindow.cpp:51: error: invalid use of member (did you forget the '&' ?)
                          connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                                                                   ^
                      

                      how my code looks like atm:

                      Keyboard *Kboard = new Keyboard();  // mainwindow.cpp constructor
                         connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                      
                      void MainWindow::send(QString c)  // send function in mainwindow.ccp
                      {
                        serial->write("c"); // still does not send the value of c, just "c"
                        qDebug() << c;
                      }
                      
                      void Keyboard::on_a_clicked() // push button function in keyboard.cpp
                      {
                         QString c;
                         c = ui->a->text();
                         emit KeyPressed(c);
                         }
                      
                      public slots: // keyboard.h
                          void KeyPressed(QString c); // also tried KeyPressed(QString); and KeyPressed(); no effect
                      }
                      
                      private slots: //mainwindow.h
                          void serialReceived(); // one of many other functions
                          void send(QString c);
                      
                      

                      I think we have all the pieces of this strange C++ puzzle now

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #12

                      @bask185 You're still missing the comma, please add it as it is needed. And if you then still get an error then please post it here: just saying there is an error does not help.

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

                      B 1 Reply Last reply
                      0
                      • B bask185

                        @jsulm said in How to use methods of an object from a different class?:

                        Aren't you missing a comma?

                        That is what I thought as well, but no. Adding comma will give errors

                        @jsulm said in How to use methods of an object from a different class?:

                        Please do NOT do this in the constructor: kBoard would be a local variable and would disappear when the constructor finishes!

                        at first I used Keyboard *keyboard = new Keyboard(); with those strange pointer things. Now I declare keyboard with the rest of my variables and I initialize it in the constructor.

                        @J.Hilk said in How to use methods of an object from a different class?:

                        ust noticed, your Send-Function wants a char and you send a QString that will most likly not work

                        I also noticed that and changed it already.

                        There are a few more things I found out. I was using 'keyboard' at first, but that turns red so it might interfere. I swapped out 'keyboard' for Kboard. But my way of decalaring it in the variable section and initializing it in the constructor suddenly upset the compiler so I went back to the constructor and now I have:

                        Keyboard *Kboard = new Keyboard();
                           connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                        

                        also in keyboard.h I changed private by public because I was getting an error message there at first.

                        But I am still having this error in the connect function:

                        C:\Users\sknippels\Documents\GUI\mainwindow.cpp:51: error: invalid use of member (did you forget the '&' ?)
                            connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                                                                     ^
                        

                        how my code looks like atm:

                        Keyboard *Kboard = new Keyboard();  // mainwindow.cpp constructor
                           connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                        
                        void MainWindow::send(QString c)  // send function in mainwindow.ccp
                        {
                          serial->write("c"); // still does not send the value of c, just "c"
                          qDebug() << c;
                        }
                        
                        void Keyboard::on_a_clicked() // push button function in keyboard.cpp
                        {
                           QString c;
                           c = ui->a->text();
                           emit KeyPressed(c);
                           }
                        
                        public slots: // keyboard.h
                            void KeyPressed(QString c); // also tried KeyPressed(QString); and KeyPressed(); no effect
                        }
                        
                        private slots: //mainwindow.h
                            void serialReceived(); // one of many other functions
                            void send(QString c);
                        
                        

                        I think we have all the pieces of this strange C++ puzzle now

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #13

                        @bask185

                        additionally to what @jsulm said,

                        
                        public slots: // keyboard.h
                            void KeyPressed(QString c); // also tried KeyPressed(QString); and KeyPressed(); no effect
                        
                        

                        ought to be a signal

                        signals: // keyboard.h
                            void KeyPressed(QString c); // also tried KeyPressed(QString); and KeyPressed(); no effect
                        
                        

                        not a slot


                        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
                        • jsulmJ jsulm

                          @bask185 You're still missing the comma, please add it as it is needed. And if you then still get an error then please post it here: just saying there is an error does not help.

                          B Offline
                          B Offline
                          bask185
                          wrote on last edited by
                          #14

                          @jsulm said in How to use methods of an object from a different class?:

                          @bask185 You're still missing the comma, please add it as it is needed. And if you then still get an error then please post it here: just saying there is an error does not help.

                          Keyboard *Kboard = new Keyboard();
                             //connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                             connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
                          

                          V

                          C:\Users\sknippels\Documents\GUI\mainwindow.cpp:52: error: undefined reference to `Keyboard::KeyPressed(QString)'
                          
                          C:\Users\sknippels\Documents\GUI\keyboard.cpp:76: error: undefined reference to `Keyboard::KeyPressed(QString)'
                          
                          C:\Users\sknippels\Documents\build-GUI-Desktop_Qt_5_8_0_MinGW_32bit-Debug\debug\moc_keyboard.cpp:251: error: undefined reference to `Keyboard::KeyPressed(QString)'
                          
                          collect2.exe:-1: error: error: ld returned 1 exit status
                          
                          jsulmJ 1 Reply Last reply
                          0
                          • B bask185

                            @jsulm said in How to use methods of an object from a different class?:

                            @bask185 You're still missing the comma, please add it as it is needed. And if you then still get an error then please post it here: just saying there is an error does not help.

                            Keyboard *Kboard = new Keyboard();
                               //connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                               connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
                            

                            V

                            C:\Users\sknippels\Documents\GUI\mainwindow.cpp:52: error: undefined reference to `Keyboard::KeyPressed(QString)'
                            
                            C:\Users\sknippels\Documents\GUI\keyboard.cpp:76: error: undefined reference to `Keyboard::KeyPressed(QString)'
                            
                            C:\Users\sknippels\Documents\build-GUI-Desktop_Qt_5_8_0_MinGW_32bit-Debug\debug\moc_keyboard.cpp:251: error: undefined reference to `Keyboard::KeyPressed(QString)'
                            
                            collect2.exe:-1: error: error: ld returned 1 exit status
                            
                            jsulmJ Offline
                            jsulmJ Offline
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #15

                            @bask185 Please read what @J-Hilk wrote

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

                            B 1 Reply Last reply
                            0
                            • jsulmJ jsulm

                              @bask185 Please read what @J-Hilk wrote

                              B Offline
                              B Offline
                              bask185
                              wrote on last edited by
                              #16

                              @jsulm said in How to use methods of an object from a different class?:

                              @bask185 Please read what @J-Hilk wrote

                              I did and changed it and I can finally atleast build it. only pressing the 'a' on the keyboard does not do anything.

                              I know that void Keyboard::on_a_clicked() gets executed using Qdebug I can also tell c has the correct value.

                              But in mainwindow void MainWindow::send(QString c) does not get executed. I added qDebug() << "hello world"; but nothing happens :(

                              jsulmJ 1 Reply Last reply
                              0
                              • B bask185

                                @jsulm said in How to use methods of an object from a different class?:

                                @bask185 Please read what @J-Hilk wrote

                                I did and changed it and I can finally atleast build it. only pressing the 'a' on the keyboard does not do anything.

                                I know that void Keyboard::on_a_clicked() gets executed using Qdebug I can also tell c has the correct value.

                                But in mainwindow void MainWindow::send(QString c) does not get executed. I added qDebug() << "hello world"; but nothing happens :(

                                jsulmJ Offline
                                jsulmJ Offline
                                jsulm
                                Lifetime Qt Champion
                                wrote on last edited by
                                #17

                                @bask185 Can you show the whole code where

                                Keyboard *Kboard = new Keyboard();
                                   //connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                   connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
                                

                                is embedded?

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

                                B 1 Reply Last reply
                                0
                                • jsulmJ jsulm

                                  @bask185 Can you show the whole code where

                                  Keyboard *Kboard = new Keyboard();
                                     //connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                     connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
                                  

                                  is embedded?

                                  B Offline
                                  B Offline
                                  bask185
                                  wrote on last edited by
                                  #18

                                  @jsulm said in How to use methods of an object from a different class?:

                                  @bask185 Can you show the whole code where

                                  Keyboard *Kboard = new Keyboard();
                                     //connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                     connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
                                  

                                  is embedded?
                                  I don't know what you mean??

                                  I moved Keyboard *Kboard; to the global variables of mainwindow but it seems that does not make a difference. Button 'a' of the keyboard still does not trigger the send function in the main window

                                  jsulmJ J.HilkJ 2 Replies Last reply
                                  0
                                  • B bask185

                                    @jsulm said in How to use methods of an object from a different class?:

                                    @bask185 Can you show the whole code where

                                    Keyboard *Kboard = new Keyboard();
                                       //connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                       connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
                                    

                                    is embedded?
                                    I don't know what you mean??

                                    I moved Keyboard *Kboard; to the global variables of mainwindow but it seems that does not make a difference. Button 'a' of the keyboard still does not trigger the send function in the main window

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #19

                                    @bask185 Can you please post your code? Else I don't know what is wrong.

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

                                    1 Reply Last reply
                                    0
                                    • B bask185

                                      @jsulm said in How to use methods of an object from a different class?:

                                      @bask185 Can you show the whole code where

                                      Keyboard *Kboard = new Keyboard();
                                         //connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                         connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
                                      

                                      is embedded?
                                      I don't know what you mean??

                                      I moved Keyboard *Kboard; to the global variables of mainwindow but it seems that does not make a difference. Button 'a' of the keyboard still does not trigger the send function in the main window

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #20

                                      @bask185

                                      what @jsulm meant, is, can you post the whole content between the brackets {} where

                                      Keyboard *Kboard = new Keyboard();
                                         //connect(Kboard, &Keyboard::KeyPressed, this &MainWindow::send);
                                         connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
                                      

                                      is copied out of.


                                      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
                                      0
                                      • B Offline
                                        B Offline
                                        bask185
                                        wrote on last edited by
                                        #21

                                        The one with the comma remains ;)

                                        #include "mainwindow.h" // mainwindow.cpp
                                        #include "ui_mainwindow.h"
                                        #include <QPixmap>
                                        #include <QDebug>
                                        #include <QPalette>
                                        #include <QtSerialPort>
                                        #include "keyboard.h"
                                        #include "numpad.h"
                                        QSerialPort *serial;
                                        void readCommand();
                                        
                                        char lettres[5][40][16];     // initializes all to be used variables;
                                        int index1 = 0;
                                        int index2 = 0;
                                        int colom[5];
                                        int row[5];
                                        int colorIndex[5];
                                        char c;
                                        Keyboard *Kboard;
                                        
                                        MainWindow::MainWindow(QWidget *parent) :
                                            QMainWindow(parent),
                                            ui(new Ui::MainWindow)
                                        {
                                            ui->setupUi(this);
                                            serial = new QSerialPort();
                                            serial->setPortName("COM4");
                                            serial->open(QIODevice::ReadWrite);
                                            serial->setBaudRate(QSerialPort::Baud115200);
                                            serial->setDataBits(QSerialPort::Data8);
                                            serial->setParity(QSerialPort::OddParity);
                                            serial->setStopBits(QSerialPort::OneStop);
                                            //serial.setFlowControl(QSerialPort::NoFlowControl);
                                            //serial.open(QIODevice::ReadWrite);
                                        
                                            QPixmap logo(":/resources/img/logo.jpg");
                                            int w = ui->label->width();
                                            int h = ui->label->height();
                                            ui->label->setPixmap(logo.scaled(w,h,Qt::KeepAspectRatio));
                                        
                                            for(int i=0;i<5;i++) {
                                                for (int j=0;j<40;j++) {
                                                    for (int k=0;k<16;k++){
                                                        lettres[i][j][k] =' ';   // initializes the letters array
                                                    }
                                                }
                                            }
                                           connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
                                        
                                           Kboard = new Keyboard();
                                           connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
                                        }
                                        
                                        MainWindow::~MainWindow()
                                        {
                                           delete ui;
                                        }
                                        
                                        void MainWindow::serialReceived()
                                        {
                                            QByteArray b = serial->readAll();
                                            qDebug() << b;
                                        }
                                        
                                        void MainWindow::send(QString c)
                                        {
                                          serial->write("c");
                                          qDebug() << "hello world " << c;
                                        }
                                        
                                        #include "keyboard.h" //keyboard.cpp
                                        #include "ui_keyboard.h"
                                        #include <QtSerialPort>
                                        #include "mainwindow.h"
                                        
                                        
                                        bool caps = true, shift = true;
                                        
                                        Keyboard::Keyboard(QWidget *parent) :
                                            QDialog(parent),
                                            ui(new Ui::Keyboard)
                                        
                                        {
                                            ui->setupUi(this);
                                        }
                                        
                                        Keyboard::~Keyboard()
                                        {
                                            delete ui;
                                        }
                                        
                                        void Keyboard::on_a_clicked()
                                        {
                                           QString c;
                                           c = ui->a->text();
                                           emit KeyPressed(c);
                                           qDebug() << c;
                                        
                                           if(caps == true && shift == false){ // disables the effect of the shift key when any lettre has been pressed.
                                               shift = false;
                                               on_shift1_clicked();
                                           }
                                        }
                                        
                                        #ifndef MAINWINDOW_H //main window.h
                                        #define MAINWINDOW_H
                                        
                                        #include <QMainWindow>
                                        #include "keyboard.h"
                                        #include "numpad.h"
                                        
                                        
                                        namespace Ui {
                                        class MainWindow;
                                        }
                                        
                                        class MainWindow : public QMainWindow
                                        {
                                            Q_OBJECT
                                        
                                        public:
                                            explicit MainWindow(QWidget *parent = 0);
                                            ~MainWindow();
                                        
                                        private slots:
                                            void send(QString c);
                                        
                                        private:
                                            Ui::MainWindow *ui;
                                            Keyboard *keyboard;
                                            Numpad *numpad;
                                        };
                                        
                                        #endif // MAINWINDOW_H
                                        
                                        #ifndef KEYBOARD_H
                                        #define KEYBOARD_H
                                        
                                        #include <QDialog>
                                        
                                        namespace Ui {  //keyboard.h
                                        class Keyboard;
                                        }
                                        
                                        class Keyboard : public QDialog
                                        {
                                            Q_OBJECT
                                        
                                        public:
                                            explicit Keyboard(QWidget *parent = 0);
                                            ~Keyboard();
                                        signals:
                                            void KeyPressed(QString);
                                        
                                        private slots:
                                            void on_a_clicked();
                                            
                                        
                                        private:
                                            Ui::Keyboard *ui;
                                        };
                                        
                                        #endif // KEYBOARD_H
                                        
                                        jsulmJ J.HilkJ 3 Replies Last reply
                                        0
                                        • B bask185

                                          The one with the comma remains ;)

                                          #include "mainwindow.h" // mainwindow.cpp
                                          #include "ui_mainwindow.h"
                                          #include <QPixmap>
                                          #include <QDebug>
                                          #include <QPalette>
                                          #include <QtSerialPort>
                                          #include "keyboard.h"
                                          #include "numpad.h"
                                          QSerialPort *serial;
                                          void readCommand();
                                          
                                          char lettres[5][40][16];     // initializes all to be used variables;
                                          int index1 = 0;
                                          int index2 = 0;
                                          int colom[5];
                                          int row[5];
                                          int colorIndex[5];
                                          char c;
                                          Keyboard *Kboard;
                                          
                                          MainWindow::MainWindow(QWidget *parent) :
                                              QMainWindow(parent),
                                              ui(new Ui::MainWindow)
                                          {
                                              ui->setupUi(this);
                                              serial = new QSerialPort();
                                              serial->setPortName("COM4");
                                              serial->open(QIODevice::ReadWrite);
                                              serial->setBaudRate(QSerialPort::Baud115200);
                                              serial->setDataBits(QSerialPort::Data8);
                                              serial->setParity(QSerialPort::OddParity);
                                              serial->setStopBits(QSerialPort::OneStop);
                                              //serial.setFlowControl(QSerialPort::NoFlowControl);
                                              //serial.open(QIODevice::ReadWrite);
                                          
                                              QPixmap logo(":/resources/img/logo.jpg");
                                              int w = ui->label->width();
                                              int h = ui->label->height();
                                              ui->label->setPixmap(logo.scaled(w,h,Qt::KeepAspectRatio));
                                          
                                              for(int i=0;i<5;i++) {
                                                  for (int j=0;j<40;j++) {
                                                      for (int k=0;k<16;k++){
                                                          lettres[i][j][k] =' ';   // initializes the letters array
                                                      }
                                                  }
                                              }
                                             connect(serial, &QSerialPort::readyRead, this, &MainWindow::serialReceived);
                                          
                                             Kboard = new Keyboard();
                                             connect(Kboard, &Keyboard::KeyPressed, this, &MainWindow::send);
                                          }
                                          
                                          MainWindow::~MainWindow()
                                          {
                                             delete ui;
                                          }
                                          
                                          void MainWindow::serialReceived()
                                          {
                                              QByteArray b = serial->readAll();
                                              qDebug() << b;
                                          }
                                          
                                          void MainWindow::send(QString c)
                                          {
                                            serial->write("c");
                                            qDebug() << "hello world " << c;
                                          }
                                          
                                          #include "keyboard.h" //keyboard.cpp
                                          #include "ui_keyboard.h"
                                          #include <QtSerialPort>
                                          #include "mainwindow.h"
                                          
                                          
                                          bool caps = true, shift = true;
                                          
                                          Keyboard::Keyboard(QWidget *parent) :
                                              QDialog(parent),
                                              ui(new Ui::Keyboard)
                                          
                                          {
                                              ui->setupUi(this);
                                          }
                                          
                                          Keyboard::~Keyboard()
                                          {
                                              delete ui;
                                          }
                                          
                                          void Keyboard::on_a_clicked()
                                          {
                                             QString c;
                                             c = ui->a->text();
                                             emit KeyPressed(c);
                                             qDebug() << c;
                                          
                                             if(caps == true && shift == false){ // disables the effect of the shift key when any lettre has been pressed.
                                                 shift = false;
                                                 on_shift1_clicked();
                                             }
                                          }
                                          
                                          #ifndef MAINWINDOW_H //main window.h
                                          #define MAINWINDOW_H
                                          
                                          #include <QMainWindow>
                                          #include "keyboard.h"
                                          #include "numpad.h"
                                          
                                          
                                          namespace Ui {
                                          class MainWindow;
                                          }
                                          
                                          class MainWindow : public QMainWindow
                                          {
                                              Q_OBJECT
                                          
                                          public:
                                              explicit MainWindow(QWidget *parent = 0);
                                              ~MainWindow();
                                          
                                          private slots:
                                              void send(QString c);
                                          
                                          private:
                                              Ui::MainWindow *ui;
                                              Keyboard *keyboard;
                                              Numpad *numpad;
                                          };
                                          
                                          #endif // MAINWINDOW_H
                                          
                                          #ifndef KEYBOARD_H
                                          #define KEYBOARD_H
                                          
                                          #include <QDialog>
                                          
                                          namespace Ui {  //keyboard.h
                                          class Keyboard;
                                          }
                                          
                                          class Keyboard : public QDialog
                                          {
                                              Q_OBJECT
                                          
                                          public:
                                              explicit Keyboard(QWidget *parent = 0);
                                              ~Keyboard();
                                          signals:
                                              void KeyPressed(QString);
                                          
                                          private slots:
                                              void on_a_clicked();
                                              
                                          
                                          private:
                                              Ui::Keyboard *ui;
                                          };
                                          
                                          #endif // KEYBOARD_H
                                          
                                          jsulmJ Offline
                                          jsulmJ Offline
                                          jsulm
                                          Lifetime Qt Champion
                                          wrote on last edited by
                                          #22

                                          @bask185 One note: why do you use global variables? This is bad habit!
                                          For example Kboard should be a member variable of your MainWindow class.

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

                                          1 Reply Last reply
                                          0

                                          • Login

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