Results 1 to 3 of 3

Thread: QCompler in QComboBox strange behaviour

  1. #1
    Join Date
    Nov 2009
    Posts
    4
    Qt products
    Qt4

    Question QCompler in QComboBox strange behaviour

    Hi,

    I instanciate a QComboBox with a completion mode like this
    Qt Code:
    1. combo->setEditable(true);
    2. combo->setInsertPolicy(QComboBox::NoInsert);
    3. combo->completer()->setCompletionMode(QCompleter::PopupCompletion);
    To copy to clipboard, switch view to plain text mode 

    Then I fill the combo with items like this
    Qt Code:
    1. combo->addItem(QIcon("someIcon"), "Choice A", 42);
    2. combo->addItem(QIcon("someIcon"), "Choice A", 4242);
    3. combo->addItem(QIcon("someIcon"), "Choice B", 43);
    4. combo->addItem(QIcon("someIcon"), "Choice B", 4343);
    To copy to clipboard, switch view to plain text mode 

    I have duplicate choice A and B with different QVariant attached to the items.
    at runtime when I use the combo with the full dropdown list, my activated() slot give me currentIndex() like this :

    I select first Choice A -> currentIndex = 0
    I select second Choice A -> currentIndex = 1
    I select first Choice B -> currentIndex = 2
    I select second Choice B -> currentIndex = 3

    the problem is when I do not use the dropdown, but use the completion it give me this :

    completion for first Choice A -> currentIndex = 0
    completion for second Choice A -> currentIndex = 0
    completion for first Choice B -> currentIndex = 2
    completion for second Choice B -> currentIndex = 2

    My guess is that the Completer do a findItem with the text of the completer, and then return me the first item that is named like that in the combobox, but it seems like a problem.
    Does someone know why it does that ? and how can I have the right currentIndex even with completion.
    Thank you

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: QCompler in QComboBox strange behaviour

    Does someone know why it does that ? and how can I have the right currentIndex even with completion.
    This is default behavior of the QCompleter. There is some flexibility in selecting the completion role, but that too will not get you what you want. The problem here is that the item's data in DisplayRole is not unique.

    I think it is not possible as long as the DisplayRole data is not unique
    Last edited by Santosh Reddy; 4th April 2013 at 14:07.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Nov 2009
    Posts
    4
    Qt products
    Qt4

    Default Re: QCompler in QComboBox strange behaviour

    ok, so do you think I can achieve what I want if I implement my own QCompleter and set it to the QCombo ?


    Added after 53 minutes:


    I just had a look at QCombo source and it appears I just can't do this !

    Qt Code:
    1. int QComboBox::findData(const QVariant &data, int role, Qt::MatchFlags flags) const
    2. {
    3. Q_D(const QComboBox);
    4. QModelIndexList result;
    5. QModelIndex start = d->model->index(0, d->modelColumn, d->root);
    6. result = d->model->match(start, role, data, 1, flags);
    7. if (result.isEmpty())
    8. return -1;
    9. return result.first().row();
    10. }
    To copy to clipboard, switch view to plain text mode 

    I f I understand well, the model match have a hits argument setted to 1 hardcoded, so it will always take the first match, and findData return the first result anyway so I just can't do it with ComboBox even with a derivation of QCompleter or QSortFilterProxyModel.
    Last edited by typedef; 4th April 2013 at 15:29.

Similar Threads

  1. strange behaviour with Qt::key_space
    By franco.amato in forum Newbie
    Replies: 8
    Last Post: 13th April 2010, 07:39
  2. strange behaviour with Qt key_space
    By franco.amato in forum Qt Programming
    Replies: 0
    Last Post: 10th April 2010, 00:21
  3. Need help: Strange behaviour
    By navi1084 in forum Qt Programming
    Replies: 3
    Last Post: 14th November 2008, 05:03
  4. QComboBox -> setModel -> Strange behaviour
    By oscar in forum Qt Programming
    Replies: 2
    Last Post: 13th August 2008, 22:27
  5. very strange behaviour
    By regix in forum Qt Programming
    Replies: 23
    Last Post: 20th July 2006, 18:38

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.