How can I move my normal c++ main() function code to qt main function?
-
@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.
-
@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.
-
@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.
-
@Ashish-Tupate What is not working? Can you please describe the problem clearly? Post the error message?
-
@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();
^ -
@Ashish-Tupate Add initEncoder(); and stopEncoder(); to encodercount.h.
Change
int result = a.exec();
to
int result = app.exec();
-
@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
-
@Ashish-Tupate Please think about what you are doing: you put both functions inside EncoderCount class! And then you try to call them as if they would be simple functions.
You should learn C++.
It should be like this:#ifndef ENCODERCOUNT_H #define ENCODERCOUNT_H #include <rotary_encoder.hpp> void initEncoder(); void stopEncoder();
-
@Ashish-Tupate "but you have to help me out" - no I don't have to.
You should really learn C++ and read the error messages carefully. What you are asking is completely unrelated to Qt.
Changeif (gpioInitialise() < 0) return 1;
to
if (gpioInitialise() < 0) return;