Recognize ~ in UNIX , with QCompleter



  • 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



  • 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



  • @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 ?



  • 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.



  • @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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.