Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) QFileDialogExample1

 

QtQt CreatorLubuntu

 

QFileDialog example 1: crafting a QFileDialog to select a file is a QFileDialog example.

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: ./CppQFileDialogExample1/CppQFileDialogExample1.pro

 

exists (../../DesktopApplication.pri) {
  include(../../DesktopApplication.pri)
}
!exists (../../DesktopApplication.pri) {
  QT += core
  QT += gui
  greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  CONFIG   += console
  CONFIG   -= app_bundle
  TEMPLATE = app
  CONFIG(release, debug|release) {
    DEFINES += NDEBUG NTRACE_BILDERBIKKEL
  }
  QMAKE_CXXFLAGS += -std=c++11 -Wall -Wextra -Weffc++
  unix {
    QMAKE_CXXFLAGS += -Werror
  }
}

exists(../../Libraries/Boost.pri) {
  include(../../Libraries/Boost.pri)
}
!exists(../../Libraries/Boost.pri) {
  win32 {
    INCLUDEPATH += \
      ../../Libraries/boost_1_55_0
  }
}

SOURCES += main.cpp

 

 

 

 

 

./CppQFileDialogExample1/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 };
  d.setViewMode(QFileDialog::Detail);

  //enum FileMode { AnyFile, ExistingFile, Directory, ExistingFiles, DirectoryOnly };
  d.setFileMode(QFileDialog::ExistingFiles);

  //enum AcceptMode { AcceptOpen, AcceptSave };
  d.setAcceptDrops(QFileDialog::AcceptOpen);

  //enum Option { ShowDirsOnly, DontResolveSymlinks, DontConfirmOverwrite, DontUseSheet, DontUseNativeDialog, ReadOnly, HideNameFilterDetails }
  d.setOptions(QFileDialog::ReadOnly);

  d.exec();

  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';
  }
  else
  {
    assert(d.result() == QDialog::Rejected);
    std::cout << "Dialog closed with cancel or close\n";
  }
}

/* Example output:

Number of files selected: 3
/home/richel/ProjectRichelBilderbeek/Test/build-CppQFileDialogExample1-Qt_5_0_1_qt5-Debug/CppQFileDialogExample1
/home/richel/ProjectRichelBilderbeek/Test/build-CppQFileDialogExample1-Qt_5_0_1_qt5-Debug/main.o
/home/richel/ProjectRichelBilderbeek/Test/build-CppQFileDialogExample1-Qt_5_0_1_qt5-Debug/Makefile

*/

 

 

 

 

 

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