Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2660091
  • 博文数量: 416
  • 博客积分: 10220
  • 博客等级: 上将
  • 技术积分: 4193
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-15 09:47
文章分类

全部博文(416)

文章存档

2022年(1)

2021年(1)

2020年(1)

2019年(5)

2018年(7)

2017年(6)

2016年(7)

2015年(11)

2014年(1)

2012年(5)

2011年(7)

2010年(35)

2009年(64)

2008年(48)

2007年(177)

2006年(40)

我的朋友

分类: C/C++

2009-04-02 11:25:38

QByteArray 与 QString 之间的转换
#include
#include
#include
#include
#include

int main(int argc, char *argv[])
{
      QCoreApplication app(argc, argv);
     
      QImage image("test.png");
      QByteArray ba;
      QBuffer buf(&ba);
      image.save(&buf, "BMP");
     
      QByteArray compressed = qCompress(ba, 1); // better just open file with QFile, load data, compress and toHex?
      QByteArray hexed = compressed.toHex();
      // save to a file
      QString str(hexed);
      QFile f("test.hex");
      if (f.exists())
          f.remove();
      if (f.open(QFile::WriteOnly))
      {
          f.write(str.toLatin1()); // holds only 0..f nothing special.
      }
      else
          qDebug("failed to open file \"test.hex\"");
      f.close();
      ////----------
      if (f.open(QFile::ReadOnly))
      {
          QByteArray read = f.readAll();
          f.close();
          QString rStr = QString::fromLatin1(read.data(), read.size());
          if (rStr != str)
              qDebug("Writed and read two different hexed strings.");
          QByteArray readCompressed = QByteArray::fromHex(rStr.toAscii());
          if (readCompressed != compressed)
              qDebug("bytes before hexing and dehexing _is_ different.");
          QByteArray readDecompressed = qUncompress(readCompressed);
          if (readDecompressed != ba)
              qDebug("bytes before and after compressions are different.");
          QImage readImg;
          //QBuffer readBuf(&readDecompressed);
          readImg.loadFromData(readDecompressed);
          if (readImg.isNull())
              qDebug("The image is null. Something failed.");
          readImg.save("test.bmp");
      }
      else
          qDebug("failed to open test.hex file for reading");
      return 0;
}
阅读(3463) | 评论(0) | 转发(0) |
0

上一篇:Qt4 QDataStream(转)

下一篇:SkinTK菜单问题

给主人留下些什么吧!~~