现在的位置: 首页 > 综合 > 正文

Qt: QTextStream流与QDataStream流

2013年05月04日 ⁄ 综合 ⁄ 共 2064字 ⁄ 字号 评论关闭

   QStringList files = QFileDialog::getOpenFileNames(this,windowTitle());

   QStringList list=files;

   for (QStringList::iterator it = list.begin();it != list.end(); ++it) {

   QString current = *it;

   ui->textBrowser->append(current); //在此逐个处理用户选择的多个文件

   }

/////////////////////////////////////////////////////////////////////////////////////////////////

void MainWindow::vOpenFile()

{

   QString name = QFileDialog::getOpenFileName(this,windowTitle(),"","*.txt");

   QFile file(name);

   if(!file.open(QIODevice::ReadOnly)){

       QMessageBox::critical(this,windowTitle(),tr("文件打开失败!"));

   }

   QTextStream in(&file);

   QString temp=in.readAll();

   ui->textBrowser->setText(temp);  //显示用户选择的文本文件的内容

}

void MainWindow::vSaveFile()

{

   QString name = QFileDialog::getSaveFileName(this,this->windowTitle(),"","*.txt");

   QFile file(name);

   if(!file.open(QIODevice::WriteOnly)){

       QMessageBox::critical(this,windowTitle(),tr("文件创建失败!"));

   }

   QTextStream out(&file);

   QString eText=ui->textEdit->toHtml();

   out<<eText<<endl;  //将用户在textEdit中编辑的内容保存到硬盘上。

}

/////////////////////////////////////////////////////////////////////////////////////////////////
void MainWindow::vSaveFile()

{

   QFile file("yvhvv.dat");

   if(!file.open(QIODevice::WriteOnly)){

       QMessageBox::critical(this,windowTitle(),tr("文件创建失败!"));

   }

   QDataStream out(&file);

   out.setVersion(QDataStream::Qt_4_7);

   quint32 n=0x19841008;

   QImage image(QFileDialog::getOpenFileName());

   out<<n<<image; //将用户选择的图片以二进制形式存入yvhvv.dat文件中

}

void MainWindow::vOpenFile()

{

   QFile file("yvhvv.dat");

   if(!file.open(QIODevice::ReadOnly)){

       QMessageBox::critical(this,windowTitle(),tr("文件打开失败!"));

   }

   QDataStream in(&file);

   in.setVersion(QDataStream::Qt_4_7);

   quint32 n;

   QImage name;

   in>>n>>name;

   if(n==0x19841008) //如果魔数匹配就显示yvhvv.dat中存储的图片

     ui->label->setPixmap(QPixmap::fromImage(name));

   else

       QMessageBox::critical(this,windowTitle(),tr("未识别格式!"));

}

 

图片

 

32位的魔数紧跟着就是QImage的格式了,我也不清楚接下来的数据是如何布局的,不过不重要,因为它提供有接口助我获取感兴的东西,比如说我想把QImage当做中间容器,把市面上流行的图片格式转换成我自己私人用的图片格式,这需要我专门为QImage类写一遍日记了。

抱歉!评论已关闭.