Send events between two Qt windows - c++

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.

Related

Qt Inter-Process function trigger

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.

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.

Control c++ application remotely via web interface

I created a C++ QT application on Linux with a simple visual interface with three buttons. This application runs on a machine with a certain IP address in my wifi network. I want to be able to access such visual interface from the browser of a smartphone and click the buttons.
To accomplish this I used a remote desktop connection, but it's just a temporary solution as I want to be able to access my GUI from any smartphone without the need of installing anything, and also without offering other functionalities... the client should be able to press the three buttons and nothing more.
In other words, I would like to be able to do the following:
After I type the IP address of the linux machine in the browser of my smartphone, a html page opens up with my visual interface with three buttons;
When I press one button, my C++ code starts running in the background;
When I press again the buttons, the C++ app receives the commands and acts accordingly.
And so on until I click the button that closes my C++ App.
Now, is there a way to accomplish this? In a few words, is it possible to have a web interface to act as a GUI for my C++ app? I must admit I am really ignorant in web applications :) But maybe you know about a QT widget that solves my problems.
Thanks!
There are a lot of ways to accomplish this.
A simple way is to use QtHttpServer to talk to the objects inside your Qt Application to do the work you asked it to.
You can quickly get started by adapting the example here to your use case.
You may want to check out https://sourceforge.net/projects/conair/ - this lib allows you to communicate with your c++ qt code via javascript. You would, however, have to replicate your existing qt gui with a traditional web front-end technology - some straight forward HTML and javascript would probably suffice.

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

Managing Windows Though QT Application in Linux (C++)

I've been making a simple application which is able to launch a variety of other applications through QT5 (using QProcess class) but I've been running into a few key issues with the design. Specifically, it seems that Qprocess cannot set focus to windows that have been created via QProcess' start() function. This means that once a user opens more than one window, it can never return to the previous window that has been opened. After looking further into this dilemma, it has become clear that my program will need to be able to handle basic window management in order to fulfill my specifications.
I've decided that the best example to study for my program is Docky, which is capable of opening, closing and switching windows. Looking at the source code for that project was helpful, but there were many C# system calls that were used for fetching the list of client-windows which aren't available for my C++ program.
How can I get a list of all the X11 Windows that the client is running and provide basic window management (Switch To/ Open / Close Window) using C++? Is there a cross platform way of doing this through QT? Can I get this information directly using XServer?