天道酬勤,学无止境

qbytearray

Best practice: how to interpret/process QDataStream?

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. All data (17 bytes) is read at once from the stream with the readRawData method and stored in there

2022-02-08 17:05:17    分类:问答    c++   qt   serialization   qbytearray

有没有更短的方法来初始化 QByteArray?(Is there a shorter way to initialize a QByteArray?)

问题 在我的程序中,我经常使用串行通信,因此经常使用QByteArray 。 我想知道是否有比以下更短的方法来初始化具有特定字节的QByteArray : 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)); static_cast<char>是必要的,因为否则 C++11 会给出关于缩小的错误,因为 0x7F 到 0xFF 的范围比char可以容纳的范围大——但char是QByteArray构造函数所要求的。 这是正在使用的QByteArray构造函数: QByteArray::QByteArray(const char *data, int size = -1) 回答1 简单有效: QByteArray b =

2022-01-13 19:04:21    分类:技术分享    c++   qt   c++11   qbytearray   qt5.6

Is there a shorter way to initialize a QByteArray?

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_cast<char> is necessary because otherwise C++11 gives an error about narrowing, because the range 0x7F to

2022-01-08 03:39:35    分类:问答    c++   qt   c++11   qbytearray   qt5.6

为什么不`unique_ptr ` 降级为 `QByteArray*`?(Why doesn't `unique_ptr<QByteArray>` degrade to `QByteArray*`?)

问题 我有以下代码: msg_buf_ptr = std::make_unique<QByteArray>(); return QDataStream{msg_buf_ptr, QIODevice::WriteOnly}; 我收到以下错误: no known conversion for argument 1 from ‘std::unique_ptr<QByteArray>’ to ‘QByteArray*’ 但为什么? 我认为当作为参数传递给采用指针的函数时, unique_ptr和shared_ptr自动降级为原始指针。 如果没有,为什么不呢? 如果他们(通常)这样做,为什么在QByteArray的情况下会失败? 我可以明确调用msg_buf_ptr.get() ,但这似乎是不必要的。 回答1 不,这不是特例; 在需要原始指针的上下文中,标准库智能指针不会隐式降级。 正如问题中提到的,从unique_ptr访问底层原始指针的正确方法是使用get() 。 这是一个设计特性,显然是为了帮助避免意外导致多所有权场景,这会导致未定义的行为。

2021-12-11 21:37:13    分类:技术分享    c++   qt   c++11   unique-ptr   qbytearray

无法打印正确解码的 readAllStandardOutput(can't print readAllStandardOutput correctly decoded)

问题 我有这段代码可以将进程的所有输出打印到文本字段中: data = self.m_process.readAllStandardOutput() s = str(data) self.m_ui.b_renderOutput.append(s) 我在输出中得到的是: b'' b'' b'' b'\r\nStarting "C:\\Program Files' b'' b'\\Autodesk\\Maya2018\\bin\\mayabatch.exe"\r\n' b'Initialized VP2.0 renderer {\r\r\n' 我无法解码它并以正确的方式打印它。 我知道来自 readAllStandardOutput 的是 QByteArray 回答1 如果要将 QByteArray 转换为字符串,首先使用 data() 方法将其转换为字节,然后使用 decode() 将其转换为字符串: data = self.m_process.readAllStandardOutput() s = data.data().decode() # <--- self.m_ui.b_renderOutput.append(s) 另一种方法是将 QByteArray 转换为 bytearray,然后使用 decode(): data = self.m_process

2021-12-05 04:43:45    分类:技术分享    python   pyqt   pyqt5   qbytearray

Why doesn't `unique_ptr<QByteArray>` degrade to `QByteArray*`?

I have the following code: msg_buf_ptr = std::make_unique<QByteArray>(); return QDataStream{msg_buf_ptr, QIODevice::WriteOnly}; I am getting the following error: no known conversion for argument 1 from ‘std::unique_ptr<QByteArray>’ to ‘QByteArray*’ But...why? I thought unique_ptr and shared_ptr automatically degrade to raw pointers when passed as arguments to functions taking pointers. If not, why not? If they (usually) do, why does this fail in the case of QByteArray? I could explicitly call msg_buf_ptr.get(), but that seems like it should be unnecessary.

2021-11-27 08:48:55    分类:问答    c++   qt   c++11   unique-ptr   qbytearray

can't print readAllStandardOutput correctly decoded

I have this code to print into a text field all the output from a process: data = self.m_process.readAllStandardOutput() s = str(data) self.m_ui.b_renderOutput.append(s) What I get in the output is this: b'' b'' b'' b'\r\nStarting "C:\\Program Files' b'' b'\\Autodesk\\Maya2018\\bin\\mayabatch.exe"\r\n' b'Initialized VP2.0 renderer {\r\r\n' I'm not able to decode it and print it in the right way. I know that what comes from readAllStandardOutput is a QByteArray

2021-11-18 22:37:10    分类:问答    python   pyqt   pyqt5   qbytearray

使用 QDataStream 将 QImage 转换为 QByteArray(Converting QImage to QByteArray using QDataStream)

问题 我试图将从 ScreenShot 制作的 QImage 转换为 QByteArray 以通过 QTCPSocket 发送。 当我将 QImage 转换为 QByteArray 并在发送它之前,我尝试反序列化并将其显示在它不能的标签上! 我的错误是什么? 谢谢你的帮助。 QByteArray ImClientShooter::toQByteArray(QImage &img) { QByteArray temp; QDataStream data(&temp, QIODevice::ReadWrite); data « img; return temp; } QByteArray goOn{toQByteArray(sampleQImage)}; //sampleQImage is a QImage Object lbl->setPixmap(QPixmap::fromImage( (QImage::fromData(goOn)))); // QLabel* lbl 从 ScreenShot 制作的 sampleQImage: QGuiApplication::primaryScreen()->grabWindow(0).toImage(); 回答1 任务通过 :D 发件人: QImage img; QByteArray *ba; bool flag{1}

2021-10-27 00:05:08    分类:技术分享    c++   qt   qimage   qbytearray   qdatastream

Converting QImage to QByteArray using QDataStream

Im trying to convert a QImage that maked from ScreenShot to a QByteArray for sending via QTCPSocket. when i convert QImage to QByteArray and before sending it i try to deserialize and show it on label it cant ! what's my mistake? thx for helping. QByteArray ImClientShooter::toQByteArray(QImage &img) { QByteArray temp; QDataStream data(&temp, QIODevice::ReadWrite); data « img; return temp; } QByteArray goOn{toQByteArray(sampleQImage)}; //sampleQImage is a QImage Object lbl->setPixmap(QPixmap::fromImage( (QImage::fromData(goOn)))); // QLabel* lbl sampleQImage maked from ScreenShot

2021-10-25 04:32:51    分类:问答    c++   qt   qimage   qbytearray   qdatastream