-
Reference to the file: #include
-
instantiated object:
QFileDialog filedlg(nullptr,//parent window "open image file", //title "C:",//default directory "ImageFile(*.jpg *.png)");//File type filtering //show if(filedlg.exec()==QDialog::Accepted){ }
display effect
File dialog example source code:
QFileDialog fileDlg(this, "FileDialog", "c://", "All File(*.*);;" "Video File(*.mp4 *.avi *.mkv *.flv);;" "Doc File(*.pdf *.doc *.docx *.xls *.xlsx)"); fileDlg.setWindowTitle(QString::fromLocal8Bit("Please select a file")); fileDlg.exec(); QStringList selectFiles = fileDlg.selectedFiles(); QMessageBox::information(this, QString::fromLocal8Bit("Select hint"), QString::fromLocal8Bit("File name:" + selectFiles[0].toLocal8Bit())); QFileInfo fileinfo(selectFiles[0]); QString strInfo = QString::fromLocal8Bit("File name: %1 Absolute file path: %2 File extension: %3 File size: %4").arg(fileinfo.fileName()) .arg(fileinfo.absolutePath()) .arg(fileinfo.suffix()) .arg(fileinfo. size()); QMessageBox::warning(this, QString::fromLocal8Bit("File Properties"), strInfo);
Open single file and multi-letter file example:
QString selectFileName = QFileDialog::getOpenFileName(this, QString::fromLocal8Bit("Open a single file"), "C://", "All File(*.*);;" "Video File(*.mp4 *.avi *.mkv *.flv);;" "Doc File(*.pdf *.doc *.docx *.xls *.xlsx)"); QMessageBox::information(this, QString::fromLocal8Bit( "filename"), selectFileName); QStringList selectFileNames = QFileDialog::getOpenFileNames(this, QString::fromLocal8Bit("Open multiple files"), "C://", "All File(*.*);;" "Video File(*.mp4 *.avi *.mkv *.flv);;" "Doc File(*.pdf *.doc *.docx *.xls *.xlsx)"); for (QString v : selectFileNames ) { QMessageBox::question(this, QString::fromLocal8Bit( "filename"), v); }
Save file example:
QString saveFileName = QFileDialog::getSaveFileName(this, QString::fromLocal8Bit("Save a single file"), "C://", "All File(*.*);;" "Video File(*.mp4 *.avi *.mkv *.flv);;" "Doc File(*.pdf *.doc *.docx *.xls *.xlsx)"); QMessageBox::information(this, QString::fromLocal8Bit( "filename"), saveFileName);
Custom dialog:
QFileDialog dlg(this,NULL,"C://","All File(*.*)"); //Enable multiple file selection mode dlg.setFileMode(QFileDialog::ExistingFiles); int r = dlg.exec(); if(QDialog::Accepted==r){ QMessageBox::information(this,"info",QString::fromLocal8Bit("Select the open button")); // Loop through the output file names for(QString v:dlg. selectedFiles()){ QMessageBox::information(this,"tip",v); } } if(QDialog::Rejected==r){ QMessageBox::information(this,"info",QString::fromLocal8Bit("Select the cancel button")); } / / Construct the save file dialog box by setting the mode QFileDialog saveDlg(this,NULL,NULL,"Text File(*.txt)"); saveDlg.setAcceptMode(QFileDialog::AcceptSave);//Set to save mode, the default is open mode saveDlg.setDirectory("C:");//Set the default directory saveDlg.exec();
Font dialog:
Font dialog source code:
QFontDialog fontDlg; fontDlg.setWindowTitle(QString::fromLocal8Bit("Please select a font")); // specify default font settings QFont defFont(QString::fromLocal8Bit("Microsoft Yahei"),72,QFont::Bold); fontDlg.setCurrentFont(defFont); if(QFileDialog::Accepted==fontDlg.exec()){ QFont fnt = fontDlg.selectedFont(); QString strFont=QString::fromLocal8Bit("family:%1 pointSize:%2 styleName:%3") .arg(fnt.family()).arg(fnt.pointSize()).arg(fnt.styleName()); QMessageBox::information(this, NULL, strFont); //Use the specified font through getFont bool isOk=false; QFont selFont = QFontDialog::getFont( &isOk,fnt,this); if(isOk){ QString strFont=QString::fromLocal8Bit("family:%1 pointSize:%2 styleName:%3") .arg(fnt.family()).arg(fnt.pointSize()).arg(fnt.styleName()); QMessageBox::information(this, NULL, strFont); } }
Color dialog:
Color dialog source code:
QColorDialog colorDlg; //set the default color QColor c("#ff156f");//255,21,111,255 for rgba colorDlg.setCurrentColor(c); QPoint p = pos();//window in screen coordinates colorDlg.setWindowTitle(QString::fromLocal8Bit("Please select a color - [x coordinate: %1, y coordinate: %2]").arg(p.x()).arg(p.y())); int r = colorDlg.exec(); if(QColorDialog::Accepted==r){ QColor color = colorDlg. selectedColor(); int qr = qRed(color.rgb()); int qg = qGreen(color.rgb()); int qb = qBlue(color.rgb()); QString strColor=QString::fromLocal8Bit("R:%1 , G:%2 , B:%3 , HUE:%4 , SAT:%5 , VAL:%6 , HEX:#%7%8%9") .arg(color.red()).arg(color.green()).arg(color.blue()) .arg(color.hue()).arg(color.saturation()).arg(color.value()) .arg(qr==0?"00":QString::number(qr,16)) .arg(qg==0?"00":QString::number(qg,16)) .arg(qb==0?"00":QString::number(qb,16)); QMessageBox::information(this, NULL, strColor); QColor newColor = QColorDialog::getColor(color,this,"color test", QColorDialog::ShowAlphaChannel);//Display alpha channel }