Qt之打开文件、图片、载入文件

1、打开图片格式

    QString fileName = QFileDialog::getOpenFileName(  
                this, tr("open image file"),  
                "./", tr("Image files(*.bmp *.jpg *.pbm *.pgm *.png *.ppm *.xbm *.xpm);;All files (*.*)"));  
  
    if(fileName.isEmpty())  
    {  
        QMessageBox mesg;  
        mesg.warning(this,"警告","打开图片失败!");  
        return;  

    }  


2、打开文件格式:

  QString fileName = QFileDialog::getOpenFileName(
                                                         this,
                                                         ("打开文档文件"),"./",
                                                         tr("文档后缀(*.*)" "::文本格式(*.txt)""::XML格式(*.xml)"));

  displayTextEdit->setText(fileName);   //在textedit中,显示打开的文件名

  loadFile(fileName);


3、载入文件(loadFile)

void Cbuitidig::loadFile(const QString filename){
    QFile file(filename);
    if(!file.open(QFile::ReadOnly | QFile::Text)){
        QMessageBox msg;
        msg.setText("error !");
        msg.show();
    }
    QTextStream in(&file);
    displayTextEdit->setText(in.readAll());
}



猜你喜欢

转载自blog.csdn.net/u014252478/article/details/80406737