QByteArray 的外部与内部声明 [英] External vs internal declaration of QByteArray
问题描述
我目前在使用 QSerialPort
时遇到一些问题:当我使用一个示例中的函数时,它看起来像
I have currently some problems with the QSerialPort
: When I am using the function from an example which looks like
QKeyEvent *e;
emit getData(e->text().toLocal8Bit());
connect(console, SIGNAL(getData(QByteArray)), this, SLOT(writeData(QByteArray)));
void MainWindow::writeData(const QByteArray &data)
{
qDebug() << "Data is to write: " << data;
serial->write(data);
}
然后接收设备可以处理数据.但是当我将函数 writeData()
更改为
then the receiving device can work with the data. But when I change the function writeData()
to
void MainWindow::writeData(const QByteArray &data)
{
QString a = "Q";
QByteArray b = a.toLocal8Bit();
serial->write(b);
}
接收设备无法处理接收到的数据.这两种方法的区别在哪里?
更新:我发现显然只有在键入字母后按 Enter
才能传输数据.不知何故,'\n'
在从 QString
到 QByteArray
的转换中丢失了.我怎样才能保留它?
the receiving device can not work with the received data. Where is the difference between those two approaches?
Update: I found out that apparently the data is only usefully transferred if I press Enter
after typing the letters. Somehow the '\n'
gets lost in the conversion from QString
to QByteArray
. How can I keep it?
推荐答案
你应该像这样在你的Qstring中添加一个回车
you should add an enter to your Qstring like this
QString a = "Q\x00D";
这篇关于QByteArray 的外部与内部声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!