Important: Please read the Qt Code of Conduct -

Normal c++ to qt c++

  • I have written a program in C++. I have tried that program in plain qt c++. That is working fine. Now I want to change my existing code. I am new for qt c++ I don't know how to add. I hope here I can get a solution.

    Existing Code: (Using wiringPi)

    Encoder Count.h

    #ifndef ENCODERCOUNT_H
    #define ENCODERCOUNT_H
    namespace Encoder {
    class EncoderCount;
    class EncoderCount
        explicit EncoderCount();
        static int cycleleftinterrupt;
        int Left_Encoder_Read();
        bool Encoder_Pulse();


    #define ENCODER_LEFT 14
    int Flag_LeftInterrupt=0;
    bool ResetFalseLeft;
    using namespace std;
        digitalWrite(ENCODER_LEFT, HIGH);
        ResetFalseLeft = false;
     int EncoderCount::Left_Encoder_Read()
        return encoderValue/4;
     void leftencoderinterrupt(void)
    My stuff here............................
    encoderValue ++;
         if(sum .............)
     encoderValue --;
         lastEncoded = encoded; 
    bool EncoderCount::Encoder_Pulse()
            return false;
        return true;

    I have tried the new code that is working fine that I want to take from project main and I will include here. I don't know how to add/include. I am new to c++, so please help to get a solution. Here I have attached new code

    #include <pigpio.h>
    #include "rotary_encoder.hpp"
    #include <iostream>
    #include <stdio.h>
    #include <unistd.h>
    #include <QApplication>
    void callback(int way)
       static int pos = 0;
       pos += way;
       std::cout << "pos=" << pos << std::endl;
    int main(int argc, char *argv[])
        QApplication a(argc, argv);
        Dialog w;;
        if (gpioInitialise() < 0) return 1;
        re_decoder dec(8, 11, callback);
        return a.exec();

  • There is not a conversion from "normal" C++ and "qt" C++.
    C++ is the same! You can compile pure C++ in Qt Creator without problems.
    Qt is an "extension" of C++.

  • @Charlie_Hdz can you tell me how to add new code to existing?

  • I'm afraid it is an imprecise request.

    Which compiler are you using?
    Which is your "existing" code?
    Which is your "new" code?

    To begin with...

  • @Charlie_Hdz encoder.h and encoder.cpp existing main.cpp new one using qt

  • Lifetime Qt Champion


    You should explain what exactly you want to do with your application. That will make things simpler to get help.

  • @Geeva As other people have said Qt is not a language it is a framework or library (which ever you prefer to call it).
    So, you have a header called Encoder Count.h, a cpp file called Encodercount.ccp (interesting inconsistency in the naming of the files). You have a main file which does not include the Encoder Count.h file? you do not include Encoder Count.h directly in the main file so we must assume you are including it in some other file which gets included?
    You are creating a dialog in your main function, so, are you having a problem with including a file in relation to the dialog implementation or what?
    You should probably show people here all of the code including the dialog to better explain the relationships and the exact issue you are having.

  • As other people have said, Qt is an "extension" of C++. It is very simple, You can take from main.cpp to encoder.cpp. It looks like,


    void leftencoder(int way)
       static int pos = 0;
       pos += way;
       cout << "pos=" << pos << endl;
       EncoderCount::cycleleftinterrupt = pos;
     void EncoderCount::Encoder_Pulse_pigpio()
         cout << "pigpio ...test"<< endl;
         if (gpioInitialise() < 0) return ;
         re_decoder dec(11, 8, leftencoder);
         sleep(50); // You can change sleep time (Based on your application)

    Similar you can try for 2 or 3 encoders.

  • @Jerwinprabu Thank You. It is working fine now.