Reputation: 213
I am trying to create a very simple UI for my existing C++ (non-Qt) project: Just one window displaying numbers that are the output of executing the existing C++ project (I might add a couple of buttons and will need some behavior, but that's for later).
So I renamed my main() function - let's call it foo() - in existing project to another name and my idea is to use the new main() to create the UI, and call foo() to display output from foo onto the UI. Should be simple enough, but I'm not able to include necessary Qt headers (e.g. QApplication) for creating the UI in the main. I modified the cmake to find the Qt5Core and Qt5Widget packages and it compiles fine, but as soon as I try to include QApplication.h, it gives an error. I also tried to add includes to cmake as given here but that didn't work.
I'm new to both cmake and Qt so pardon my ignorance. Is there anything obvious I'm missing. Many posts online and on SO said to create a Qt project and call the existing code, but for the functionality I require that's not an option. I must create the UI in the existing C++ code, and not the other way round.
Upvotes: 0
Views: 810
Reputation: 155
I am not sure what is the proper way to achieve your goal, but there is another way if you want: you can build your existing code and use QProcess class to call it and read its output and display it, you also can pass arguments to it
from Qt documentation
The QProcess class is used to start external programs and to communicate with them.
Running a Process
To start a process, pass the name and command line arguments of the program you want to run as arguments to start(). Arguments are supplied as individual strings in a QStringList.
Alternatively, you can set the program to run with setProgram() and setArguments(), and then call start() or open().
For example, the following code snippet runs the analog clock example in the Fusion style on X11 platforms by passing strings containing "-style" and "fusion" as two items in the list of arguments:
QObject *parent; ... QString program = "./path/to/Qt/examples/widgets/analogclock"; QStringList arguments; arguments << "-style" << "fusion"; QProcess *myProcess = new QProcess(parent); myProcess->start(program, arguments);
Upvotes: 0