Results 1 to 8 of 8

Thread: a smarter QCompleter

  1. #1
    Join Date
    Apr 2007
    Posts
    46
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Unhappy a smarter QCompleter

    Hello,
    Is there another QCompleter-like class that does the same thing as the Index search in Qt Assistant? QCompleter will not match items where the search term is in the middle of the item.
    E.g. Qt Assistant can complete "ActiveX" giving this result:
    - A standard ActiveX and the "simple" ActiveQt widget
    - testcon - An ActiveX Test Container (ActiveQt)
    QCompleter cannot do this.

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: a smarter QCompleter

    You can always have a look at the Qt Assistant sources... However it is not that simple because Qt Assistant does not use anything like a QCompleter but instead builds a custom database and search it in its own way...

    As QCompleter is not designed to be extended you'll have to reimplement it from scratch (or from Qt sources ) to achieve what you want. If you're interested in a simpler example of completion classes you might want to have a look at the QCompletionWidget class I've crafted to handle completion in Edyuk. It relies on custom model and nodes types but should not be too hard to adapt to fit your needs.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: a smarter QCompleter

    Isn't Assistant using a sort filter proxy and not a completer? At least I can't get any "completion" from it With a sort filter proxy matching in the middle of a string is trivial.

  4. #4
    Join Date
    Apr 2007
    Posts
    46
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: a smarter QCompleter

    I tried this, but setFilterRegExp() has no effect.

    Qt Code:
    1. void FindDialog::filterRegExpChanged(const QString &s) {
    2. proxyModel->setFilterRegExp(QRegExp(s, Qt::CaseInsensitive, QRegExp::RegExp));
    3. }
    4.  
    5. FindDialog::FindDialog(QWidget *parent) : QDialog(parent) {
    6. ...
    7. model = new QStringListModel(this);
    8. model->setStringList(names);
    9. proxyModel = new QSortFilterProxyModel(this);
    10. proxyModel->setSourceModel(model);
    11. proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
    12. proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
    13. completer = new QCompleter(proxyModel, this);
    14. completer->setCaseSensitivity(Qt::CaseInsensitive);
    15. completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
    16.  
    17. label = new QLabel(tr("...:"), this);
    18. lineEdit = new QLineEdit(this);
    19. lineEdit->setCompleter(completer);
    20. label->setBuddy(lineEdit);
    21. findButton = new QPushButton(tr("&Find"),this);
    22. findButton->setDefault(true);
    23. connect(findButton, SIGNAL(clicked()), parent->parentWidget()->parentWidget(), SLOT(show_image()));
    24. connect(lineEdit, SIGNAL(textEdited(const QString &)), this, SLOT(filterRegExpChanged(const QString &)));
    25. ...
    26. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: a smarter QCompleter

    Could you provide a minimal compilable example reproducing the problem?

  6. #6
    Join Date
    Apr 2007
    Posts
    46
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: a smarter QCompleter

    Qt Code:
    1. #include <QtGui>
    2.  
    3. bool caseInsensitiveLessThan(const QString &s1, const QString &s2) {
    4. return s1.toLower() < s2.toLower();
    5. }
    6.  
    7. class FindDialog : public QDialog
    8. {
    9. Q_OBJECT
    10. public:
    11. FindDialog(QWidget *parent = 0);
    12. QString get_item() const { return lineEdit->text(); }
    13. void clear() { lineEdit->setText(""); }
    14. private slots:
    15. void filterRegExpChanged(const QString &s) {
    16. proxyModel->setFilterRegExp(QRegExp(s, Qt::CaseInsensitive, QRegExp::RegExp));
    17. }
    18. private:
    19. QSortFilterProxyModel *proxyModel;
    20. QList<QString> names;
    21. QLabel *label;
    22. QLineEdit *lineEdit;
    23. QCompleter *completer;
    24. QPushButton *findButton;
    25. };
    26.  
    27. FindDialog::FindDialog(QWidget *parent) : QDialog(parent) {
    28. for (int i = 0; i < 5; i++)
    29. names << QString::number(i) + QString(" ") +
    30. QString::number(i + 1) + QString(" ") +
    31. QString::number(i + 2);
    32. qSort(names.begin(), names.end(), caseInsensitiveLessThan);
    33.  
    34. model = new QStringListModel(this);
    35. model->setStringList(names);
    36. proxyModel = new QSortFilterProxyModel(this);
    37. proxyModel->setSourceModel(model);
    38. proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
    39. proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
    40. completer = new QCompleter(proxyModel, this);
    41. completer->setCaseSensitivity(Qt::CaseInsensitive);
    42. completer->setModelSorting(QCompleter::CaseInsensitivelySortedModel);
    43.  
    44. label = new QLabel(tr("...:"), this);
    45. lineEdit = new QLineEdit(this);
    46. lineEdit->setCompleter(completer);
    47. label->setBuddy(lineEdit);
    48. findButton = new QPushButton(tr("&Find"),this);
    49. findButton->setDefault(true);
    50. connect(lineEdit, SIGNAL(textEdited(const QString &)), this, SLOT(filterRegExpChanged(const QString &)));
    51.  
    52. QVBoxLayout *mainLayout = new QVBoxLayout(this);
    53. mainLayout->addWidget(label);
    54. mainLayout->addWidget(lineEdit);
    55. mainLayout->addWidget(findButton, 0, Qt::AlignRight);
    56. setLayout(mainLayout);
    57.  
    58. setWindowTitle(tr("Find"));
    59. }
    60.  
    61. #include "main.moc"
    62.  
    63. int main(int argc, char **argv){
    64. QApplication app(argc, argv);
    65. FindDialog *dialog = new FindDialog(0);
    66. dialog->show();
    67. return app.exec();
    68. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: a smarter QCompleter

    With this approach you shouldn't use a completer anymore, proxy does all the work for you. You just need to find a way to display the list of available items (either through a fake listview like QCompleter does or for example through an editable combobox).

  8. #8
    Join Date
    Feb 2011
    Posts
    55
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: a smarter QCompleter

    where can i have the sources of Edyuk to look at that class?

Similar Threads

  1. QCompleter + QSqlTableModel problem
    By Lykurg in forum Qt Programming
    Replies: 2
    Last Post: 11th March 2007, 20:59
  2. QCompleter with QTextEdit
    By ePharaoh in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2006, 13:03

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.