Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Connecting a QTcpServer to an existing QtGuiApplication business logic



  • I have developed a tiny server using QTcpServer within an old version of Qt, Qt4. It consists of a simple class that serves each request and which I have initialized in main.cpp file, like this:

    #include <QtGui/QApplication>
    #include "mainwindow.h"
    #include "server/server.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        MainWindow w;
        w.show();
    
        Server server;
    
        return a.exec();
    }
    

    It starts and works fine so far, dealing with TCP sockets and connections. Hello world typical example is working just fine :)

    The problem is that this tiny server is going to be part of a bigger existing Qt4 QtGuiApplication where all the bussiness logic is accessible from MainWindow w class on. Now, as you will expect, I need to "use" (call) that logic (functions, methods, etc.) to compose the responses of my new server, but I can not see how to clearly access that logic from where I am. It is more of an architecture problem, as I see it.

    I have thought that I could use some Qt slots & signals for the communication, but where my Server is initialized, I can not easily link it to that existing internal logic...

    Any ideas of how could it be done?
    Thanks in advance.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Looks like there's some drawing needed ;-)

    From an architecture point of view you do not want to have all the business logic coded inside a widget. As you have your Server class, you will also need other pieces to implement that business logic in a nice encapsulated and testable way.

    The various elements will likely end up member variables of the MainWindow but should be separate pieces.



  • First of all, thanks for the welcome and thanks for the reply.
    I totally agree with you, but I am afraid that I have to tie to the existing application since I do not have access to that code directly.
    I am just looking for a "simple" way to anyhow use the logic without messing up with the current architecture.
    I know you are absolutely right, but I that is not an option for me right now.

    Just to point me in a direction, could you suggest me some approach? i.e., how could I (from my server class) somehow execute or simply call a function or method located in Mainwindow.cpp without messing around?

    Thanks again.


  • Lifetime Qt Champion

    Are you allowed to modify that MainWindow class ?



  • @SGaist Yes. If I ask to, I can do it. Thanks again.


  • Lifetime Qt Champion

    Can you explain what kind of logic should your server trigger ?



  • Of course, the application itself can be categorized as engineering software.
    Logic basically makes calculations and numeric simulations based on some predefined input values, and some others that come over its GUI, from the user in each case.
    The idea behind the server is to implement an extra channel to serve the results of the application through a web page, receiving those GUI input values through web requests, as an alternative to the current standalone desktop application.

    Thank you again.


  • Lifetime Qt Champion

    In that case it seems that you would need to split some stuff anyway. Otherwise you will have to have your GUI application running on possibly a headless server. Doable but not really practical.

    What you could do is split the calculation and business logic in its own library so you can link both the GUI and web version to it.

    This would be a subdirs project. So you can have everything in one project.

    There might be other client/server possible architecture but that might be out of scope.



  • @SGaist said in Connecting a QTcpServer to an existing QtGuiApplication business logic:

    Otherwise you will have to have your GUI application running on possibly a headless server

    Yes, I assume the final deploy will be like that, not very practical, yet valid indeed. Running in a machine with an untouched GUI which also runs the server.
    Knowing that, could you please suggest me some quite specific approach from where to start trying?
    Just the one which does not imply that functionality splitting, no matter if it is not a very pragmatic solution...
    I would appreciate that.
    Thanks for all.


  • Lifetime Qt Champion

    I just thought of something, you might be able to avoid the GUI part on the server machine. Pass a parameter to your application so that you can use the web version in place of the GUI version.

    You can now concentrate on building your web version. The question is: is absolutely everything build within MainWindow or do you have some stuff that you can reuse in the web version ?



  • I am afraid it is all built within MainWindow, and I have to use logic from there.
    Thanks @SGaist


  • Lifetime Qt Champion

    Do you mean it's one big massive class that does absolutely everything ?


Log in to reply