Qt Inter-Process function trigger - c++

I know I can use QSharedMemory to transfer information between applications (I've used it successfully). But I'd like to trigger a function on another application. Ex: App1 has a button that when pressed triggers a function on App2 that prints "HelloWorld".
I've checked IPC solutions' page and I see that linux supports D-Bus communications and Qt uses it to extend signals and slots to IPC level, which is essentially the kind of thing I want. But I'm working on Windows for now and need a solution that is compatible with both OSs.
I also see that perhaps I could use TCP/IP solutions in Qt library to establish a server/client communication, but given that I'm not so versed in it I'd prefer something simpler. But maybe TCP/IP is quite simple and with a few variables and functions calls I could get it done. Please tell me if that's the case.
I've also thought of using a boolean variable on QSharedMemory that is set by the master app and is regularly checked and cleared by the slave app when a timer, running on a loop, runs out. But that means more overhead. It's not at all a clean solution, but it would work ok in my case.

Related

What's the best way to connect a Qt4 and a Qt5 process by IPC?

I want to build an application which is based on two separate processes. One of them (Process 1) is using Qt4 for accessing the functionalities of a legacy code base. The other one (Process 2) is the UI layer of the application using Qt5.
I'll need to access the functions of Process 1 from Process 2, and I'll need to access the results of Process 2 from Process 1.
Can anyone suggest a best practice for connecting the two processes via IPC?
http://doc.qt.io/qt-4.8/ipc.html
According to the link you have to choose between TCP/IP (QNetworkAccessManager etc.) or Shared Memory with (QSharedMemory). In your case DBUS would not be a good idea as you are working on windows.
I can also suggest to have a look at QProcess, through that you can make your QT5 application execute your QT4 application and collect the result from standard output.
It depends a lot on how much data you need to exchange and how flxible you are with your legacy stuff.
Personally if it is possible I would go for the QProcess.

How do I get my Qt console app to communicate with another Qt gui app?

My project needs to have two parts to it. The first part where input is taken from a Qt console window. The input is processed and a signal is sent to the Qt gui app (the second part) which accordingly updates the UI. How Do I implement this? Can these be a part of the same app, or do I need to keep the two separate and communicate between the two?
Please direct me to the specific classes and functions I would have to use.
I've had a look at QProcess but wasn't sure whether that would serve the purpose.
I think that it is not possible to run both console and GUI application in Qt. You can try create .exe file which will be your console and another .exe which will be the GUI application.
To run console from GUI application you should use QProcess where you have to specify the absolute path to your console executable.
More information about QProcess you can found here.
If you are working on UNIX or GNU/Linux systems, you can use QtDBus module.
You can write 2 softwares, one using a console, the other using GUI and make them communicating through D-Bus. You can find some examples here.
The QtDBus module is a Unix-only library you can use to implement IPC using the D-Bus protocol. It extends Qt's Signals and Slots mechanism to the IPC level, allowing a signal emitted by one process to be connected to a slot in another process. This D-Bus page has detailed information on how to use the QtDBus module.
Also, you can use TCP/IP sockets wrapped by Qt to communicate. This is a portable solution.
Qt Fortune Server and Qt Fortune Client are examples well written and explained step by step.
Every solutions for Inter Process Communication (IPC) proposed by Qt is proposed on this page.
As Qt 5.9, there is module which is currently under technology preview name QtRemoteObjects which provides Qt native IPC for QObjects, its easy to use and could be used to share signal/slots and also models between two processes

Send events between two Qt windows

I have created a Qt application named my-app. And I start it by typing in the terminal:
./my-app
Now I want to send an event between two independent windows from the same application. So, suppose that I have two terminal tabs and I start my-app by typing:
./my-app 1
./my-app 2
Can the first instance of the app (./my-app 1) send an event to the second one, and when the second window receives this event to print something in the terminal using qDebug() (maybe using the second argument passed in args)?
If yes, how?
Qt provides various ways of communicating between apps. If the applications are to be run on the same machine, then I recommend you take a look at using QLocalServer and QLocalSocket.
The first instance of the application can look to see if it can connect to a QLocalServer and if it fails, create its own.
It's not too hard to work with and if you look at the Qt examples, you should find a QLocalSocket example that will demonstrate how to use it.
On Windows, QLocalSockets use named pipes, so non-Qt apps could also connect to it too.
The fortuneServer example from Qt shows how you can use QTcpSocket and QTcpServer and the interface is almost identical for QLocalSocket and QLocalServer
Even if you use the same application, you run more than one instances of it, so that you need to implement communication between the processes. In this regard I would suggest to read the following paragraphs from the Qt documentation.

Getting QT to respond to power-events (sleep/hibernate)

I'm trying to develop software that is intelligent wrt sleep events (cleanly closing network connections, making sure data restart locations are set properly, etc).
Are there mechanisms in QT (4.6) currently that facilitate me responding to system power events?
I dont think there's a native to qt and multi platform way for these but there are propably some ways and api's to do things you are looking for. Posix signals might provide something to notify your app about ongoing system events. Also you might want to look QDbus stuff, some cases dbus will broadcast system events.. But how to do the stuff you are really looking for is very os dependant and without knowning your target os, its rather hard to point out a optimal solution.
No, nothing like that in Qt no. Probably not and will not.
You need to use the API of the operating system.

How can I communicate between two C++ MFC plugins?

I have a plugin for a c++ MFC app. I'm working with the developer of another plugin for the same app, that's trying to get notifications of events in my code. Both plugins are in the form of c++ dlls.
How can I pass messages from my plugin to his plugin? The solution needs to be robust to mismatched versions of our two plugins, as well as the host app. The notifications are during control point movement, so several times a second.
I could set up a callback mechanism, where upon load his plugin calls a function in my plugin with a function pointer. We're not guaranteed any loading order, but we could probably just check periodically.
I know Win32 has a messaging system, but I'm not sure how it works, really. We could add a hook, and I could send messages, but I'm a bit fuzzy on how we'd synchronize what the message id is, or any details other than what I said, really.
Any other ideas on how to do this?
I'm a bit fuzzy on how we'd synchronize what the message id
Use the RegisterWindowMessage API.
Take a look at this article here, it shows the available IPC mechanisms in windows. I might try COM, Mailslots, Pipes or Shared Memory (file mapping) in your case, in addition to windows messages which you already mentioned.