QPixmap, QByteArray, QString互相转换


  QString filename = QDir::homePath() + "/Desktop/TOF.png";
  QImage* img = new QImage;
  if(!(img->load(filename))) //加载图像
  {
    QMessageBox::information(this, tr("打开图像失败"), tr("打开图像失败!"));
        delete img;
        return;
  }
  //QPixmap->QByteArray
  QPixmap pixmap = QPixmap::fromImage(*img);
  QByteArray byteArray = QByteArray();
  QBuffer buffer(&byteArray);
  buffer.open(QIODevice::WriteOnly);
  pixmap.save(&buffer,"png",0);
  //QByteArray->QString
  QString str2 = QString(byteArray.toBase64());
  printf("%s", str2.toStdString()); 
  //QString->QByteArray
  QByteArray byteArray = QByteArray::fromBase64(str2.toLocal8Bit();
  //QByteArray->QPixmap
  QPixmap pix; 
  pix.loadFromData(byteArray, "png");

 

你可能感兴趣的:(QPixmap, QByteArray, QString互相转换)