Results 1 to 5 of 5

Thread: QCompleter and fuzzy search

  1. #1
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QCompleter and fuzzy search

    Hi,
    After reading this article about fuzzy search I felt empowered to add to default QCompleter mentioned code.
    If it wasn't for QCompleter and it's graphical represenation (i mean list view with suggested completion) I would remove it and use mentioned in link code.
    How can I do it?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QCompleter and fuzzy search

    So, yes, the fuzzy matcher looks very cool.

    Unfortunately, I do not think that fuzzy matching is compatible with the matching modes available with QCompleter. QCompleter uses a small subset of Qt::MatchFlags to tell it how to match a string from your complete list of possibilities (the QAbstractItemModel set as input).

    The actual matching is done by a private class derived from the private base class QCompleterEngine, defined in the private header file qcompleter_p.h. Which derived completion engine class is instantiated depends on which MatchFlags you have specified. The only supported match flags are MatchStartsWith, MatchContains, and MatchEndsWith. None of the other flags are supported. All of the code to create and use the engine is deeply embedded in the implementation of QCompleter. There isn't any handy "setCompletionEngine" method that would let you specify a custom engine for matching the user's typing.

    If QCompleter supported the MatchWildcard flag, then you could trick it into accepting your fuzzy matches as input: set the flag to MatchWildcard and use a QSortFilterProxyModel as the input to the QCompleter. Your QSortFilterProxyModel would use the fuzzy match to filter the entries in the complete model and present that to the QCompleter. With a wildcard to "match anything", then the entire content of the proxy model would be shown as possible completions.

    So if you wanted to modify the Qt source code for QCompleter, you could probably implement support for Qt::MatchWildcard. This would be in the QUnsortedModelEngine private class (in qcompleter.cpp), in the QUnsortedModelEngine:: buildIndices() method. Move the "case Qt:: MatchWildcard" to above "case Qt:: MatchExactly" and simply implement it as:

    Qt Code:
    1. case Qt::MatchWildcard:
    2. break;
    To copy to clipboard, switch view to plain text mode 

    This would cause every string that was passed in to be accepted as a possible match to the current index in the model.

    And, of course, you would have to rebuild your Qt distribution to include this new code...
    Last edited by d_stranz; 19th December 2016 at 17:07.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCompleter and fuzzy search

    Currently I'm investigating QSortFilterProxyModel::filterAcceptsRow()(). Perhaps it could be answer.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QCompleter and fuzzy search

    Yes, that would be where you implement the fuzzy match. But unfortunately, there is no way to set QCompleter up to "accept anything from the model" mode. It puts an event filter on the widget (line edit, for example) that captures what the user is typing and passes that into its completion engine. So you would have to short-circuit that process so that keystrokes get sent to your fuzzy filter instead. And then you still have to implement wildcard mode inside of QCompleter - I don't see any way to implement this otherwise. The matching modes that it supports aren't enough to handle fuzzy matching.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Feb 2007
    Location
    Wroclaw, Poland
    Posts
    72
    Thanks
    6
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QCompleter and fuzzy search

    Seems that I will have to reproduce QCompleter functionality by my own (internal model, listview below attached edit line, etc).

    It would be nice to have setSearchFunction( function object) or useCustomSearch and provide search functionality in overridden customSearch function. This way user will have more control over what to search.
    On the other hand - this is so unique and rather rare demand that I don't believe it would be implemented.

Similar Threads

  1. Replies: 1
    Last Post: 14th November 2012, 21:00
  2. QCompleter + SQL
    By Wild Pointer in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2012, 10:18
  3. Replies: 51
    Last Post: 26th November 2010, 13:24
  4. QCompleter Help
    By shinegun in forum Qt Programming
    Replies: 0
    Last Post: 2nd September 2010, 13:26
  5. Replies: 1
    Last Post: 12th October 2008, 08:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.