Run Server code in different QThread



  • Hello, i have been struggling for a solution the past days. I want to create a new Qthread as a server(c++) in it and base on the data update the GUI in qml.
    First of all i decided to go with QT Quick Controls 2 Application.
    As i read i need to re implement ::run() function in my Thread class(use server code there).
    My problem is how to emit signals base on c++ code changes, i did tried some examples but nothing gets to work.
    It would be helpful if someone could advice me how to start,is the feasible?

    General Question: Is the correct way to update QML through main.cpp? so new QThread communicate with main.cpp and main.cpp will update QML?


  • Moderators

    @nnicou said in Run Server code in different QThread:

    General Question: Is the correct way to update QML through main.cpp? so new QThread communicate with main.cpp and main.cpp will update QML?

    that wont work. Since your main.cpp should start the event-loop.
    You need to update the GUI/QML from inside the event loop.

    For example one possibility could be to create a global context object and connect it to the thread, In QML you then can connect to this contect object and update the parts you need to.

    But probably it's the best to show some code.



  • I dont have anything that works yet.I only have plain c++ knowledge and i am currently asking in which direction i should go and sorry for my silly questions
    Well i am still wondering if is better to use Widgets instead of QML
    C++ Logic will collect data from a Multi-core simulator through sockets and save ,lets say in a vector cycle by cycle the data in a class/sructure. (I want the Gui to be interactive and get info per Router etc)

    Question:Can i build my whole Graphical enviroment from C++ (MainWindow)and the second thread update that file? (Like FortuneServer Example)
    Which is the better approach QT quick or Widgets?


  • Moderators

    @nnicou said in Run Server code in different QThread:

    Which is the better approach QT quick or Widgets?

    depends what you are planning to do. If you just want a Desktop app, i think it's easier for now to stay with QtWidgets and C++ only.

    Only important thing about the thread communication is you can use signals/slots and make sure you connect to the signals using Qt::QueuedConnection in the connect() statement.



  • Yeah i just want a Desktop app i will search about QueuedConnection thank you for the hint!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.