QT编写的串口助手,串口发送16进制数据的结尾会默认加上一个空格,怎么将空格去掉?

发送的数据结尾是没有空格的,但虚拟串口接收到的数据结尾却多了一个空格,怎么可以去掉这个空格。
QString Dat = ui->sendTextEdit->toPlainText().toLatin1();//获取文本框内容
QByteArray Data_4 = QByteArray::fromHex (Dat.toLatin1().data());//按十六进制编码发送
// 写入发送缓存区
serial.write(Data_4);

img

img

while(Dat.at(Dat.length()-1) == ' ')
  Dat.replace(Dat.length()-1,1,"");

或者

while(Dat.right(1).compare(" ") == 0)
        Dat = Dat.left(Dat.length() -1);