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. Update the QCompleter when calling QLineEdit::setText
Forum Updated to NodeBB v4.3 + New Features

Update the QCompleter when calling QLineEdit::setText

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 920 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.
  • K Offline
    K Offline
    Kerndog73
    wrote on last edited by Kerndog73
    #1

    I'm using a QLineEdit to let the user choose a directory (there's also a button to show a file dialog). I'm initializing a QCompleter like this:

    dirBox = new QLineEdit{this};
    auto *completer = new QCompleter{dirBox};
    auto *model = new QFileSystemModel{completer};
    model->setRootPath(QDir::rootPath());
    completer->setModel(model);
    dirBox->setCompleter(completer);
    dirBox->setText(QDir::homePath());
    

    The problem I'm having is that I want to initialize the QLineEdit with QDir::homePath() but have the QFileSystemModel watching QDir::rootPath(). Calling setText doesn't "update the completer". So if the user appends /De to their home directory, the completer doesn't show them /Desktop. The user must delete the contents of the QLineEdit and start again from the root if they want auto completion.

    I could simply do this:

    model->setRootPath(QDir::homePath());
    

    ...but then the model is no longer watching the root. If there are any changes outside of the home directory, the model won't know about them.

    It seems like I can't have my cake and eat it too!


    As an aside, is there a way to make the completer only show directories and not files?

    jsulmJ 1 Reply Last reply
    0
    • K Kerndog73

      I'm using a QLineEdit to let the user choose a directory (there's also a button to show a file dialog). I'm initializing a QCompleter like this:

      dirBox = new QLineEdit{this};
      auto *completer = new QCompleter{dirBox};
      auto *model = new QFileSystemModel{completer};
      model->setRootPath(QDir::rootPath());
      completer->setModel(model);
      dirBox->setCompleter(completer);
      dirBox->setText(QDir::homePath());
      

      The problem I'm having is that I want to initialize the QLineEdit with QDir::homePath() but have the QFileSystemModel watching QDir::rootPath(). Calling setText doesn't "update the completer". So if the user appends /De to their home directory, the completer doesn't show them /Desktop. The user must delete the contents of the QLineEdit and start again from the root if they want auto completion.

      I could simply do this:

      model->setRootPath(QDir::homePath());
      

      ...but then the model is no longer watching the root. If there are any changes outside of the home directory, the model won't know about them.

      It seems like I can't have my cake and eat it too!


      As an aside, is there a way to make the completer only show directories and not files?

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

      @Kerndog73 said in Update the QCompleter when calling QLineEdit::setText:

      As an aside, is there a way to make the completer only show directories and not files?

      https://doc.qt.io/qt-5/qfilesystemmodel.html#setFilter

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

      K 1 Reply Last reply
      3
      • jsulmJ jsulm

        @Kerndog73 said in Update the QCompleter when calling QLineEdit::setText:

        As an aside, is there a way to make the completer only show directories and not files?

        https://doc.qt.io/qt-5/qfilesystemmodel.html#setFilter

        K Offline
        K Offline
        Kerndog73
        wrote on last edited by
        #3

        @jsulm Excellent! Do you have any ideas for the main problem?

        jsulmJ 1 Reply Last reply
        0
        • K Kerndog73

          @jsulm Excellent! Do you have any ideas for the main problem?

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

          @Kerndog73 said in Update the QCompleter when calling QLineEdit::setText:

          Do you have any ideas for the main problem?

          No. I would actually expect this to work as home path is under root.

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

          1 Reply Last reply
          0
          • K Offline
            K Offline
            Kerndog73
            wrote on last edited by
            #5

            For anyone who's curious, I ended up subclassing QLineEdit and implementing the completion logic myself. The problem discussed in this topic was not the only problem I had with QCompleter. I ended up with a much nicer experience for the user afterwards. It might not be as efficient (I'm using QDir::entryList) but it's not slow enough for the user to notice.

            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