Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.






(C++) QFileDialogExample4


QtQt CreatorLubuntu


QFileDialog example 4: crafting a QFileDialog to save a file is a QFileDialog example.


Technical facts


Application type(s)

Operating system(s) or programming environment(s)


Project type:

C++ standard:


Libraries used:






Qt project file: ./CppQFileDialogExample4/CppQFileDialogExample4.pro


QT       += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
QMAKE_CXXFLAGS += -std=c++11
SOURCES += main.cpp








#include <cassert>
#include <iostream>

#include <QApplication>
#include <QFileDialog>

int main(int argc, char *argv[])
  QApplication a(argc, argv);

  QFileDialog d;

  //enum ViewMode { Detail, List };

  //enum FileMode { AnyFile, ExistingFile, Directory, ExistingFiles, DirectoryOnly };

  //enum AcceptMode { AcceptOpen, AcceptSave };

  //enum Option { ShowDirsOnly, DontResolveSymlinks, DontConfirmOverwrite, DontUseSheet, DontUseNativeDialog, ReadOnly, HideNameFilterDetails }
  //Without QFileDialog::DontUseNativeDialog there will not be an edit box to supply
  //a filename, under Lubuntu 13.04 (raring)

  d.setWindowTitle("Save"); //Otherwise it states 'Open'


  if (d.result() == QDialog::Accepted)
    std::cout << "Number of files selected: " << d.selectedFiles().size() << '\n';
    const auto v = d.selectedFiles();
    for (auto s: v) std::cout << s.toStdString() << '\n';
    assert(d.result() == QDialog::Rejected);
    std::cout << "Dialog closed with cancel or close\n";

/* Example output:

Number of files selected: 1







Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.


Valid XHTML 1.0 Strict

This page has been created by the tool CodeToHtml