qbytearray

Is there a shorter way to initialize a QByteArray?

ぃ、小莉子 提交于 2019-12-08 16:27:48
问题 In my program I work a lot with serial communication so QByteArray is used very often. I was wondering if there was a shorter way to initialize a QByteArray with specific bytes than: const char test_data[] = { static_cast<char>(0xB1), static_cast<char>(0xB2), 0x5, static_cast<char>(0xFF), static_cast<char>(0xEE), static_cast<char>(0xEE), static_cast<char>(0x0)}; // Note QByteArray should be able to hold 0 byte const QCanBusFrame frame = QCanBusFrame(0xA1, QByteArray(test_data)); The static

How to convert QVector<double> to QBytearray

时光毁灭记忆、已成空白 提交于 2019-12-08 14:01:26
I would like to convert QVector<double> to QBytearray , and I have no idea on how to do this. I tried this but the program crashes: QVector<double> vec; QByteArray arr = QByteArray::fromRawData(reinterpret_cast<const char*>(vec),vec.size()); for(int i = 0; i< vec.size(); i++) arr.append(reinterpret_cast<const char*>(vec.data(&numberOfData),sizeof(double)); Can someone tell me how to do it properly? You must pass through a native array, so your QByteArray will receive a sequence of contiguous bytes. double *bytes = new double[vec.size()]; for (int i = 0; i < vec.size(); ++i) { bytes[i] = vec[i]

How to convert QVector<double> to QBytearray

自古美人都是妖i 提交于 2019-12-08 04:07:29
问题 I would like to convert QVector<double> to QBytearray , and I have no idea on how to do this. I tried this but the program crashes: QVector<double> vec; QByteArray arr = QByteArray::fromRawData(reinterpret_cast<const char*>(vec),vec.size()); for(int i = 0; i< vec.size(); i++) arr.append(reinterpret_cast<const char*>(vec.data(&numberOfData),sizeof(double)); Can someone tell me how to do it properly? 回答1: You must pass through a native array, so your QByteArray will receive a sequence of

How can I convert QByteArray to string in Qt 5.3?

自作多情 提交于 2019-12-08 01:36:15
问题 I am using some functions to convert QVector's to QByteArray's , for example: QByteArray Serialize::serialize(QVector<double> data) { QByteArray byteArray; QDataStream out(&byteArray, QIODevice::WriteOnly); out << data; return byteArray; } void Serialize::deserialize(QByteArray byteArray, QVector<double> *data) { QDataStream in(&byteArray, QIODevice::ReadOnly); in >> *data; } Now, that I have the QByteArray I need to put it in a text file, how can I convert it to QString ? I already tried the

Append a QByteArray to QDataStream?

£可爱£侵袭症+ 提交于 2019-12-06 09:02:04
问题 I have to populate a QByteArray with different data. So I'm using the QDataStream . QByteArray buffer; QDataStream stream(&buffer, QIODevice::WriteOnly); qint8 dataHex= 0x04; qint8 dataChar = 'V'; stream << dataHex<< dataChar; qDebug() << buffer.toHex(); // "0456" This is what I want However, I would also like to append a QByteArray to the buffer . QByteArray buffer; QDataStream stream(&buffer, QIODevice::WriteOnly); qint8 dataHex= 0x04; qint8 dataChar = 'V'; QByteArray moreData = QByteArray:

Qt QJson解析json数据

无人久伴 提交于 2019-12-04 18:28:28
Qt QJson解析json数据 //加载根目录文件 void TeslaManageData::loadRootFolderFiles() { QNetworkAccessManager *manager = new QNetworkAccessManager(this);; QNetworkRequest network_request; QByteArray post_data; network_request.setUrl(QUrl("http://121.60.49.57:9080/tsinghuaVCService/init/selectAllFolder.action")); network_request.setHeader(QNetworkRequest::UserAgentHeader, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 UBrowser/6.2.4098.3 Safari/537.36"); connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*))); //发送请求

Append a QByteArray to QDataStream?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 14:08:02
I have to populate a QByteArray with different data. So I'm using the QDataStream . QByteArray buffer; QDataStream stream(&buffer, QIODevice::WriteOnly); qint8 dataHex= 0x04; qint8 dataChar = 'V'; stream << dataHex<< dataChar; qDebug() << buffer.toHex(); // "0456" This is what I want However, I would also like to append a QByteArray to the buffer . QByteArray buffer; QDataStream stream(&buffer, QIODevice::WriteOnly); qint8 dataHex= 0x04; qint8 dataChar = 'V'; QByteArray moreData = QByteArray::fromHex("ff"); stream << dataHex<< dataChar << moreData.data(); // char * QByteArray::data () qDebug()

TCPIP 数组传输

别等时光非礼了梦想. 提交于 2019-12-04 06:56:08
最近一直在弄TCPIP进行数据传输,刚刚入门,网上搜了很多例子,今天终于完成了一个小demo 客户端发数组:主要就是数据类型的转化从float 转化成QByteArray //1.建立客户端连接套接字connected 2.建立连接客户端不断监听服务器的信息readyread //3.读取服务器信息readAll 4.设置连接按钮tcpsocket->connectToHost; //5.设置发送信息的按钮write() //6.关闭套接字disconnectFromHost()和close() #include "widget.h" #include "ui_widget.h" #include <QHostAddress> Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); tcpsocket = new QTcpSocket(this); connect(tcpsocket,&QTcpSocket::connected, [=]() { QString str = "successfully connect!"; ui->textBrowser->append(str); }); connect(tcpsocket,&QTcpSocket:

How can I partition a QByteArray efficiently?

回眸只為那壹抹淺笑 提交于 2019-12-04 03:50:20
问题 I want to partition a QByteArray message efficiently, so this function I implemented take the Bytes, the part I want to extract, and toEnd flag which tells if I want to extract part1 till the end of the array. my dilimeter is spcae ' ' example if I have: ba = "HELLO HOW ARE YOU?" ba1 = getPart(ba, 1, false) -> ba1 = "HELLO" ba2 = getPart(ba, 2, true) -> ba2 = "HOW ARE YOU?" ba3 = getPart(ba, 3, false) -> ba3 = "ARE" the function below works just fine, but I am wondering if this is efficient.

How to convert QByteArray to std::istream or std::ifstream?

我的梦境 提交于 2019-12-03 18:10:51
问题 I want to create istream from QByteArray at runtime, without saving a physical file in memory of QByteArray . I found that there are many ways to do the opposite conversion, i.e. istream to QByteArray , but not this one. How to accomplish that? 回答1: To read via std::istringstream from QByteArray seems quite easy: testQByteArray-istream.cc : #include <iostream> #include <sstream> #include <QtCore> int main() { qDebug() << "Qt Version:" << QT_VERSION_STR; // make a QByteArray QByteArray data(