qbytearray

QT开发之旅二TCP调试工具

不想你离开。 提交于 2019-12-25 21:24:46
TCP 调试工具顾名思义用来调试 TCP 通信的,网上这样的工具 N 多,之前用 .NET 写过一个,无奈在 XP 下还要安装个 .NET 框架才能运行,索性这次用 QT 重写,发现 QT 写 TCP 通信比 .NET 还要便捷一些,运行效率貌似要高,还能识别客户端断开,这个真神奇,除了断电之外。 项目名称: TCP 调试工具 开发环境: WIN7+QT4.7+QT CREATOR2.8+MINGW 技术实现:通过 QTcpServer 和 QTcpSocket 类,解析协议并作出处理 实现功能: ASCII 格式和 16 进制数据收发,支持多个客户端收发消息,可以指定客户端发送消息,动态增加和移除已连接客户端。 运行截图: 粗略步骤: 第一步:添加主界面,布局好主界面,并命名好控件,例如服务端的清空按钮命名为 btnClearServer ,客户端的清空按钮命名为 btnClearClient 。 第二步:编写服务端中客户端通信类,服务端可以接受多个客户端的连接,这里采用了同步通信机制,先编写 myTcpClient 类,封装了客户端连接断开接收数据的操作。具体代码如下: myTcpClient.h #ifndef MYTCPCLIENT_H #define MYTCPCLIENT_H #include <QTcpSocket> class myTcpClient :

Convert QByteArray from big endian to little endian

孤街醉人 提交于 2019-12-24 23:03:52
问题 I think I’m a kind of at a loss here. I trying such a simple thing, I can’t believe that there is nothing build-in Qt (using Qt 5.6.2). I try to convert the data inside a QByteArray from big endian to little endian. Always starts with the same test QByteArray like this. QByteArray value; value.append(0x01); value.append(0x02); value.append(0x03); qDebug() << "Original value is: " << value.toHex(); // “010203” like expected What I need is the little endian, which means the output should be

Qt编写图片及视频TCP/UDP网络传输

无人久伴 提交于 2019-12-16 10:50:31
一、前言 很多年前就做过类似的项目,无非就是将本地的图片上传到服务器,就这么简单,其实用http的post上传比较简单容易,无需自定义协议,直接设置好二进制数据即可,而采用TCP或者UDP通信的话,必须自定义协议,因为不知道什么时候数据接收完了是完整的图片数据,可能同时在发送很多图片数据,而且还不能区分收到的图片是哪个客户端发来的,TCP长连接的话,还需要有心跳来检测连接,所以必须自定义一套协议来支撑通信,这套协议采用的是上海监管平台的通信协议格式,拓展性比较强,其中头部信息包括了类型+当前完整包的数据长度,这个类型就是通信协议的标识,这样下次来一个其他类型的比如楼宇对讲可以叫IDOOR,服务端根据这个标识就能知道采用何种解析算法来处理后面的数据,而当前完整包的数据长度可以用来处理收到的数据,只有该长度的数据才表示接收完成一个完整的图片数据,再去解码处理。当传输的图片到了一定速度的时候比如一秒钟传输20张图片,其实就相当于传输视频了,一般人的肉眼看到一秒钟20张图片基本上认识就是视频了。 TCP理论上是稳定的连接,不会丢包,也不会随便一个包插入到一个包的中间,肯定能保证一个数据包的完整性,TCP连接也分两种,一种是长连接,一旦连接了就一直通信,主要用在频繁通信的场景中比如实时上传,还有一种叫短连接,客户端发完数据或者服务端接收完数据就立即断开连接

Best practice: how to interpret/process QDataStream?

前提是你 提交于 2019-12-14 02:33:05
问题 I need to process streamed binary data (QDataStream) of defined structure created by another non-Qt program. I wonder what is the best practice to interpret that data. Let's say the data is structured (struct definition provided) in telegrams as follows and cannot be changed by myself: 4 bytes header | 2 bytes sequence number | 1 byte checksum | 10 bytes data I see the following possibilities to handle the data in a "Telegram" class: The Telegram class has a private QByteArray member variable

Append quint16/unsigned short to QByteArray quickly

