Results 1 to 15 of 15

Thread: QComboBox autocomplete

  1. #1
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QComboBox autocomplete

    Dear All

    We have problem for the combo box

    We loead the combo like

    Qt Code:
    1. QComboBox *cmbBox = Concept::ui.cmbItems;
    2. _model->setQuery("select countryname, id from country");
    3. cmbBox->clear();
    4. cmbBox->setModel(_model);
    To copy to clipboard, switch view to plain text mode 

    The combo is not editable.

    The problem is when the users is writing on the combo, if he is not fast enough the word order change

    like

    e 1 sec n 1 sec g - it will go to words eng....

    but if you print like

    e 1 sec n 4 sec g - it will go to words g........

    And the combo cant be editable?
    Can any body help?

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QComboBox autocomplete

    Try to create your own completer by sublclassing QCompleter and setting it for your Combobox.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QComboBox autocomplete

    QSqlQueryModel can not be editable
    from docs
    The QSqlQueryModel class provides a read-only data model for SQL result sets.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  4. #4
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox autocomplete

    Dear Sprit

    Problem is, I dont want that to be editable. And yes you are correct it is not editable.

    But if I change the combo editable, if user change the curser with tab, it doesnt get the id for the country

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QComboBox autocomplete

    Subclass QComboBox and filter key press event. Save the current "search string" in a member variable and perform the search in your QSqlQueryModel or jump to the matching item. Use QTimer to terminate the elapsed time while last key press and clear your internal string if necessary.

  6. #6
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox autocomplete

    was little bit complex?

  7. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QComboBox autocomplete

    Something like that:
    Qt Code:
    1. // QTime m_time;
    2. // QString m_search;
    3. void MyComboBox::keyPressEvent(QKeyEvent *event)
    4. {
    5. if (event->key() == Qt::Key_Up || event->key() == Qt::Key_Down /* || ... */)
    6. QComboBox::keyPressEvent(event);
    7. else
    8. {
    9. if (m_time.elapsed() > 10)
    10. m_search.clear();
    11. m_search.append(event->text());
    12. m_time.restart();
    13. performSearch(); // handles the selection chance of the box according to m_search via setting the querymodel or use QComboBox::findText
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QComboBox autocomplete

    Quote Originally Posted by Lykurg View Post
    Something like that:
    I would made a little bit simpler
    Qt Code:
    1. void MyComboBox::keyPressEvent(QKeyEvent *event)
    2. {
    3. const QString text = event->text();
    4. if (text.isEmpty())
    5. QComboBox::keyPressEvent(event);
    6. else
    7. {
    8. if (m_time.elapsed() > 10)
    9. m_search.clear();
    10. m_search.append(text);
    11. m_time.restart();
    12. performSearch(); // handles the selection chance of the box according to m_search via setting the querymodel or use QComboBox::findText
    13. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  9. #9
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox autocomplete

    is this possible if I promote this from designer?

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QComboBox autocomplete

    Of course. Just use a normal QComboBox and promote it to MyComboBox. If you want a working combo box in your designer preview you have to write a designer plugin with MyComboBox.

  11. #11
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox autocomplete

    Dear all

    I think this will not solve my problem. Because,

    My combo cant be editable.

    My main problem is, I got combo box in a widget or in a delegate, and in the combo boxes I have lots of rows. And every row get an ID from sql when I load the combo.

    Like 'country name', and ID, I save these ID to the database. Problem is If I leave the combo not editable, it take to much time to find it in the combo, because there is no write delay, I mean you have to write really fast to do that.

    If I make it editable, if users passes the combo without pressing return, the ID doesnt change but the text seems to be changed, so the users cant write it on the database

    This problem is my main problem in this project!

  12. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QComboBox autocomplete

    Is setEditable(false) blocking the keyPressEvent??? Don't get your problem. If you think finding a item in the box takes to long time (blocks your GUI) use a thread for it. But really a combo box can't be too huge.

  13. #13
    Join Date
    Jan 2007
    Posts
    201
    Thanks
    22
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QComboBox autocomplete

    let me tell you

    like in combo I have 2000 items

    let me tell you country name and ids,

    I make the combo editable, in that case I could write and find in combo, but in that case if the users passes the combo without pressing return key, the id doesnt change, it stays the old id

  14. #14
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QComboBox autocomplete

    Quote Originally Posted by aekilic View Post
    let me tell you

    like in combo I have 2000 items
    A normal CPU says: haha, that's no size for me... So finding items won't take long l'time
    I make the combo editable, in that case I could write and find in combo,
    You don't need to set the box ediatable. Then user can navigate to the item also by typing. To avoid too short "input cleaning time" use the solution mentioned above.
    but in that case if the users passes the combo without pressing return key, the id doesnt change, it stays the old id
    An editable combo box doesn't intend to provide a solution to your problem. Your need can better be fitted by an QLineEdit with an installed QCompleter holding the countries. Or a QlineEdit as search fiel over an QListView etc...

    but in that case if the users passes the combo without pressing return key, the id doesnt change, it stays the old id
    Then check if the user has edited the values and call press yourself before using the value.

  15. #15
    Join Date
    May 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Wink Re: QComboBox autocomplete

    You might also want to have a look at:
    http://doc.trolltech.com/4.5/qapplic...tInterval-prop

Similar Threads

  1. QDataWidgetMapper and QCombobox
    By miraks in forum Qt Programming
    Replies: 4
    Last Post: 6th December 2008, 17:53
  2. QComboBox - Few virtual methods
    By gruszczy in forum Qt Programming
    Replies: 17
    Last Post: 16th July 2008, 16:08
  3. QComboBox drop list button events
    By maird in forum Qt Programming
    Replies: 5
    Last Post: 20th October 2007, 19:25
  4. using QComboBox as an ItemView
    By EricTheFruitbat in forum Qt Programming
    Replies: 3
    Last Post: 24th January 2007, 16:14
  5. Automatically add items to a QComboBox (Qt4)
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 19th May 2006, 15:32

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.