Communication between Qt GUI and separate C++ program
-
Hi,
I am new to Qt. I have code running calculations made by C++ core language and STL, on Visual Studio. Recently I installed Qt in order to create a GUI (with Qt Designer) to let enter parameters then send them to my C++ code (in the form of an std::array, for ex), after running calculations, send the results back to GUI and display it.
I can create a GUI, but I don't know how to run the communication of data between GUI made by Qt and program written in C++ classic.
Could anyone please help me? Thanks in advance!
-
Qt is still C++, so you can integrate your STL code with Qt code by merging the codebase, for example, possibly also utilizing QThread. Or if your STL code builds to a library, you can include it in Qt app and use it's API. If it compiles into an console app, you can also communicate with it from Qt by using QProcess.
-
Thanks for the reply. Yes my code is basically a console application. It would take a long time if I convert my code into Qt because it's very long (and I suppose if I merge them in Qt I have to rewrite my code according to Qt classes, Qt functions,...). BTW, it's running on multi-threads already on console to reach high calculation performance.
So I think the easiest way to do is just to keep original code on console and make it communicate with Qt GUI?
I tried to seek for tutorial of QProgress (if this is the best choice), but there is not a lot online...
-
[quote author="dekeenfrance" date="1368268640"] (and I suppose if I merge them in Qt I have to rewrite my code according to Qt classes, Qt functions,...)[/quote]
Not true. It's all still C++, you would just need to add an interface between the GUI and your old code.
But anyway, it seems that using "QProcess":http://qt-project.org/doc/qt-4.8/qprocess.html is better in your use case. Take a look at the documentation I've linked, it should give you some basic idea on how to communicate with your console app.
On the GUI side, you will probably need some QtWidgets interface, like a QProgressbar, some QLineEdits to specify parameters, etc. You can use QMainWindow as your base widget, then lay out the UI in there. If you use Qt Creator for Qt development, it has some built-in templates that will help you to start your adventure :)
Other than that, you'll need to ask more specific questions, as the subject is rather broad.
-
Thanks. if we can easily merge C++ console code and Qt GUI by implement an interface, is there any simple example about such interface? And does that have an influence on the speed of execution? (I suppose if it's merged, it will be compiled by Qt compiler, I don't know whether that have impact on the speed of my calculation...)
I am reading the QProcess document now. Thanks again!
-
Another question about merge: should that be done in Visual Studio or Qt Creator? VS is a good IDE that I always work with. I am total new to Qt Creator.
-
[quote author="dekeenfrance" date="1368269719"]Thanks. if we can easily merge C++ console code and Qt GUI by implement an interface, is there any simple example about such interface? And does that have an influence on the speed of execution? (I suppose if it's merged, it will be compiled by Qt compiler, I don't know whether that have impact on the speed of my calculation...)[/quote]
Depends a lot on how your app is made. If your "worker" code has an API, you can simply include relevant headers in your GUI and call those methods. Integrating the build system can be harder, but is no necessarily needed (you can include precompiled libs in Qt apps). I don't know about any example on that, but then again, I haven't searched for it - I've already got a few solutions that work this way so I don't need tutorials all that much. I would recommend starting off with QProcess, though, it should be easier for you in the beginning. Once you get more comfy in Qt, you can start experimenting :D
Qt can be compiled by any C++98 compatible compiler, so it should not be an issue (I do recommend clang, it's ultra-fast: projects compile faster, and resulting machine code is better optimised than in GCC). ... I think it's also available on Windows, although I have ever tried it there. In any case, MSVC works, too.
-
You can use MSVS if you prefer, Qt is not choosy about IDE it's being developed in ;) There is also some MSVS Qt plugin available from Downloads page, you can check it out.
I'm not a big Windows user myself, though, so I can help you too much there.
-
Thanks a lot! Sure it's better to start with Qprocess. My basic code has no API stuff (just pure computations and display on console). So that could be more complicated to merge together... :)