如何将Singleton类信号连接到MainWindow类插槽或方法

How can I connect singleton class signals to mainwindow class slot or method

本文关键字:MainWindow 插槽 方法 信号 Singleton 连接      更新时间:2023-10-16

我拥有一个串行通信类,并且由于数据接收到结束,我想在获得赞誉时发送信号。由于此信号,我正在调用图形方法。建立了连接,但是接收插槽不听信号。

singleton类:

class SerialCommunication : public QObject{
Q_OBJECT public:
static SerialCommunication  &   GetInstance()                                                               ;
....
QVector<double>                 ReadDataVector                                                              ;  

私人:

SerialCommunication()                                                                                       ;
static SerialCommunication  *   Instance                                                                    ;
QList<QSerialPortInfo>          PortList                                                                    ;
bool                            IsOpen          =   false   ; ....

私人老虎机: void readdata((;

信号: void dataisdone((;

};

接收数据插槽:

void SerialCommunication::ReceivedData()

{ ...

for(;true;)
{
    ....
    switch (Commend) {
    case Data_Reply:
        qDebug()<<"Data Reply" ;
        AppendToUint16Vector(ReadDataVector,Package);
       break;
    case Data_Reply_Done:
        qDebug()<<" Data_Reply_Done" ;
        MsgBox.information(0,"Transfer information. ","The data transfer is complete n Please click draw button.");
        emit SerialCommunication::GetInstance().DataIsDone();
       break;
   default:
        ClearBuffer();
        qDebug()<<" default" ;
       break;
    }}}

和连接在这里

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(&SerialCommunication::GetInstance(),SIGNAL(SerialCommunication::DataIsDone),this,SLOT( GraficSetupAndInit()));
    SetupUI();
}

qobject ::连接:预期的括号,信号serialCommunication :: serialCommunication :: dataisdone in .. muteferrika mainwindow.cpp:10Qobject :: Connect :(接收器名称:'MainWindow'(

使用旧的Qt4语法您的connect呼叫应该是...

connect(&SerialCommunication::GetInstance(), SIGNAL(DataIsDone()),
        this, SLOT(GraficSetupAndInit()));

说您确实应该使用Qt5可用的较新的信号插槽语法。具体...

connect(&SerialCommunication::GetInstance(), &SerialCommunication::DataIsDone,
        this, &MainWindow::GraficSetupAndInit);