Results 1 to 10 of 10

Thread: Can a QCompleter emit a signal of index of position of its inside list, by clicking?

  1. #1
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Can a QCompleter emit a signal of index of position of its inside list, by clicking?

    Hi,
    Assume there is a lineEdit With a completer that a stringList is added to the completer. While typing in the lineEdite, some words appear that by clicking on one of them, I need the index of that word in the stringList be returned. is there any signal for this?

    for example:
    Qt Code:
    1. QWidget wdg;
    2.  
    3. QStringList wordList;
    4. wordList << "yawalpha" << "omeyawga" << "omicyaron" << "zetaYAW";
    5.  
    6. QLineEdit *lineEdit = new QLineEdit(&wdg);
    7. QCompleter *completer = new QCompleter(wordList,&wdg);
    8.  
    9. completer->setCaseSensitivity(Qt::CaseInsensitive);
    10. completer->setFilterMode(Qt::MatchContains);
    11. lineEdit->setCompleter(completer);//for example when i type "omicyaron" in the lineEdit and click on that in the completer, it emits a signal with value of 2 (index of position of "omicyaron" in the wordList )
    12.  
    13. wdg.show();
    To copy to clipboard, switch view to plain text mode 

    thanks for any help

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,318
    Thanks
    316
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can a QCompleter emit a signal of index of position of its inside list, by clicki

    Do you ever read the Qt docs? Or do you just ask every question here?

    I hope that you could teach yourself find the index of a QString in a QStringList if you are given the string itself instead of posting yet another question. Besides, you already asked that question in a previous post.

  3. #3
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can a QCompleter emit a signal of index of position of its inside list, by clicki

    Quote Originally Posted by d_stranz View Post
    Do you ever read the Qt docs? Or do you just ask every question here?
    I have read that. this signal "void QCompleter::activated(const QString & text)" with a for-loop for searching could be used. But I think an optimal way being to use "void QCompleter::activated(const QModelIndex & index)" signal. After that I read this: http://doc.qt.io/qt-5/qmodelindex.html about QModelIndex. there is no example in this doc.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can a QCompleter emit a signal of index of position of its inside list, by clicki

    QCompleter works on an option list provided by a model. The convenience constructor taking a QStringList builds that model for you, but it still exists. The row numbers in the model, probably a QStringListModel, correspond to the indices of the original list. So, when the completer tells you the QModelIndex of the activated item you know which QStringList entry that was: index.row()

  5. The following user says thank you to ChrisW67 for this useful post:

    Alex22 (23rd January 2016)

  6. #5
    Join Date
    Nov 2015
    Posts
    128
    Thanks
    70
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can a QCompleter emit a signal of index of position of its inside list, by clicki

    Thanks ChrisW67,
    I tried this but it returns 0 always:
    Qt Code:
    1. connect(completer, SIGNAL(activated(QModelIndex)), this, SLOT(myslot(QModelIndex)));
    2.  
    3. void MyClass::slot_searched(QModelIndex index)
    4. {
    5. qDebug() << index.column(); // always returns 0
    6. qDebug() << index.row(); // always returns 0
    7. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can a QCompleter emit a signal of index of position of its inside list, by clicki

    always returns 0
    No it doesn't, but it also does not quite do what I thought either. Run the code below, type 'B', press down-arrow twice to highlight "bison", press Enter and you get something other than row == 0.
    Qt Code:
    1. #include <QApplication>
    2. #include <QLineEdit>
    3. #include <QStringList>
    4. #include <QCompleter>
    5. #include <QDebug>
    6.  
    7. class Test: public QLineEdit {
    8. Q_OBJECT
    9. public:
    10. Test(QWidget *p = 0): QLineEdit(p) {
    11. QStringList const entries = QStringList()
    12. << "aardvark" << "bear" << "beaver" << "bison" << "civet";
    13. QCompleter *completer = new QCompleter(entries, this);
    14. connect(completer, SIGNAL(activated(QModelIndex)), SLOT(activated(QModelIndex)));
    15. setCompleter(completer);
    16. }
    17.  
    18. private slots:
    19. void activated(const QModelIndex& index) {
    20. qDebug() << "Activated:" << index;
    21. }
    22. };
    23.  
    24. int main(int argc, char **argv) {
    25. QApplication app(argc, argv);
    26. Test t;
    27. t.show();
    28. return app.exec();
    29. }
    30. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 

    Unfortunately the row number is in the filtered list not the full model. The completer is using a proxy model internally. Unfortunately that model is undocumented/private or we might be able to use QAbstractProxyModel::mapToSource().

    Here is an option:
    Qt Code:
    1. class MyCompleter: public QCompleter {
    2. Q_OBJECT
    3. public:
    4. MyCompleter(QObject *p = 0): QCompleter(p) {
    5. connect(this, SIGNAL(activated(QModelIndex)), SLOT(generateIndexSignal(QModelIndex)));
    6. }
    7.  
    8. MyCompleter(QAbstractItemModel *model, QObject *p = 0): QCompleter(model, p) {
    9. connect(this, SIGNAL(activated(QModelIndex)), SLOT(generateIndexSignal(QModelIndex)));
    10. }
    11.  
    12. MyCompleter(const QStringList& strings, QObject *p = 0): QCompleter(strings, p) {
    13. connect(this, SIGNAL(activated(QModelIndex)), SLOT(generateIndexSignal(QModelIndex)));
    14. }
    15.  
    16. signals:
    17. void selectedSourceRow(int index);
    18.  
    19. private slots:
    20. void generateIndexSignal(const QModelIndex& index)
    21. {
    22. QAbstractItemModel * const baseModel = model();
    23. QModelIndexList indexList = baseModel->match(
    24. baseModel->index(0, completionColumn(), QModelIndex()),
    25. completionRole(),
    26. index.data(),
    27. 1,
    28. Qt::MatchExactly);
    29. if (!indexList.isEmpty()) {
    30. emit selectedSourceRow(indexList.at(0).row());
    31. }
    32. }
    33. };
    To copy to clipboard, switch view to plain text mode 

  8. The following user says thank you to ChrisW67 for this useful post:

    Alex22 (25th January 2016)

  9. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,318
    Thanks
    316
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can a QCompleter emit a signal of index of position of its inside list, by clicki

    And all that is easier than QStringList::indexOf() when you are passing a QStringList to the completer in the first place?

    Yuzuru-Masuda.jpg

  10. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Can a QCompleter emit a signal of index of position of its inside list, by clicki

    No, but the modified QCompleter is more general than that (need not be a string list, any table model will do) and does not require the receiver to have access to the original data.

    An editable QComboBox may be a better option than either.
    Last edited by ChrisW67; 24th January 2016 at 05:08.

  11. The following user says thank you to ChrisW67 for this useful post:

    Alex22 (25th January 2016)

  12. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Can a QCompleter emit a signal of index of position of its inside list, by clicki

    Quote Originally Posted by ChrisW67 View Post
    Unfortunately the row number is in the filtered list not the full model. The completer is using a proxy model internally. Unfortunately that model is undocumented/private or we might be able to use QAbstractProxyModel::mapToSource().
    Interesting, didn't know that.

    So alternatively one could do something like

    Qt Code:
    1. QModelIndex baseIndex = index;
    2. while (QAbstractProxyModel *proxy = qobject_cast<QAbstractProxyModel*>(baseIndex.model()) {
    3. baseIndex = proxy->mapToSource(baseIndex);
    4. }
    5. int row = baseIndex.row();
    To copy to clipboard, switch view to plain text mode 

    But using a completer subclass is definitely cleaner.

    Cheers,
    _

  13. The following user says thank you to anda_skoa for this useful post:

    Alex22 (25th January 2016)

  14. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,318
    Thanks
    316
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Can a QCompleter emit a signal of index of position of its inside list, by clicki

    No, but the modified QCompleter is more general than that
    Point taken.

    In a bit of circular logic, I suppose you could give a table model-based completer to a line-edit based delegate in a QTableView that uses the same model as the view to emulate what happens when editing a real spreadsheet - as you type a new entry, it shows completed entries based on what has already been entered into the column.
    Last edited by d_stranz; 24th January 2016 at 18:25.

Similar Threads

  1. Seems that my emit doesn't emit the signal
    By roseicollis in forum Newbie
    Replies: 2
    Last Post: 19th January 2015, 16:05
  2. Replies: 1
    Last Post: 12th January 2011, 22:40
  3. Replies: 0
    Last Post: 4th April 2010, 17:42
  4. emit a signal from inside a callback routine
    By franco.amato in forum Qt Programming
    Replies: 20
    Last Post: 21st January 2010, 18:05
  5. eventFilter and clicking inside QSpinBox
    By T4ng10r in forum Qt Programming
    Replies: 2
    Last Post: 6th March 2007, 21:02

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
  •  
Qt is a trademark of The Qt Company.