Qt Creator语言 - 写入串行接收数据到UI
Qt Creator - write serial received data to UI
我正在与qt creator一起工作,我想获得一个QByteArray到实现UI的类,让我们说类a类B实现串行连接(RS232)并以以下方式接收数据:
QSerialPort::connect(serialport,SIGNAL(readyRead()),this,SLOT(SerialPortManager::GiveReceivedDataToUI()));
因此,在我的方法SerialPortManager::GiveReceivedDataToUI(){}
中,我将从我的串行连接中读取所有数据,如下所示:serialport->readAll();
数据(存储在QByteArray中)仍然在b类中。现在我想将这些数据获取到a类中的文本框中。
我看到了一些关于访问来自不同类的UI元素的线程。尝试了另一个连接,但我只能弄清楚如何将我的数据连接到类a (UI)中编写文本框的方法。我想用体面的OO来写它,而不仅仅是把它公开。
有什么建议吗?
更新
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
可能是这样的。这也是与使用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)))
}
相关文章:
- 防止主数据类型C++的隐式转换
- 用于访问容器<T>数据成员的正确 API
- 嵌套在类中时无法设置成员数据
- 使用流处理接收到的数据
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 在cuda线程之间共享大量常量数据
- C++将文本文件中的数据读取到结构数组中
- 如何在MS Visual Studio 2019中运行QT UI
- 如何在C++中序列化结构数据
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 在c代码之间共享数据的最佳方式
- 链表,反向函数,数据结构
- 数据成员SFINAE的C++17测试:gcc vs clang
- 从Qt中的对话框获取数据并在Ui中使用
- 使用具有非类表数据和非表/列表UI的Qt模型/视图
- Qt Creator语言 - 写入串行接收数据到UI
- 将用户在Qt4 UI中输入的数据写入文件
- 如何/应该在Qt中创建ui表单和数据成员之间的自动链接