Emit a signal in a static function

I have a static function:, static void lancerServeur(std::atomic<bool>& boolServer)this function is static, because I run it in the stream, but because of this I can not emit a signal in this function. Here is what I am trying to do:

void MainWindow::lancerServeur(std::atomic<bool>& boolServer){
    serveur s;
    StructureSupervision::T_StructureSupervision* bufferStructureRecu;
    while(boolServer){
        bufferStructureRecu = s.receiveDataUDP();
        if(bufferStructureRecu->SystemData._statutGroundFlight != 0){
            emit this->signal_TrameRecu(bufferStructureRecu);//IMPOSSIBLE TO DO
        }
    }
}

Is there any way to emit my signal?

Thank.

+4
source share
2 answers

You can save a static pointer to an instance of MainWindow in the MainWindow class and initialize it in the constructor. You can then use this pointer to call the emission from a static function.

class MainWindow : public QMainWindow
{
    ...
    private:
        static MainWindow* m_psMainWindow;

        void emit_signal_TrameRecu(StructureSupervision::T_StructureSupervision* ptr)
        {
            emit signal_TrameRecup(ptr);
        }
};

// Implementation

// init static ptr
MainWindow* MainWindow::m_psMainWindow = nullptr; // C++ 11 nullptr

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
{
    m_psMainWindow = this;
}


void MainWindow::lancerServeur(std::atomic<bool>& boolServer)
{
    StructureSupervision::T_StructureSupervision* bufferStructureRecu;

    ...

    if(m_psMainWindow)
        m_psMainWindow->emit_signal_TrameRecu( bufferStructureRecu );
}
+4
source

Source: https://habr.com/ru/post/1546515/


All Articles