简体   繁体   中英

C++/Qt: Pass variables between C++ and Qt GUI without the C++ part including the Qt library

Closed:

Thanks everyone for the ideas, will try to work with your provided suggestions so solve my problem!

Problem:

"C++ main" (which CANNOT #include QObject or any Qt lib) gets data, processes the data and passes it into a separate Qt process (Qt GUI in this case).

Data gets visualized within the Qt GUI and provides feedback, eg you can send commands to the "C++ main" (like START/STOP MEASUREMENT).

Visualization of the problem in best paint quality.

Question:

Is there any possible way for the "C++ main" to get feedback from the Qt GUI WITHOUT including Qt in "C++ main" in any way? (The "C++ main" runs on an I/O-card which is not able to process/load the Qt lib.)

Thank you in advance for helpful answers!

Without much code on what goes on in your "C++ main" it is difficult to answer. But if you have class with a proper interface that is created in main and then used for the IO you could do something like the following:

class MyIoHandler {
public:
    enum Command {START, STOP};
    MyIoHandler() {}

    void command(Command command) { d_command = command; } // Set the command
    void getData(MyData& data) { data = d_data; } // Do a deep copy
private:
    void run() 
    {
        while(d_command == START) {
            readDataFromIO();
            d_data = data;
        }
    }
    Command d_command;
    MyData d_data;
};

Then the GUI just need to call the correct functions on the class to interface with the IO handler. There is no need for the main class to know how the GUI looks, but the GUI must know how the class looks.

This is also working on the assumption that they are in the same executable (from the comments) and

You just need to take care about threading etc.

But as mentioned, without some structure or code examples it is very difficult to give a useful answer.

I normally use QUdpSocket (in the Qt world) to talk to my other apps (effectively for IPC). Then your c++ world you can use normal sockets sys/socket.h for the same job. Since your comms is simplistic - ie message passing this is quite easy to do. There is some effort creating your c++ / Qt class to handle your UDP, but from then on its really easy.

The main drawback for me is that the two programs have to agree on a port to use (The IP address would be loop back address 127.0.0.1). So you may have a configuration file, or a command line parameter to set this...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM