0

I have two separate c++ projects that run simultaneously and exchange information between them using TCP/IP. The names of those two projects are "World" and "Platform". To display Data and output in Real time, I have created a Qapplication object inside "World" class. (I do not want to use Qt forms to create QGprahics view I just want to code it). TCP/IP works without any problems. here is my code:

main.cpp:

#include "QTcpServer"
#include "QTcpSocket"
#include "QApplication"
#include "QGraphicsScene"
#include "QTime"

World::World()
{
    WorldSettings  settings;
    Sys    = new Dynamics(settings);
}


void World::run()
{
   while (!time->finished())
    {
        if (controlStatus)
        {
            sendWorldData();
            getPlatformData();
        }

        displayData();

    }



int World::displayData()

{   char *argv[] = {"a", "arg1", "arg2", NULL};
    int argc = sizeof(argv) / sizeof(char*) - 1;

 QApplication a(argc, argv);

 QGraphicsScene * scene = new QGraphicsScene();

 // create an item to put into the scene
 QGraphicsRectItem * sag = new QGraphicsRectItem();

sag->setRect(0,0,10,15);

view->show();
 return a.exec();
}

with run this file, in first step time, the widget viwe is display and then the program stops running.(In other words, the program stops in the first iteration). I expect the code I'm writing to behave like this:First, the widget is displayed and then the data are displayed in the corresponding graphic items, number displays, etc. and in every iteration of the code, these display objects are updated. In other words, I have a real time program that I need to display it's output data in real time.

esmaeil
  • 1
  • 2
  • It's not clear what you expect this code to do. `World::run` isn't called anywhere. Also, `a.exec()` will block until some event occurs that causes the event loop to quit. You should try to clarify the problem and provide a [mcve]. – G.M. May 16 '20 at 09:56
  • It's a bad idea to place any endless loop into the GUI thread of a Qt application. `QApplication` provides it's own event loop inside `QApplication::exec()`. It calls event handlers for resp. events which should return in as least time as possible (or would make the whole application blocking otherwise). A usual solution is either to use `QTimer` for things like periodical update or to run separate threads (e.g. using `QThread`). However, please note that Qt provides TCP/IP support with event handlers as well: [QTcpSocket](https://doc.qt.io/qt-5/qtcpsocket.html) – Scheff's Cat May 16 '20 at 09:57
  • @Scheff, I did not understand your last suggestion for used QTimer. I want to display Realtime data in form of Graphically in qt. What is your best suggestion for me? It is possible to introduce an Tutorail link in this field?Thank you I really appreciate your help. – esmaeil May 16 '20 at 11:26
  • For the usage of a `QTimer`, Qt doc. provides e.g. [Analog Clock Example](https://doc.qt.io/qt-5/qtwidgets-widgets-analogclock-example.html). For the usage of [QTcpSocket](https://doc.qt.io/qt-5/qtcpsocket.html), just follow the link. Its doc. links itself the various available examples. For mixing the GUI with `std::thread`s, I just wrote an answer recently for [SO: How to alter Qt Widgets in WINAPI threads?](https://stackoverflow.com/a/61750145/7478597). However, I would prevent threads whenever possible. (They often cause additional trouble.) ;-) – Scheff's Cat May 16 '20 at 11:31
  • 1
    Btw. I wouldn't stretch the "Realtime" requirement too much concerning Qt. On one hand, Qt is good for a lot of things but not for specifically reliable repetition nor for specifically fast graphics. On the other hand, this is rarely really necessary. The perception rate of humans is 10 images per second. Cinema uses 15 images per second to provide what we usually recognize as smooth animation. Hence, Qt is usually fast enough when used right. (We do visual simulation with OpenGL in a Qt appl. and are quite satisfied with the outcome - after "tighten the screws" here and there.) ;-) – Scheff's Cat May 16 '20 at 11:35
  • 1
    @Scheff , I don’t know how to thank you! I'm grateful for your assistance Dear Scheff. – esmaeil May 16 '20 at 12:41

0 Answers0