Class

class Logger: public QObject{ Q_OBJECT public slots: void write(QString txt); //записываем в файл void process(); public: Logger(); private: void createFile(); QFile LogFile; }; 

I create a stream of this class:

 QThread* thread = new QThread; Logger* log = new Logger(); log->moveToThread(thread); connect(thread, SIGNAL(started()), log , SLOT(process())); connect(this, SIGNAL(write(QString)), log , SLOT(write(QString))); thread->start(); 

Help with creating a stream that will receive a large number of signals, place them in a queue and process them. If the queue is empty, then the stream should fall asleep for, say, 1 second. And how to call SIGNAL WRITE from any class of the program?

UPD: I figured out the queue. If you call the write signal EventLoop , they will be in the EventLoop queue, since The slot is in another thread.

    2 answers 2

    What does the flow? In the context of C ++, streams are stream classes. To send a log message on a signal is rather inefficient. Signals / slots 20 times slower than function calls. For logs this is not valid. In addition, the classes that create log messages must be QObjects.

    Hide the writing thread inside the Logger class. Make a mutex-protected Queue in the Logger, in which the boot will save the logs sent to the write () function. Save the logs to a file in a separate thread.

      Perhaps you better use event instead of signal. Then you don’t have to connect signals from all classes in the class-processor, but simply in this class to handle the write event.