Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QFileDialog oddity on Windows when using setFilter()
Forum Updated to NodeBB v4.3 + New Features

QFileDialog oddity on Windows when using setFilter()

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 465 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • PerdrixP Offline
    PerdrixP Offline
    Perdrix
    wrote on last edited by Perdrix
    #1

    My code below displayed the contents of the directory that match the filter selected:

    QSettings settings;
    
    auto directory{ settings.value("Folders/SavePictureFolder").toString() };
    auto extension{ settings.value("Folders/SavePictureExtension").toString().toLower() };
    auto apply{ settings.value("Folders/SaveApplySetting", false).toBool() };
    TIFFCOMPRESSION compression{ static_cast<TIFFCOMPRESSION>(
    	settings.value("Folders/SaveCompression", (uint)TC_NONE).toUInt()) };
    auto filterIndex{ settings.value("Folders/SavePictureIndex", 0).toUInt() };
    if (filterIndex > 5) filterIndex = 0;
    
    QStringList fileFilters{
    	tr("TIFF Image 16 bit/ch (*.tif)", "IDS_FILTER_OUTPUT"),
    	tr("TIFF Image 32 bit/ch - integer (*.tif)", "IDS_FILTER_OUTPUT"),
    	tr("TIFF Image 32 bit/ch - rational (*.tif)", "IDS_FILTER_OUTPUT"),
    	tr("FITS Image 16 bit/ch (*.fts)", "IDS_FILTER_OUTPUT"),
    	tr("FITS Image 32 bit/ch - integer (*.fts)", "IDS_FILTER_OUTPUT"),
    	tr("FITS Image 32 bit/ch - rational (*.fts)", "IDS_FILTER_OUTPUT")
    };
    
    if (filterIndex > 2) extension = ".fts";
    else extension = ".tif";
    
    //
    // SavePicture is a sub-class of QFileDialog, we'll set the QFileDialog vars first
    //
    SavePicture dlg{ this, tr("Save Image"), directory };
    dlg.setDefaultSuffix(extension);
    //dlg.setFilter(QDir::Files | QDir::Writable);
    dlg.setNameFilters(fileFilters);
    auto filter{ fileFilters.at(filterIndex) };
    dlg.selectNameFilter(filter);
    //
    // selectNameFilter doesn't fire the QFileDialog::filterSelected signal
    // so need to drive the slot ourself
    //
    dlg.onFilter(filter);
    //
    // Now set our sub-class variables
    //
    dlg.setCompression(TIFFCOMPRESSION(compression));
    dlg.setApply(apply);
    if (!selectionRect.isEmpty()) dlg.setUseRect(true);
    
    //
    // display the dialogue
    //
    if (QDialog::Accepted == dlg.exec())
    

    1782465a-0b60-4519-830c-baaee7d87867-image.png

    If I un-comment the dlg.setFilter(QDir::Files | QDir::Writable); line I get:

    1b38a300-c7ae-4984-a4f0-a105da9dcf0e-image.png

    IOW NO files are shown?

    Why not?

    David

    jsulmJ 1 Reply Last reply
    0
    • PerdrixP Perdrix

      My code below displayed the contents of the directory that match the filter selected:

      QSettings settings;
      
      auto directory{ settings.value("Folders/SavePictureFolder").toString() };
      auto extension{ settings.value("Folders/SavePictureExtension").toString().toLower() };
      auto apply{ settings.value("Folders/SaveApplySetting", false).toBool() };
      TIFFCOMPRESSION compression{ static_cast<TIFFCOMPRESSION>(
      	settings.value("Folders/SaveCompression", (uint)TC_NONE).toUInt()) };
      auto filterIndex{ settings.value("Folders/SavePictureIndex", 0).toUInt() };
      if (filterIndex > 5) filterIndex = 0;
      
      QStringList fileFilters{
      	tr("TIFF Image 16 bit/ch (*.tif)", "IDS_FILTER_OUTPUT"),
      	tr("TIFF Image 32 bit/ch - integer (*.tif)", "IDS_FILTER_OUTPUT"),
      	tr("TIFF Image 32 bit/ch - rational (*.tif)", "IDS_FILTER_OUTPUT"),
      	tr("FITS Image 16 bit/ch (*.fts)", "IDS_FILTER_OUTPUT"),
      	tr("FITS Image 32 bit/ch - integer (*.fts)", "IDS_FILTER_OUTPUT"),
      	tr("FITS Image 32 bit/ch - rational (*.fts)", "IDS_FILTER_OUTPUT")
      };
      
      if (filterIndex > 2) extension = ".fts";
      else extension = ".tif";
      
      //
      // SavePicture is a sub-class of QFileDialog, we'll set the QFileDialog vars first
      //
      SavePicture dlg{ this, tr("Save Image"), directory };
      dlg.setDefaultSuffix(extension);
      //dlg.setFilter(QDir::Files | QDir::Writable);
      dlg.setNameFilters(fileFilters);
      auto filter{ fileFilters.at(filterIndex) };
      dlg.selectNameFilter(filter);
      //
      // selectNameFilter doesn't fire the QFileDialog::filterSelected signal
      // so need to drive the slot ourself
      //
      dlg.onFilter(filter);
      //
      // Now set our sub-class variables
      //
      dlg.setCompression(TIFFCOMPRESSION(compression));
      dlg.setApply(apply);
      if (!selectionRect.isEmpty()) dlg.setUseRect(true);
      
      //
      // display the dialogue
      //
      if (QDialog::Accepted == dlg.exec())
      

      1782465a-0b60-4519-830c-baaee7d87867-image.png

      If I un-comment the dlg.setFilter(QDir::Files | QDir::Writable); line I get:

      1b38a300-c7ae-4984-a4f0-a105da9dcf0e-image.png

      IOW NO files are shown?

      Why not?

      David

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Perdrix What if you remove QDir::Writable ?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • PerdrixP Offline
        PerdrixP Offline
        Perdrix
        wrote on last edited by
        #3

        That seems to work as expected ...
        image.png

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You forgot QDir::Readable so all files which are readable were filtered out.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          1
          • PerdrixP Offline
            PerdrixP Offline
            Perdrix
            wrote on last edited by Perdrix
            #5

            Meh - that isn't obvious at all - so what use is QDir::Writable.??? The concept of a write only file seems a bit crazy.

            JonBJ 1 Reply Last reply
            0
            • PerdrixP Perdrix

              Meh - that isn't obvious at all - so what use is QDir::Writable.??? The concept of a write only file seems a bit crazy.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Perdrix
              Passing QDir::Writable (and not QDir::Readable) is a good way to find those "crazy write-only" files :)

              Although I'm not sure I've seen it, might have an application for a log file that a program can write to when run by a user but you don't want/need the user to be able to look at it.

              Docs give an example of:

              For example, setting the Readable, Writable, and Files flags allows all files to be listed for which the application has read access, write access or both.

              So does QDir::Readable on its own (I mean with QDir::Files of course) list only read-only files?

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved