c++ - 将 QImage* 转换为 void* ,反之亦然

标签 c++ qt pointers casting qimage

我正在开发 C++ QT 项目,该项目包含一些模块,这些模块使用带有签名的函数通过一个 Controller 模块相互通信:

notify(QString stream_id, const void* stream, unsigned __int64 size)

所以我问的是如何将 QT 数据类型主要是 QImage* 转换为 void*,反之亦然,以使用 void 指针及其大小发送数据,

我正在尝试使用这段代码,但它不起作用:

    void* imageVoidPtr = static_cast<void*>(&image);

StreamListener::getInstance()->notify(QString("Stream_Name"),imageVoidPtr,sizeof(imageVoidPtr));

------------ 编辑

我正在尝试使用以下方法检索数据:

    void VideoView::receive(QString stream_id, const void* stream, unsigned __int64 size){
QByteArray data = QByteArray::fromRawData(reinterpret_cast<const char*>(stream), size);
QBuffer buffer(&data);
QImageReader reader(&buffer);
QImage img = reader.read();}

-------- 编辑 2

Void* 缓冲区的大小为 4 或 12(在 sizeOf(QImage) 的情况下)并且没有给出正确的图像字节大小的代码有什么问题, 并且接收器中接收到的图像是空的(没有出现错误)。

谢谢

最佳答案

令人惊奇的是,您期望它能起作用。

指针只是一个整数,表示可以保存任意数据的内存地址。因此,无论指针指向什么,指针的大小始终相同,因为它始终是内存地址,并且始终由固定位数表示,32 位版本中为 32 位,64 位版本中为 64 位。

QImage 将数据存储在堆上。实际类只是数据的 Controller 。因此,无论图像有多大,sizeof() 都会给您相同的结果。

QImage 已经支持使用 QDataStream 进行序列化和反序列化:

  QImage img(100, 100, QImage::Format_Mono); // original image
  QByteArray data; // data container
  QBuffer buffer(&data); // io device
  buffer.open(QIODevice::ReadWrite);
  QDataStream stream(&buffer); // data stream
  stream << img; // save img to data 
  // you can now use the data.data() pointer, convert to void, and send data.size() number of bytes and put it into another byte array on the other side
  // then replicate the io dev and data stream steps too, omitted for brevity       
  QImage img2; // new image
  buffer.seek(0); // go to beginning of data
  stream >> img2; // read data into new image
  qDebug() << (img == img2) << data.size(); // true, it is the same content, size is 723 bytes, that includes the image format and raw data

关于c++ - 将 QImage* 转换为 void* ,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48862884/

相关文章:

c++ - 多个对话过程来控制? winapi/C++

pointers - 如何存储指针指向的地址?

c - 通过 malloc 故意隐藏内存

c++ - 混合 C/C++ 代码中的结构指针

c++ - 以下带有 OpenCV 代码的 C++ 未被编译

C++ 和压缩结构

unit-testing - Qt 单元测试设置

qt - WSARecv 和 WSABuf 问题

linux - 如何从命令行传递 QMAKE 变量?

c++ - int * [2] 是个什么样的变量?如声明 int* p2[2]