How send data between two instances of same application
-
wrote on 11 May 2020, 08:58 last edited by
As the title says it, how can I send data between two instances of the same application or in general, between two Qt application. The data will be probably very small, just a parameter object with few strings. The instances don't know about each other so I need first to find any existing instances. Any suggestion for a lightweight solution?
-
As the title says it, how can I send data between two instances of the same application or in general, between two Qt application. The data will be probably very small, just a parameter object with few strings. The instances don't know about each other so I need first to find any existing instances. Any suggestion for a lightweight solution?
hi @dporobic
the first things that come to my mind are
- Sockets (TCP/UDP)
- QSettings(with the same source file)
- QRemoteObject
- QSharedMemory
The most light option is probably QSettings, but you'll have to regularly check, if the File changed or not and it relies on the FileSystem.
So I would suggest QSharedMemory, (if the shared data is reasonably small) -
wrote on 11 May 2020, 09:07 last edited by
There are several methods in Qt: https://doc.qt.io/qt-5/ipc.html
-
wrote on 11 May 2020, 09:11 last edited by
Hey @J-Hilk thanks for the quick reply. The amount of data is small, its basically 0 to ~5 strings or booleans. Can I get notified when new data is available in QSharedMemory? Can't see any signals or slots on the documentation page.
-
Hey @J-Hilk thanks for the quick reply. The amount of data is small, its basically 0 to ~5 strings or booleans. Can I get notified when new data is available in QSharedMemory? Can't see any signals or slots on the documentation page.
@dporobic said in How send data between two instances of same application:
Can I get notified when new data is available in QSharedMemory? Can't see any signals or slots on the documentation page.
I don't think it's build in.
But here's an interesting solution to the situation
https://forum.qt.io/topic/26298/solved-qsharedmemory-checking -
wrote on 11 May 2020, 19:37 last edited by
QSharedMemory will work. QLocalSocket will work either.
-
wrote on 15 May 2020, 07:12 last edited by
I've ended up using QLocalSocket and QLocalServer which seem to be working and lightweight. One drawback is that only ByteArrays are sent between the Caller and Sender, I'm working around that by converting my object to ByteArrays messages on one side and back to objects on the other.
In case someone is looking for a solution:
https://github.com/ksnip/ksnip/blob/master/src/backend/ipc/IpcServer.h
https://github.com/ksnip/ksnip/blob/master/src/backend/ipc/IpcServer.cpp
https://github.com/ksnip/ksnip/blob/master/src/backend/ipc/IpcClient.h
https://github.com/ksnip/ksnip/blob/master/src/backend/ipc/IpcClient.cpp
1/7