Go back to Richel Bilderbeek's homepage.

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






(C++) QFileDialogExample3


QtQt CreatorLubuntu


QFileDialog example 3: compare class and convenience function from GUI 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: ./CppQFileDialogExample3/CppQFileDialogExample3.pro


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

HEADERS  += dialog.h

FORMS    += dialog.ui








#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
  class Dialog;

class Dialog : public QDialog
  explicit Dialog(QWidget *parent = 0);
private slots:
  void on_button_1_clicked();

  void on_button_2_clicked();

  Ui::Dialog *ui;

#endif // DIALOG_H








#include "dialog.h"

#include <cassert>
#include <sstream>
#include <QFileDialog>

#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
  ui(new Ui::Dialog)

  delete ui;

void Dialog::on_button_1_clicked()
  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 }


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

void Dialog::on_button_2_clicked()
  const auto v = QFileDialog::getOpenFileNames();

  std::stringstream s;
  s << "Number of files selected: " << v.size() << '\n';
  for (auto f: v) s << f.toStdString() << '\n';








#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
  QApplication a(argc, argv);
  Dialog w;
  return a.exec();






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