廉价感情. 提交于 2019-12-12 16:47:37
问题 In my project I'm working with QByteArrays appending data to them as the program goes. Most of the time, a simple quint8 gets appended just fine using QByteArray::append() . But when a quint16 gets appended, only 1 byte gets appended instead of 2. QByteArray ba = QByteArray::fromHex("010203"); quint number(300);//300 in hex is 012c ba.append(number);//What should be appended instead of just number? //the current incorrect result is ba.toHex() == "0102032c" //the desired result is ba.toHex() =

QT中的char、float、QbyteArray数据间的转换

前提是你 提交于 2019-12-11 10:45:04
在QT开发上位机与单片机通信时,因通信协议包的数据类型有“char”和“float”类型,特此记录。协议包类型如下: typedef struct{ char data1; char data2; char data3; char data4; char dataGroup[5]; float value; char data7; }DataInit; 因为代码中需要计算每个字节的内存地址的数据的累加和,char类型的可以直接进行累加,而float类型的数据值无法直接进行累加,如果进行累加数据将会出错,这时就需要将float类型转化为char型。有以下两种方式: 1、可以重新定义协议包将float类型的数据更改为char 类型数组,例如: typedef struct{ char data1; char data2; char data3; char data4; char dataGroup[5]; char floatData[4]; char data7; }DataInit; 因为float本身在内存中分配就为4个字节,所以可在定义时直接将其定义为内存大小是4个字节的char类型的数组,在应用时可再将float数据转换为char类型并存入char类型数组中,具体操作方式参考第二种方式。 2、原本的数据包类型不变,在应用时定义一个char类型的指针

How to unpack 32bit integer packed in a QByteArray?

梦想的初衷 提交于 2019-12-10 22:14:28
问题 I'm working with serial communication, and I receive 32bit integers in a QByteArray , packed in 4 separate bytes (little-endian). I attempt to unpack the value from the 4 bytes using QByteArray::toLong() but it fails the conversion and returns the wrong number: quint8 packed_bytes[] { 0x12, 0x34, 0x56, 0x78 }; QByteArray packed_array { QByteArray(reinterpret_cast<char*>(packed_bytes), sizeof(packed_bytes)) }; bool isConversionOK; qint64 unpacked_value { packed_array.toLong(&isConversionOK) };

Load QPixmap from QByteArray in Qt?

风格不统一 提交于 2019-12-10 12:59:05
问题 I have a byte array with the contents of an image (in png/bmp or some other format). How can I load it into a QPixmap? 回答1: bool QPixmap::loadFromData ( const QByteArray & data, const char * format = 0, Qt::ImageConversionFlags flags = Qt::AutoColor ) Format here is string literal like "PNG" or something similar QPixmap p; QByteArray pData; // fill array with image if(p.loadFromData(pData,"PNG")) { // do something with pixmap } 回答2: You should use the folowing, where your bytes are in the

Convert from QByteArray to array of double

半腔热情 提交于 2019-12-10 09:19:25
问题 I have an array of double : QVector<double> Y(count); I need to pack it to QByteArray to send via Ethernet. So I did it. It was not too hard: QByteArray line; line.clear(); line.append(QByteArray::fromRawData(reinterpret_cast<const char*>(Y.data()), count*sizeof(double))); I try use this code to unpack the data from QByteArray recv : QVector<double> data((line.size())/sizeof(double)); QByteArray dou(sizeof(double),0x0); for(int i = 0; i<data.count(); i++){ dou = recv.mid(i*sizeof(double)

Storing integer to QByteArray using only 4 bytes

自闭症网瘾萝莉.ら 提交于 2019-12-10 02:28:53
问题 It takes 4 bytes to represent an integer. How can I store an int in a QByteArray so that it only takes 4 bytes? QByteArray::number(..) converts the integer to string thus taking up more than 4 bytes. QByteArray((const char*)&myInteger,sizeof(int)) also doesn't seem to work. 回答1: There are several ways to place an integer into a QByteArray , but the following is usually the cleanest: QByteArray byteArray; QDataStream stream(&byteArray, QIODevice::WriteOnly); stream << myInteger; This has the