Qt Creator - 将串行接收数据写入 UI

Qt Creator - write serial received data to UI

我正在与 qt creator 合作,我想获得一个 QByteArray 到实现 UI 的 class,比方说 class A. Class B实现串行连接(RS232)并通过以下方式接收数据:

QSerialPort::connect(serialport,SIGNAL(readyRead()),this,SLOT(SerialPortManager::GiveReceivedDataToUI()));

所以在我的方法中 SerialPortManager::GiveReceivedDataToUI(){} 我将从我的串行连接中读取所有数据,如下所示: serialport->readAll(); 数据(存储在 QByteArray 中)仍在 Class B 中。现在我想将此数据添加到 Class A.

中的文本框

我红了一些关于从不同 class 访问 UI 元素的话题。尝试使用另一个连接,但我只是想知道如何将我的数据连接到 Class A(UI) 中写入文本框的方法。我想用体面的 OO 编写它,而不仅仅是制作这个 public.

有什么建议吗?

UPDATE

class SerialPortManager : QMainWindow
{
     Q_OBJECT
private:    
    static SerialPortManager* instance;    
protected:
    SerialPortManager();
public:
    static SerialPortManager* GetInstance();
    void OpenSerialConnection();
    void CloseSerialConnection();
    void WriteSingleACLCommand(QString);
    void WriteMultipleACLCommands();

public slots:
    void GiveReceivedDataToUI();
signals:
    void Send(QByteArray& s);

};
#endif // SERIALPORTMANAGER_H

可能是这样。这也只是与 class 通信的概念,它用于使用 QThread 发送和接收数据 SIGNAL 和 SLOT。这也是安全的,因为我们可以避免冻结。 (未经测试的代码。)

        class SerialPortManager : QSerialPort
        {
        Q_OBJECT

        public:
        SerialPortManager();

        private slots:
        SLOT_ReciveData()

        // here slot which will be received command from difrent class
        public slots:
        void CommandFromMainWindow(QByteArray command); 

        // here signal which will be emited if data was be received
        signals:
        void SendToMainWindow(QByteArray s); 

        };

        /******SOURCE********/
        SerialPortManager:: SerialPortManager()
        {
    // here also all setup Your serial port

        // in this connection we will be received data. This happen also in diffrent thread, because this is a part signal which was be send from main window
        QObject::connect(this , SIGNAL(readyRead()) , this , SLOT(SLOT_ReciveData()) , Qt::QueuedConnection);

}

// and slot to recived directly form Serial Port               
        void SerialPortManager::SLOT_ReciveData()
        {

                /************HERE WE GOT SOME DATA********/
        QByteArray buffer;

        while(this->bytesAvailable() > 0) // we read to time is data is available in buffer
        {
        buffer.append(this->readAll());

        }

        /******If any data is yet avaiable we send SIGNAL to mainwindow!!!!****/

        SendToMainWindow(buffer)

        }

MainWindowClass 是 Singelton

class MainWindow : public QMainWindow
{
private:

QThread *threadCOM;
SerialPortManager *comconfigure;         

// From this signal we something send to Serial Port for example some command
signals:
void SIGNAL_To_SerialManager_LetMeSomeData(QByteArray cmd);

// And here we something received form Serial Port
public slots:
void SLOT_Yeah_I_Get_Data(QByteArray &s);

};

MainWindow::MainWindow()
{

threadCOM = new QThread(this); // new thread for your class with serial port
comconfigure = new SerialPortManager();
comconfigure->moveToThread(threadCOM); // attachment particular thread to pinter Your class
threadCOM->start(QThread::HighestPriority); // and start, but wait for signal

// here connect signal form main windowd which send command to serial port
QObject::connect(this , SIGNAL(SIGNAL_To_SerialManager_GevMeSomeData(QByteArra);) , comconfigure , CommandFromMainWindow(QByteArray)))

// also connect signal to recived data to main window
QObject::connect(comconfigure , SIGNAL(SendToMainWindow(QByteArray)) , this, SLOT_Yeah_I_Get_Data(QByteArray)))

}