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. Recognize ~ in UNIX , with QCompleter
Qt 6.11 is out! See what's new in the release blog

Recognize ~ in UNIX , with QCompleter

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.6k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    It seems that QFileSystemModel doesn't recognize the starting "~" sign , and treat that as home folder , so i tried to replace it while using QLineEdit:

    @
    // ui->uri is a QLineEdit
    connect (ui->uri , SIGNAL(textEdited(QString)) , SLOT(slotReplaceHomeSymbol(QString)));
    @

    @
    void Window::slotReplaceHomeSymbol (QString text)
    {
    ui->uri->setText(text.replace(QRegExp("^~"), QDesktopServices::storageLocation(QDesktopServices::HomeLocation)));
    }
    @

    After connecting that signal , completer never shows up again.

    [Edit] code tags introduced. Please use "code wrapping":http://developer.qt.nokia.com/wiki/ForumHelp#e3f82045ad0f480d3fb9e0ac2d58fb01

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      The completer shows up with a slight delay. Since you are setting the text right after it is edited, that probably interferes with the completer showing.

      Instead, I would look at making a custom completer using the splitPath methods or something like that. Basically, instead of editing the text in the line edit, you would tell the completer how to interpret it.

      See http://developer.qt.nokia.com/doc/qt-4.8/qcompleter.html#splitPath

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        @class UnixFsCompleter : public QCompleter
        {
        Q_OBJECT
        public:
        UnixFsCompleter (QObject *parent):
        QCompleter (parent)
        {}

        QStringList splitPath(const QString &path) const
        {
            if ( path.startsWith("~/") )
            {
                return QCompleter::splitPath(
                            QDesktopServices::storageLocation(QDesktopServices::HomeLocation) +
                            QDir::separator() +
                            path.right( path.length() - 2 )
                            );
            }
        
            return QCompleter::splitPath(path);
        }
        

        };@

        It's not working , am i wrong here ?

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          bq. The [[Doc:QCompleter]] class provides completions based on an item model.

          I doubt hat this class is what you need. I recommend looking at [[Doc:QValidator]], and especially its fixup() method.

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            @class UnixFsValidator : public QValidator
            {
            public:
            UnixFsValidator(QObject *parent): QValidator (parent)
            {}

            virtual void fixup  (QString & path) const
            {
                path.replace(QRegExp ("^~") , QDesktopServices::storageLocation(QDesktopServices::HomeLocation));
                qDebug() << "Replaced to: " << path;
            }
            
            virtual State validate (QString &, int &) const
            {
                return QValidator::Acceptable;
            }
            

            };
            @

            So I'm wrong again ? This time i even see no output.

            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