简体   繁体   English

C ++ dll和QT GUI之间的通信

[英]Communication between C++ dll and QT GUI

Good day to all of you, whoever sees my question. 祝大家有个美好的一天,无论谁看到我的问题。 I have a dll, which is written on c++ and some GUI QT project. 我有一个用c ++和一些GUI QT项目编写的dll。 The dll function sends packets in this cycle when called: dll函数在被调用时在此循环中发送数据包:

for (int offset = 0; offset < filelen; offset += 4)
{
    for (int i = 0; i < 4 ; i++)
    {
        FPGA_Packet.DATA[i] = (program_data[i + offset];
    }
    if ( SOCKET_ERROR == ( send(mysocket, (char*)&FPGA_Packet, 8, 0) ))
    {
        error = WSAGetLastError();
        return error;
    }
}

The problem is, that i need to increment progressbar every time by offset, when i call "send" function. 问题是,当我调用“发送”功能时,我需要每次按偏移量递增进度栏。 The question is - how can i establish the connection between GUI and DLL? 问题是-如何建立GUI和DLL之间的连接? Must i use slots and signals, or i can solve it with "connect" thread function, or there are simplier or harder options? 我必须使用插槽和信号,还是可以使用“连接”线程功能解决它,还是有更简单或更困难的选择? I need to give the DLL the pointer on form, or somehow use the get/set? 我需要给DLL窗体上的指针,或者以某种方式使用get / set? I'll appretiate any advices, links, examples and all other help. 我将提供所有建议,链接,示例和所有其他帮助。 Thank you. 谢谢。

You have to emit a signal from you function ( or better say a model if we are talking about MVC patter implementation ) and catch it inside your view class. 您必须从函数中发出一个信号(如果我们正在谈论MVC模式实现,则最好说一个模型),并将其捕获到您的视图类中。 Here is a code snippet for this ( I'm using QML for GUI developing but the idea must be clear anyhow ) : 这是用于此的代码段(我正在使用QML进行GUI开发,但是无论如何都必须清楚这个想法):

   QObject * const callViewObject = getView()->getSlotsSignalsObject();

   bool isSlotSignalConnected = false;

   isSlotSignalConnected = QObject::connect( this,
                                             SIGNAL( clearViewSignal() ),
                                             callViewObject,
                                             SLOT( clearViewSlot() ) );

where, getView()->getSlotsSignalsObject() is implemented in the following way : 其中,getView()-> getSlotsSignalsObject()是通过以下方式实现的:

QObject * const QmlViewBase::getSlotsSignalsObject() const
{
   return reinterpret_cast<QObject* const >( m_declarativeView->rootObject() );
}

To declare a signal you have to use the following definition in your class : 要声明信号,您必须在类中使用以下定义:

   signals:

   void clearView();

Don't forget that in case your class utilizes signal-slot feature you must put Q_OBJECT macro inside it. 不要忘记,如果您的班级使用信号槽功能,则必须在其中放入Q_OBJECT宏。

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

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