QFileDialog的使用总结

函数原型:

QString QFileDialog::getOpenFileName (
	QWidget * parent = 0,
	const QString & caption = QString(),
	const QString & dir = QString(), 
	const QString & filter = QString(),
​	QString * selectedFilter = 0,
​	Options options = 0 );

使用:

QString selectFilter;
QString setFilter = "ALL Files(*.*);;JPEG Files(*.jpg);;PNG Files(*.png);;Tiff Files(*.tiff)";
QString fileName = QFileDialog::getOpenFileName(this, tr("打开图片文件"), QCoreApplication::applicationDirPath(), setFilter,&selectFilter);
    qDebug() << "fileName: " << fileName << " selectFilter: " << selectFilter;

可通过参数selectFilter获取选择类型

使用QFileDialog::getSaveFileName()函数时,可通过参数获取保存的文件类型,解析类型后进行;逻辑处理

QMap<QString, int> typeMap;
    typeMap.insert("ALL Files(*.*)", 0);
    typeMap.insert("JPEG Files(*.jpg)", 1);
    typeMap.insert("PNG Files(*.png)", 2);
    typeMap.insert("Tiff Files(*.tiff)", 3);
    QString selectFilter;
    QString setFilter = "ALL Files(*.*);;JPEG Files(*.jpg);;PNG Files(*.png);;Tiff Files(*.tiff)";
    QString fileName = QFileDialog::getSaveFileName(this, tr("保存文件"), "",
        setFilter, &selectFilter);
    int type = typeMap[selectFilter];
    switch (type) {
    case 0:
        qDebug() << "all files type---------------------";
        break;
    case 1:
        qDebug() << "jpeg files type---------------------";
        break;
    case 2:
        qDebug() << "png files type---------------------";
        break;
    case 3:
        qDebug() << "tiff files type---------------------";
        break;
    default:
        break;
    }
补充:中文乱码时,使用如下代码:
 #if _MSC_VER >= 1600
		#pragma execution_character_set("utf-8")
	#endif

你可能感兴趣的:(Qt,开发语言)