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 can I move my normal c++ main() function code to qt main function?

How can I move my normal c++ main() function code to qt main function?

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 3 Posters 6.8k 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.
  • A Offline
    A Offline
    Ashish Tupate
    wrote on last edited by Ashish Tupate
    #1

    I have created small normal c++ code to test in raspberry pi which is working perfectly fine on the terminal.Now I want to write it down in qt, the problem here I am facing is I can able to move my functions inside the main function into another function. This is the code:
    rotary_encoder.cpp

    #include <iostream>
    
    #include <pigpio.h>
    
    #include "rotary_encoder.hpp"
    
    void re_decoder::_pulse(int gpio, int level, uint32_t tick)
    {
       if (gpio == mygpioA) levA = level; else levB = level;
    
       if (gpio != lastGpio) /* debounce */
       {
          lastGpio = gpio;
    
          if ((gpio == mygpioA) && (level == 1))
          {
             if (levB) (mycallback)(1);
          }
          else if ((gpio == mygpioB) && (level == 1))
          {
             if (levA) (mycallback)(-1);
          }
       }
    }
    
    void re_decoder::_pulseEx(int gpio, int level, uint32_t tick, void *user)
    {
       re_decoder *mySelf = (re_decoder *) user;
       mySelf->_pulse(gpio, level, tick); /* Call the instance callback. */
    }
    
    re_decoder::re_decoder(int gpioA, int gpioB, re_decoderCB_t callback)
    {
       mygpioA = gpioA;
       mygpioB = gpioB;
    
       mycallback = callback;
    
       levA=0;
       levB=0;
    
       lastGpio = -1;
    
       gpioSetMode(gpioA, PI_INPUT);
       gpioSetMode(gpioB, PI_INPUT);
    
       gpioSetPullUpDown(gpioA, PI_PUD_UP);
       gpioSetPullUpDown(gpioB, PI_PUD_UP);
    
       gpioSetAlertFuncEx(gpioA, _pulseEx, this);
       gpioSetAlertFuncEx(gpioB, _pulseEx, this);
    }
    
    void re_decoder::re_cancel(void)
    {
       gpioSetAlertFuncEx(mygpioA, 0, this);
       gpioSetAlertFuncEx(mygpioB, 0, this);
    }
    
    
    

    rotary_encoder.hpp

    #ifndef ROTARY_ENCODER_HPP
    #define ROTARY_ENCODER_HPP
    
    #include <stdint.h>
    
    typedef void (*re_decoderCB_t)(int);
    class re_decoder
    {
       int mygpioA, mygpioB, levA, levB, lastGpio;
    
       re_decoderCB_t mycallback;
       void _pulse(int gpio, int level, uint32_t tick);
       static void _pulseEx(int gpio, int level, uint32_t tick, void *user);
    
       public:
       re_decoder(int gpioA, int gpioB, re_decoderCB_t callback);
       void re_cancel(void);
    };
    
    #endif
    
    

    test_encoder.cpp

    #include <iostream>
    #include <pigpio.h>
    #include "rotary_encoder.hpp"
    
    void callback(int way)
    {
       static int pos = 0;
       pos += way;
       std::cout << "pos=" << pos << std::endl;
    }
    
    int main(int argc, char *argv[])
    {
       if (gpioInitialise() < 0) return 1;
       e_decoder dec(7, 8, callback);
       sleep(3000);
       dec.re_cancel();
       gpioTerminate();
    }
    
    
    

    I have created qt widget project and added rotary_encoder.cpp hpp file but I have a problem with a test_encoder.cpp file. Can anyone suggest something?

    jsulmJ 1 Reply Last reply
    0
    • A Ashish Tupate

      I have created small normal c++ code to test in raspberry pi which is working perfectly fine on the terminal.Now I want to write it down in qt, the problem here I am facing is I can able to move my functions inside the main function into another function. This is the code:
      rotary_encoder.cpp

      #include <iostream>
      
      #include <pigpio.h>
      
      #include "rotary_encoder.hpp"
      
      void re_decoder::_pulse(int gpio, int level, uint32_t tick)
      {
         if (gpio == mygpioA) levA = level; else levB = level;
      
         if (gpio != lastGpio) /* debounce */
         {
            lastGpio = gpio;
      
            if ((gpio == mygpioA) && (level == 1))
            {
               if (levB) (mycallback)(1);
            }
            else if ((gpio == mygpioB) && (level == 1))
            {
               if (levA) (mycallback)(-1);
            }
         }
      }
      
      void re_decoder::_pulseEx(int gpio, int level, uint32_t tick, void *user)
      {
         re_decoder *mySelf = (re_decoder *) user;
         mySelf->_pulse(gpio, level, tick); /* Call the instance callback. */
      }
      
      re_decoder::re_decoder(int gpioA, int gpioB, re_decoderCB_t callback)
      {
         mygpioA = gpioA;
         mygpioB = gpioB;
      
         mycallback = callback;
      
         levA=0;
         levB=0;
      
         lastGpio = -1;
      
         gpioSetMode(gpioA, PI_INPUT);
         gpioSetMode(gpioB, PI_INPUT);
      
         gpioSetPullUpDown(gpioA, PI_PUD_UP);
         gpioSetPullUpDown(gpioB, PI_PUD_UP);
      
         gpioSetAlertFuncEx(gpioA, _pulseEx, this);
         gpioSetAlertFuncEx(gpioB, _pulseEx, this);
      }
      
      void re_decoder::re_cancel(void)
      {
         gpioSetAlertFuncEx(mygpioA, 0, this);
         gpioSetAlertFuncEx(mygpioB, 0, this);
      }
      
      
      

      rotary_encoder.hpp

      #ifndef ROTARY_ENCODER_HPP
      #define ROTARY_ENCODER_HPP
      
      #include <stdint.h>
      
      typedef void (*re_decoderCB_t)(int);
      class re_decoder
      {
         int mygpioA, mygpioB, levA, levB, lastGpio;
      
         re_decoderCB_t mycallback;
         void _pulse(int gpio, int level, uint32_t tick);
         static void _pulseEx(int gpio, int level, uint32_t tick, void *user);
      
         public:
         re_decoder(int gpioA, int gpioB, re_decoderCB_t callback);
         void re_cancel(void);
      };
      
      #endif
      
      

      test_encoder.cpp

      #include <iostream>
      #include <pigpio.h>
      #include "rotary_encoder.hpp"
      
      void callback(int way)
      {
         static int pos = 0;
         pos += way;
         std::cout << "pos=" << pos << std::endl;
      }
      
      int main(int argc, char *argv[])
      {
         if (gpioInitialise() < 0) return 1;
         e_decoder dec(7, 8, callback);
         sleep(3000);
         dec.re_cancel();
         gpioTerminate();
      }
      
      
      

      I have created qt widget project and added rotary_encoder.cpp hpp file but I have a problem with a test_encoder.cpp file. Can anyone suggest something?

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

      @Ashish-Tupate There is no such thing like "qt main function". Qt is a C++ framework, so you simply write your main function like in any other C++ project.
      What exact problem do you have?

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

      A 1 Reply Last reply
      1
      • jsulmJ jsulm

        @Ashish-Tupate There is no such thing like "qt main function". Qt is a C++ framework, so you simply write your main function like in any other C++ project.
        What exact problem do you have?

        A Offline
        A Offline
        Ashish Tupate
        wrote on last edited by
        #3

        @jsulm I want to write functions from file test_encoder from the main function to somewhere in other file but not inside the main function. How can I write these function? Because in qt in the function we cant write these functions.

        aha_1980A jsulmJ 2 Replies Last reply
        0
        • A Ashish Tupate

          @jsulm I want to write functions from file test_encoder from the main function to somewhere in other file but not inside the main function. How can I write these function? Because in qt in the function we cant write these functions.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by aha_1980
          #4

          Hi @Ashish-Tupate,

          given that your Qt main.cpp looks like the following:

          #include "mainwindow.h"
          
          #include <QApplication>
          
          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              MainWindow w;
              w.show();
          
              return a.exec();
          }
          

          a starting point could look like this:

          #include "mainwindow.h"
          #include "rotary_encoder.hpp"
          
          #include <QApplication>
          
          #include <iostream>
          #include <pigpio.h>
          
          void callback(int way)
          {
             static int pos = 0;
             pos += way;
             std::cout << "pos=" << pos << std::endl;
          }
          
          int main(int argc, char *argv[])
          {
              if (gpioInitialise() < 0) 
                  return 1;
              e_decoder dec(7, 8, callback);
          
              QApplication a(argc, argv);
              MainWindow w;
              w.show();
              int result = a.exec();
              
              dec.re_cancel();
              gpioTerminate();
              
              return result;
          }
          

          Qt has to stay free or it will die.

          A 1 Reply Last reply
          1
          • A Ashish Tupate

            @jsulm I want to write functions from file test_encoder from the main function to somewhere in other file but not inside the main function. How can I write these function? Because in qt in the function we cant write these functions.

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

            @Ashish-Tupate Sorry, I don't really get what you mean. Can you describe it better, maybe write some code? It is completely unrelated to Qt - Qt is not a programming language. You write your functions as always in C++ and call them in your main function.
            Lets say you have your main function in a.cpp. In b.h/b.cpp you define a function void do_something(), then in a.cpp you can call it like:

            //b.h
            void do_something();
            
            // b.cpp
            #include "b.h"
            void do_something()
            {
            }
            
            // a.cpp
            #include "b.h" // to be able to call the function
            int main()
            {
                do_something();
                return 0;
            }
            

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

            A 1 Reply Last reply
            0
            • aha_1980A aha_1980

              Hi @Ashish-Tupate,

              given that your Qt main.cpp looks like the following:

              #include "mainwindow.h"
              
              #include <QApplication>
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
                  MainWindow w;
                  w.show();
              
                  return a.exec();
              }
              

              a starting point could look like this:

              #include "mainwindow.h"
              #include "rotary_encoder.hpp"
              
              #include <QApplication>
              
              #include <iostream>
              #include <pigpio.h>
              
              void callback(int way)
              {
                 static int pos = 0;
                 pos += way;
                 std::cout << "pos=" << pos << std::endl;
              }
              
              int main(int argc, char *argv[])
              {
                  if (gpioInitialise() < 0) 
                      return 1;
                  e_decoder dec(7, 8, callback);
              
                  QApplication a(argc, argv);
                  MainWindow w;
                  w.show();
                  int result = a.exec();
                  
                  dec.re_cancel();
                  gpioTerminate();
                  
                  return result;
              }
              
              A Offline
              A Offline
              Ashish Tupate
              wrote on last edited by tekojo
              #6

              @aha_1980 said in How can I move my normal c++ main() function code to qt main function?:

              int main(int argc, char *argv[])
              {
              if (gpioInitialise() < 0)
              return 1;
              e_decoder dec(7, 8, callback);

              QApplication a(argc, argv);
              MainWindow w;
              w.show();
              int result = a.exec();
              
              dec.re_cancel();
              gpioTerminate();
              
              return result;
              

              }

              I dont want to write

              if (gpioInitialise() < 0) 
                      return 1;
                  e_decoder dec(7, 8, callback);
              
                  QApplication a(argc, argv);
                  MainWindow w;
                  w.show();
                  int result = a.exec();
                  
                  dec.re_cancel();
                  gpioTerminate();
              

              code inside main function
              So where should i write this code?

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @Ashish-Tupate Sorry, I don't really get what you mean. Can you describe it better, maybe write some code? It is completely unrelated to Qt - Qt is not a programming language. You write your functions as always in C++ and call them in your main function.
                Lets say you have your main function in a.cpp. In b.h/b.cpp you define a function void do_something(), then in a.cpp you can call it like:

                //b.h
                void do_something();
                
                // b.cpp
                #include "b.h"
                void do_something()
                {
                }
                
                // a.cpp
                #include "b.h" // to be able to call the function
                int main()
                {
                    do_something();
                    return 0;
                }
                
                A Offline
                A Offline
                Ashish Tupate
                wrote on last edited by
                #7

                @jsulm Thank you but If I want to call do_something();
                not from main() because we cant call it in qt like this. So, then from where should I call?

                jsulmJ 1 Reply Last reply
                0
                • A Ashish Tupate

                  @jsulm Thank you but If I want to call do_something();
                  not from main() because we cant call it in qt like this. So, then from where should I call?

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

                  @Ashish-Tupate I don't understand the problem. Call it where you need to call it. You as developer have to know where to call it.
                  Which code exactly you don't want to call in main?
                  QApplication instance is usually created in main, same for main window. But you can create both somewhere else.

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

                  A 1 Reply Last reply
                  1
                  • jsulmJ jsulm

                    @Ashish-Tupate I don't understand the problem. Call it where you need to call it. You as developer have to know where to call it.
                    Which code exactly you don't want to call in main?
                    QApplication instance is usually created in main, same for main window. But you can create both somewhere else.

                    A Offline
                    A Offline
                    Ashish Tupate
                    wrote on last edited by
                    #9

                    @jsulm I cant write the main function in qt like this:

                    void callback(int way)
                    {
                       static int pos = 0;
                       pos += way;
                       std::cout << "pos=" << pos << std::endl;
                    } 
                    
                    int main(int argc, char *argv[])
                    {
                        if (gpioInitialise() < 0) return 1;
                        re_decoder dec(7, 8, callback);
                    
                        QApplication a(argc, argv);
                        Dialog w;
                        w.show();
                    
                        sleep(3000);
                        dec.re_cancel();
                        gpioTerminate();
                    
                        return a.exec();
                    }
                    
                    
                    
                    

                    So where can I write this code?

                    jsulmJ 1 Reply Last reply
                    0
                    • A Ashish Tupate

                      @jsulm I cant write the main function in qt like this:

                      void callback(int way)
                      {
                         static int pos = 0;
                         pos += way;
                         std::cout << "pos=" << pos << std::endl;
                      } 
                      
                      int main(int argc, char *argv[])
                      {
                          if (gpioInitialise() < 0) return 1;
                          re_decoder dec(7, 8, callback);
                      
                          QApplication a(argc, argv);
                          Dialog w;
                          w.show();
                      
                          sleep(3000);
                          dec.re_cancel();
                          gpioTerminate();
                      
                          return a.exec();
                      }
                      
                      
                      
                      

                      So where can I write this code?

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

                      @Ashish-Tupate I'm guessing now what the problem is: you want to execute this code when the app terminates, right?

                      dec.re_cancel();
                      gpioTerminate();
                      

                      Then do it like this:

                      int main(int argc, char *argv[])
                      {
                          if (gpioInitialise() < 0) return 1;
                          re_decoder dec(7, 8, callback);
                      
                          QApplication a(argc, argv);
                          Dialog w;
                          w.show();
                      
                          int result = a.exec(); // exec() blocks until you close the app
                      
                          dec.re_cancel();
                          gpioTerminate();
                      
                          return result;
                      }

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

                      A 1 Reply Last reply
                      4
                      • jsulmJ jsulm

                        @Ashish-Tupate I'm guessing now what the problem is: you want to execute this code when the app terminates, right?

                        dec.re_cancel();
                        gpioTerminate();
                        

                        Then do it like this:

                        int main(int argc, char *argv[])
                        {
                            if (gpioInitialise() < 0) return 1;
                            re_decoder dec(7, 8, callback);
                        
                            QApplication a(argc, argv);
                            Dialog w;
                            w.show();
                        
                            int result = a.exec(); // exec() blocks until you close the app
                        
                            dec.re_cancel();
                            gpioTerminate();
                        
                            return result;
                        }
                        A Offline
                        A Offline
                        Ashish Tupate
                        wrote on last edited by
                        #11

                        @jsulm Thank you.

                        aha_1980A 1 Reply Last reply
                        0
                        • A Ashish Tupate

                          @jsulm Thank you.

                          aha_1980A Offline
                          aha_1980A Offline
                          aha_1980
                          Lifetime Qt Champion
                          wrote on last edited by aha_1980
                          #12

                          @Ashish-Tupate said in How can I move my normal c++ main() function code to qt main function?:

                          @jsulm Thank you.

                          I don't get it. That is the same code I posted this morning (one comment more) and you said it is not what you want...

                          Qt has to stay free or it will die.

                          A 1 Reply Last reply
                          2
                          • aha_1980A aha_1980

                            @Ashish-Tupate said in How can I move my normal c++ main() function code to qt main function?:

                            @jsulm Thank you.

                            I don't get it. That is the same code I posted this morning (one comment more) and you said it is not what you want...

                            A Offline
                            A Offline
                            Ashish Tupate
                            wrote on last edited by Ashish Tupate
                            #13

                            @aha_1980 I still didn't get the answer from this forum and the answer by the jsulm is not the expected answer for me. What I want is to write main() as it is like this

                            int main(int argc, char *argv[])
                            {
                               QApplication a(argc, argv);
                               MainWindow w;
                               w.show();
                            
                               return a.exec();
                            }
                            

                            and the code inside this

                            if (gpioInitialise() < 0) 
                                    return 1;
                            e_decoder dec(7, 8, callback);
                            sleep(50);
                            dec.re_cancel();
                            gpioTerminate();
                            

                            and this code

                            void callback(int way)
                            {
                               static int pos = 0;
                               pos += way;
                               std::cout << "pos=" << pos << std::endl;
                            }
                            

                            I want to write somewhere in other file. Like I can create a new file (ex. test.cpp) and write it down there. So the code should work. Is it possible?I have tried to write the code.
                            encodercount.h

                            #ifndef ENCODERCOUNT_H
                            #define ENCODERCOUNT_H
                            
                            namespace Encoder {
                            class EncoderCount;
                            }
                            
                            class EncoderCount
                            {
                            public:
                                explicit EncoderCount();
                                ~EncoderCount();
                                int pigpiotest();
                            
                            private:
                                static void callback(int way);
                            };
                            

                            encodercount.cpp

                            #include <pigpio.h>
                            #include "rotary_encoder.hpp"
                            #include <iostream>
                            #include <stdio.h>
                            #include <unistd.h>
                            
                            static int pos = 0;
                            void EncoderCount::callback(int way)
                            {
                                   pos += way;
                                   qDebug() << "pos=" << pos ;
                            }
                             int EncoderCount::pigpiotest()
                             {
                                 if (gpioInitialise() < 0) return 1;
                            
                                 re_decoder dec(8, 11, callback);
                            
                                 sleep(50);
                            
                                 dec.re_cancel();
                            
                                 gpioTerminate();
                            
                             }
                            

                            Still no output.

                            aha_1980A jsulmJ 2 Replies Last reply
                            0
                            • A Ashish Tupate

                              @aha_1980 I still didn't get the answer from this forum and the answer by the jsulm is not the expected answer for me. What I want is to write main() as it is like this

                              int main(int argc, char *argv[])
                              {
                                 QApplication a(argc, argv);
                                 MainWindow w;
                                 w.show();
                              
                                 return a.exec();
                              }
                              

                              and the code inside this

                              if (gpioInitialise() < 0) 
                                      return 1;
                              e_decoder dec(7, 8, callback);
                              sleep(50);
                              dec.re_cancel();
                              gpioTerminate();
                              

                              and this code

                              void callback(int way)
                              {
                                 static int pos = 0;
                                 pos += way;
                                 std::cout << "pos=" << pos << std::endl;
                              }
                              

                              I want to write somewhere in other file. Like I can create a new file (ex. test.cpp) and write it down there. So the code should work. Is it possible?I have tried to write the code.
                              encodercount.h

                              #ifndef ENCODERCOUNT_H
                              #define ENCODERCOUNT_H
                              
                              namespace Encoder {
                              class EncoderCount;
                              }
                              
                              class EncoderCount
                              {
                              public:
                                  explicit EncoderCount();
                                  ~EncoderCount();
                                  int pigpiotest();
                              
                              private:
                                  static void callback(int way);
                              };
                              

                              encodercount.cpp

                              #include <pigpio.h>
                              #include "rotary_encoder.hpp"
                              #include <iostream>
                              #include <stdio.h>
                              #include <unistd.h>
                              
                              static int pos = 0;
                              void EncoderCount::callback(int way)
                              {
                                     pos += way;
                                     qDebug() << "pos=" << pos ;
                              }
                               int EncoderCount::pigpiotest()
                               {
                                   if (gpioInitialise() < 0) return 1;
                              
                                   re_decoder dec(8, 11, callback);
                              
                                   sleep(50);
                              
                                   dec.re_cancel();
                              
                                   gpioTerminate();
                              
                               }
                              

                              Still no output.

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @Ashish-Tupate said in How can I move my normal c++ main() function code to qt main function?:

                              So the code should work. Is it possible?

                              Yes, of course this is possible - it's a very common task for C++ programmers.

                              But did you actually try what I and @jsulm suggested you? I wrote "as a starting point". This should work with minimum change to your existing program.

                              If you want the code to reside in a separate file, you will have to refactor it. The sleep can not be used in Qt GUI programs! You can create the functions in a new file decoder.cpp:

                              void callback()
                              {
                                  //...
                              }
                              
                              void initDecoder()
                              {
                                  if (gpioInitialise() < 0) 
                                      return 1;
                                   e_decoder dec(7, 8, callback);
                              }
                              
                              void stopDecoder()
                              {
                                  dec.re_cancel();
                                  gpioTerminate();
                              }
                              

                              and then call them, for example from main::

                              #include "decoder.h"
                              
                              int main(int argc, char *argv[])
                              {
                                  initEncoder();
                              
                                  QApplication a(argc, argv);
                                  Dialog w;
                                  w.show();
                              
                                  int result = a.exec(); // exec() blocks until you close the app
                              
                                  stopEncoder();
                              
                                  return result;
                              }
                              

                              But better try the examples we gave you yesterday first.

                              Qt has to stay free or it will die.

                              1 Reply Last reply
                              2
                              • A Ashish Tupate

                                @aha_1980 I still didn't get the answer from this forum and the answer by the jsulm is not the expected answer for me. What I want is to write main() as it is like this

                                int main(int argc, char *argv[])
                                {
                                   QApplication a(argc, argv);
                                   MainWindow w;
                                   w.show();
                                
                                   return a.exec();
                                }
                                

                                and the code inside this

                                if (gpioInitialise() < 0) 
                                        return 1;
                                e_decoder dec(7, 8, callback);
                                sleep(50);
                                dec.re_cancel();
                                gpioTerminate();
                                

                                and this code

                                void callback(int way)
                                {
                                   static int pos = 0;
                                   pos += way;
                                   std::cout << "pos=" << pos << std::endl;
                                }
                                

                                I want to write somewhere in other file. Like I can create a new file (ex. test.cpp) and write it down there. So the code should work. Is it possible?I have tried to write the code.
                                encodercount.h

                                #ifndef ENCODERCOUNT_H
                                #define ENCODERCOUNT_H
                                
                                namespace Encoder {
                                class EncoderCount;
                                }
                                
                                class EncoderCount
                                {
                                public:
                                    explicit EncoderCount();
                                    ~EncoderCount();
                                    int pigpiotest();
                                
                                private:
                                    static void callback(int way);
                                };
                                

                                encodercount.cpp

                                #include <pigpio.h>
                                #include "rotary_encoder.hpp"
                                #include <iostream>
                                #include <stdio.h>
                                #include <unistd.h>
                                
                                static int pos = 0;
                                void EncoderCount::callback(int way)
                                {
                                       pos += way;
                                       qDebug() << "pos=" << pos ;
                                }
                                 int EncoderCount::pigpiotest()
                                 {
                                     if (gpioInitialise() < 0) return 1;
                                
                                     re_decoder dec(8, 11, callback);
                                
                                     sleep(50);
                                
                                     dec.re_cancel();
                                
                                     gpioTerminate();
                                
                                 }
                                

                                Still no output.

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

                                @Ashish-Tupate said in How can I move my normal c++ main() function code to qt main function?:

                                Is it possible?

                                Sure it is, why not?

                                The code you put in test.cpp - is it called somewhere? I mean: do you use class EncoderCount somewhere? You're using a member function (method) as a callback - this will not work as you need a function.

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

                                A 1 Reply Last reply
                                1
                                • jsulmJ jsulm

                                  @Ashish-Tupate said in How can I move my normal c++ main() function code to qt main function?:

                                  Is it possible?

                                  Sure it is, why not?

                                  The code you put in test.cpp - is it called somewhere? I mean: do you use class EncoderCount somewhere? You're using a member function (method) as a callback - this will not work as you need a function.

                                  A Offline
                                  A Offline
                                  Ashish Tupate
                                  wrote on last edited by
                                  #16

                                  @jsulm test.cpp is encodercount.cpp and yes it is not working.

                                  jsulmJ 1 Reply Last reply
                                  0
                                  • A Ashish Tupate

                                    @jsulm test.cpp is encodercount.cpp and yes it is not working.

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

                                    @Ashish-Tupate What is not working? Can you please describe the problem clearly? Post the error message?

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

                                    A 1 Reply Last reply
                                    1
                                    • jsulmJ jsulm

                                      @Ashish-Tupate What is not working? Can you please describe the problem clearly? Post the error message?

                                      A Offline
                                      A Offline
                                      Ashish Tupate
                                      wrote on last edited by Ashish Tupate
                                      #18

                                      @jsulm my main.cpp file look like this

                                      #include "encodercount.h"
                                      
                                      using namespace std;
                                      
                                      int main(int argc, char *argv[])
                                      {
                                          initEncoder();
                                      
                                          QApplication app(argc, argv);
                                          Dialog w;
                                          w.show();
                                      
                                          int result = a.exec(); // exec() blocks until you close the app
                                          stopEncoder();
                                      
                                          return app.exec();
                                      }
                                      

                                      and encodercount.h

                                      #ifndef ENCODERCOUNT_H
                                      #define ENCODERCOUNT_H
                                      
                                      #include <rotary_encoder.hpp>
                                      
                                      
                                      namespace Encoder {
                                      class EncoderCount;
                                      }
                                      
                                      class EncoderCount
                                      {
                                      public:
                                          explicit EncoderCount();
                                          ~EncoderCount();
                                      
                                          void initDecoder();
                                          void stopDecoder();
                                      
                                      };
                                      
                                      
                                      #endif // ENCODERCOUNT_H
                                      
                                      

                                      encodercount.cpp

                                      void callback(int way)
                                      {
                                          cout << " callback"<< endl;
                                      
                                          static int pos = 0;
                                      
                                             pos += way;
                                             qDebug() << "pos=" << pos ;
                                      }
                                      
                                      void initDecoder()
                                       {
                                           cout << "pigpio test"<< endl;
                                      
                                           if (gpioInitialise() < 0) return 1;
                                      
                                           re_decoder dec(8, 11, callback);
                                      
                                       }
                                      
                                       void stopDecoder()
                                       {
                                           dec.re_cancel();
                                           gpioTerminate();
                                       }
                                      

                                      and I am getting error :
                                      'initEncoder' was not declared in this scope
                                      initEncoder();
                                      ^
                                      'a' was not declared in this scope
                                      int result = a.exec(); // exec() blocks until you close the app
                                      ^
                                      'stopEncoder' was not declared in this scope
                                      stopEncoder();
                                      ^

                                      jsulmJ 1 Reply Last reply
                                      0
                                      • A Ashish Tupate

                                        @jsulm my main.cpp file look like this

                                        #include "encodercount.h"
                                        
                                        using namespace std;
                                        
                                        int main(int argc, char *argv[])
                                        {
                                            initEncoder();
                                        
                                            QApplication app(argc, argv);
                                            Dialog w;
                                            w.show();
                                        
                                            int result = a.exec(); // exec() blocks until you close the app
                                            stopEncoder();
                                        
                                            return app.exec();
                                        }
                                        

                                        and encodercount.h

                                        #ifndef ENCODERCOUNT_H
                                        #define ENCODERCOUNT_H
                                        
                                        #include <rotary_encoder.hpp>
                                        
                                        
                                        namespace Encoder {
                                        class EncoderCount;
                                        }
                                        
                                        class EncoderCount
                                        {
                                        public:
                                            explicit EncoderCount();
                                            ~EncoderCount();
                                        
                                            void initDecoder();
                                            void stopDecoder();
                                        
                                        };
                                        
                                        
                                        #endif // ENCODERCOUNT_H
                                        
                                        

                                        encodercount.cpp

                                        void callback(int way)
                                        {
                                            cout << " callback"<< endl;
                                        
                                            static int pos = 0;
                                        
                                               pos += way;
                                               qDebug() << "pos=" << pos ;
                                        }
                                        
                                        void initDecoder()
                                         {
                                             cout << "pigpio test"<< endl;
                                        
                                             if (gpioInitialise() < 0) return 1;
                                        
                                             re_decoder dec(8, 11, callback);
                                        
                                         }
                                        
                                         void stopDecoder()
                                         {
                                             dec.re_cancel();
                                             gpioTerminate();
                                         }
                                        

                                        and I am getting error :
                                        'initEncoder' was not declared in this scope
                                        initEncoder();
                                        ^
                                        'a' was not declared in this scope
                                        int result = a.exec(); // exec() blocks until you close the app
                                        ^
                                        'stopEncoder' was not declared in this scope
                                        stopEncoder();
                                        ^

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

                                        @Ashish-Tupate Add initEncoder(); and stopEncoder(); to encodercount.h.

                                        Change

                                        int result = a.exec();
                                        

                                        to

                                        int result = app.exec();
                                        

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

                                        A 1 Reply Last reply
                                        0
                                        • jsulmJ jsulm

                                          @Ashish-Tupate Add initEncoder(); and stopEncoder(); to encodercount.h.

                                          Change

                                          int result = a.exec();
                                          

                                          to

                                          int result = app.exec();
                                          
                                          A Offline
                                          A Offline
                                          Ashish Tupate
                                          wrote on last edited by
                                          #20

                                          @jsulm I dont know why but this error again even if these functions are inside encodercount.h:

                                          'initEncoder' was not declared in this scope
                                          initEncoder();
                                          ^
                                          'stopEncoder' was not declared in this scope
                                          stopEncoder();
                                          ^

                                          #ifndef ENCODERCOUNT_H
                                          #define ENCODERCOUNT_H
                                          #include <rotary_encoder.hpp>
                                          
                                          
                                          namespace Encoder {
                                          class EncoderCount;
                                          }
                                          
                                          class EncoderCount
                                          {
                                          public:
                                              explicit EncoderCount();
                                              ~EncoderCount();
                                          
                                              void initEncoder();
                                              void stopEncoder();
                                          
                                          };
                                          
                                          
                                          #endif // ENCODERCOUNT_H
                                          
                                          
                                          jsulmJ 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