zoukankan      html  css  js  c++  java
  • qt通过QFileDialog获取文件路径&保存文件&选择文件夹

    1、获取文件路径对话框

    file_name = QFileDialog::getOpenFileName(self,"open file dialog","C:UsersAdministratorDesktop","Txt files(*.txt)")  
    ##"open file Dialog "为文件对话框的标题,第三个是打开的默认路径,第四个是文件类型过滤器 

    2、保存文件对话框

    file_path =  QFileDialog::getSaveFileName(self,"save file","C:UsersAdministratorDesktop" ,"xj3dp files (*.xj3dp);;all files(*.*)")
    # file_path即为文件即将保存的绝对路径。形参中的第二个为对话框标题,第三个为打开后的默认给路径,第四个为文件类型过滤器

    保存文件:

    QString fileName = QFileDialog::getSaveFileName(this, tr("Save As"), "", tr("Curve TagName Files (*.TXT)"));
    if (!fileName.isEmpty())
    {
        //一些处理工作,写数据到文件中
        QList<QwtPlotCurve*>& plotCurves = m_plot->getPlotCurves();
        QFile file(fileName);
        if (!file.open(QIODevice::ReadWrite | QIODevice::Text))
            QMessageBox::critical(this, tr("Error"), tr("Failed to open file "%1" for save!").arg(fileName), QMessageBox::Ok);
        //write file
        QTextStream wr(&file);
        for(int i=0; i<plotCurves.count(); ++i)
        {
            wr <<plotCurves[i]->title().text() <<"
    ";
        }
        file.close();
    }
    else
        return;            

    3、选择文件夹对话框

    dir_path=QFileDialog::getExistingDirectory(self,"choose directory","C:UsersAdministratorDesktop") 
    # dir_path即为选择的文件夹的绝对路径,第二形参为对话框标题,第三个为对话框打开后默认的路径。
    以上返回的都是QString类型的对象,若想不出现编码问题,建议用如下语句将QString转换为python的string对象
    str=unicode(your_path.toUtf8(), ‘utf-8‘, ‘ignore‘) 

    根据指定文件夹找对应文件:

    QString fileName(tr("ok.txt")) ;  
    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),"/home", QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);
    
        QDir d;
        d.mkpath(dir);//可以不用,因为路径已经有了,就不用mk了
        QFile file(dir+"/"+fileName);
        file.open(QFile::WriteOnly);

    另存为:

    QString path;   
    path = QDir::currentPath() + "/CscanData/Cscan.bmp";    
    QString filename = QFileDialog::getSaveFileName(this, tr("Save As"), path, tr("Image Files (*.bmp)"));    
    if(!filename.isNull())    
    {        
        //一些处理工作,复制
        QFile::copy(path+ filename);   
    }    
    else    
        return;

    参考链接:https://www.136.la/python/show-13717.html

         https://blog.csdn.net/bzhxuexi/article/details/10044037

  • 相关阅读:
    【Codeforces Round #645 (Div. 2) F】 Tasty Cookie
    【Codeforces Round #645 (Div. 2) E】 Are You Fired?
    【Educational Codeforces Round 88 (Rated for Div. 2) C】 Mixing Water
    20191114-4 Beta发布用户使用报告
    20191114-3 Beta阶段贡献分配
    beta发布
    20191107-1 每周例行报告
    beta 2/2 阶段中间产物提交入口
    beta week 2/2 Scrum立会报告+燃尽图 07
    beta week 2/2 Scrum立会报告+燃尽图 06
  • 原文地址:https://www.cnblogs.com/kongbursi-2292702937/p/15035697.html
Copyright © 2011-2022 走看看