简体   繁体   English

在静态函数中发出信号

[英]Emit a signal in a static function

I've got aa static function : static void lancerServeur(std::atomic<bool>& boolServer) , this function is force to be static because I launch it in a thread, but due to this, I can't emit a signal in this function. 我有一个静态函数: static void lancerServeur(std::atomic<bool>& boolServer) ,这个函数是强制静态的,因为我在一个线程中启动它,但由于这个原因,我无法发出信号在这个功能。 Here's what I try 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 a way to emit my signal ? 有没有办法发出我的信号?

Thanks. 谢谢。

You can keep a static pointer to the instance of MainWindow in the MainWindow class and initialise it in the constructor. 您可以在MainWindow类中保留一个指向MainWindow实例的静态指针,并在构造函数中初始化它。 Then you can use that pointer to call the emit from the static function. 然后,您可以使用该指针从静态函数调用emit。

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 );
}

Pass the pointer to the class as a parameter to lancerServeur, or you can use a slot on a worker class and move it to a thread. 将指针作为参数传递给lancerServeur,或者您可以使用工作类上的插槽并将其移动到线程。

See this example http://qt-project.org/doc/qt-4.8/qthread.html on how to use a slot to do the work on a separate thread. 请参阅此示例http://qt-project.org/doc/qt-4.8/qthread.html ,了解如何使用插槽在单独的线程上执行工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM