如何将单例类信号连接到主窗口类槽或方法

时间:2018-04-28 00:13:01

标签: c++ qt

我拥有一个串行通信类,我希望在收到推荐信号时发送信号,因为数据接收结束了。由于这个信号,我正在调用图形方法。建立连接,但接收时隙不听信号。

单身人士课程:

class SerialCommunication : public QObject{
Q_OBJECT public:
static SerialCommunication  &   GetInstance()                                                               ;
....

QVector<double>                 ReadDataVector                                                              ;  

私人:

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

私人广告位:      void ReceivedData();

信号:      void DataIsDone();

};

ReceivedData插槽:

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 :: connect:预期的括号,信号SerialCommunication :: SerialCommunication :: DataIsDone in .. \ Muteferrika \ MainWindow.cpp:10 QObject :: connect :(接收者名称:&#39; MainWindow&#39;)

1 个答案:

答案 0 :(得分:0)

使用旧的Qt4语法,connect调用应为...

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

说过你真的应该使用Qt5提供的较新版signal-slot syntax。具体地说...

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