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. Completion for QFileSystemModel
Forum Updated to NodeBB v4.3 + New Features

Completion for QFileSystemModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 757 Views
  • 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.
  • D Offline
    D Offline
    Diego Iastrubni
    wrote on last edited by
    #1

    I would like to add a line edit, which can select files from a file system model - searching files by name. In theory - this code should work:

        auto model = new QFileSystemModel(this);
        auto completer = new QCompleter(model, this);
        auto home = QDir::homePath();
        model->setRootPath(home);
        completer->setCompletionColumn(QFileSystemModel::FileNameRole);
        ui->treeView->setModel(model);
        ui->treeView->setRootIndex(model->index(home));
        ui->lineEdit->setCompleter(completer);
        ui->lineEdit->setFocus();
    

    In practice - if I remove the "completion column" I can select a file using pull path. However - I only want completion by filename.

    Other alternative I can use - is extract the whole tree in the dir, create a flat list of files - and manually filter this (overriding most of built in Qt classes). I would like to avoid it.

    JonBJ 1 Reply Last reply
    0
    • D Diego Iastrubni

      I would like to add a line edit, which can select files from a file system model - searching files by name. In theory - this code should work:

          auto model = new QFileSystemModel(this);
          auto completer = new QCompleter(model, this);
          auto home = QDir::homePath();
          model->setRootPath(home);
          completer->setCompletionColumn(QFileSystemModel::FileNameRole);
          ui->treeView->setModel(model);
          ui->treeView->setRootIndex(model->index(home));
          ui->lineEdit->setCompleter(completer);
          ui->lineEdit->setFocus();
      

      In practice - if I remove the "completion column" I can select a file using pull path. However - I only want completion by filename.

      Other alternative I can use - is extract the whole tree in the dir, create a flat list of files - and manually filter this (overriding most of built in Qt classes). I would like to avoid it.

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

      @Diego-Iastrubni

      completer->setCompletionColumn(QFileSystemModel::FileNameRole);

      For a role like you have you're supposed to use setCompletionRole(int role). You have picked a non-existent column number :)

      D 1 Reply Last reply
      2
      • JonBJ JonB

        @Diego-Iastrubni

        completer->setCompletionColumn(QFileSystemModel::FileNameRole);

        For a role like you have you're supposed to use setCompletionRole(int role). You have picked a non-existent column number :)

        D Offline
        D Offline
        Diego Iastrubni
        wrote on last edited by
        #3

        @JonB

        https://github.com/openwebos/qt/blob/92fde5feca3d792dfd775348ca59127204ab4ac0/src/gui/dialogs/qfilesystemmodel.cpp#L706

        I was under impression that this is the role to use :)

        I used also :

        completer->setCompletionColumn(Qt::DisplayRole
        

        And completion still works on full path (not the displayed text).

        JonBJ 1 Reply Last reply
        0
        • D Diego Iastrubni

          @JonB

          https://github.com/openwebos/qt/blob/92fde5feca3d792dfd775348ca59127204ab4ac0/src/gui/dialogs/qfilesystemmodel.cpp#L706

          I was under impression that this is the role to use :)

          I used also :

          completer->setCompletionColumn(Qt::DisplayRole
          

          And completion still works on full path (not the displayed text).

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

          @Diego-Iastrubni
          Please just re-read (carefully) what I wrote, you were using the correct role but calling the wrong method for a role. Roles and columns are quite different things.

          D 1 Reply Last reply
          0
          • JonBJ JonB

            @Diego-Iastrubni
            Please just re-read (carefully) what I wrote, you were using the correct role but calling the wrong method for a role. Roles and columns are quite different things.

            D Offline
            D Offline
            Diego Iastrubni
            wrote on last edited by Diego Iastrubni
            #5

            @JonB

            Got what you said. Next iteration:

            
            completer->setCompletionRole(QFileSystemModel::FileNameRole);
            completer->setCompletionRole(Qt::DisplayRole);
            

            I tried each one of those - and with each one of them, still completion completion works on path, and not display.

            JonBJ 1 Reply Last reply
            0
            • D Diego Iastrubni

              @JonB

              Got what you said. Next iteration:

              
              completer->setCompletionRole(QFileSystemModel::FileNameRole);
              completer->setCompletionRole(Qt::DisplayRole);
              

              I tried each one of those - and with each one of them, still completion completion works on path, and not display.

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

              @Diego-Iastrubni
              I would expect the first one to be correct. I know no more than that.

              You might verify how many columns the model has (I don't know), you are currently searching on column #0, make sure that's right.

              If you derived from QFileSystemModel just so you can override the data() method, you could put debug in there to verify it is being called with FileNameRole, and what that is returning.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Diego Iastrubni
                wrote on last edited by
                #7

                Adding debug to a derived model .. .was not succesfull. It produced too much unusable debug.

                I looked at the code of QCompleter, and saw that it has ... "hack" specific to QFileSystemModel - so my task seems untrivial:

                https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/util/qcompleter.cpp?h=dev#n1790

                I will probable need to derive a new QCompleter... and bypass lots of functionality. Unless - someone tells me I am going completely off - and there is a simpler way.

                JonBJ 1 Reply Last reply
                0
                • D Diego Iastrubni

                  Adding debug to a derived model .. .was not succesfull. It produced too much unusable debug.

                  I looked at the code of QCompleter, and saw that it has ... "hack" specific to QFileSystemModel - so my task seems untrivial:

                  https://code.qt.io/cgit/qt/qtbase.git/tree/src/widgets/util/qcompleter.cpp?h=dev#n1790

                  I will probable need to derive a new QCompleter... and bypass lots of functionality. Unless - someone tells me I am going completely off - and there is a simpler way.

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

                  @Diego-Iastrubni
                  Although I agree that code is naughty to be doing QFileSystemModel-specific in QCompleter, I think it just makes pathFromIndex() return the full path. I don't know why that should be a problem for you.

                  It would explain data() being called with QFileSystemModel::FileNameRole a lot.

                  I still think your original code should work with setCompletionRole(QFileSystemModel::FileNameRole).

                  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