Results 1 to 8 of 8

Thread: Adding items to QComboBox

  1. #1
    Join Date
    Jan 2020
    Posts
    37
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Adding items to QComboBox

    I have an editable QComboBox *m_combo_name. I modify/correct the text as the user type
    QString mod_name = ....
    and write the modifed text back, thus:
    Qt Code:
    1. m_combo_name->lineEdit()->setText(mod_name);
    To copy to clipboard, switch view to plain text mode 
    That works fine, but when I try to add more items, from a list, QList<QString> optional_names, thus
    Qt Code:
    1. for (int i=0; i<optional_names->size(); i++){
    2. m_combo_name->insertItem(i+1, optional_names.at(i));
    3. }
    To copy to clipboard, switch view to plain text mode 
    the text in the combobox' LineEdit gets overwritten (I want to preserve the text in the LineEdit). I thought I could avoid that by using the index i+1, but it makes no difference. What am I doing wrong?

    Another question: is it possible to open the drop-list, from code, when new items are added? In the same manner as Wikipedia.

  2. #2
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    454
    Thanks
    10
    Thanked 60 Times in 58 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Adding items to QComboBox

    Hi, insertItem() will append the new item to the list of items, but if you only modify the internal QLineEdit before, there are no items. Therefore the first new item replaces your text.
    How do you "modify/correct the text" the user types? Maybe using QValidator/QCompleter may help you.

    Ginsengelf

  3. #3
    Join Date
    Jan 2020
    Posts
    37
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Adding items to QComboBox

    Quote Originally Posted by Ginsengelf View Post
    Hi, insertItem() will append the new item to the list of items, but if you only modify the internal QLineEdit before, there are no items. Therefore the first new item replaces your text.
    How do you "modify/correct the text" the user types? Maybe using QValidator/QCompleter may help you.

    Ginsengelf
    That clarified things. I fixed it by inserting the edited text at the head of the list and then inserting all items (strings) to the QComboBox. That's probably the way it is intended to be done.

    I still haven't figured out how to open the "drop down" from code, to show the options added. I can open it by calling showPopup(), but then it isn't possible to continue writing. Is there a way to do that?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,714
    Thanks
    259
    Thanked 759 Times in 749 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Adding items to QComboBox

    I can open it by calling showPopup()
    showPopup() is probably a nonmodal dialog-type method; that is, the popup widget contains its own event loop to monitor user selections and while it is visible it doesn't respond to changes in the combo box contents. If you want to change this behavior, you may be able to derive from QComboBox and override showPopup() . I don't think it will be very easy to do that and maintain the same behavior that the combobox popup normally has. Its implementation probably depends on there being a fixed number of items in the list, so if you are modifying the list while the user is trying to make a selection, then the indexing has to be carefully managed to avoid returning the wrong index values when a selection is made.
    <=== 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
    Jan 2020
    Posts
    37
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Adding items to QComboBox

    It is strange that QComboBox doesn't implement this behaviour, as it would be very useful. Perhaps it will be included in Qt 6?

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,714
    Thanks
    259
    Thanked 759 Times in 749 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Adding items to QComboBox

    It is strange that QComboBox doesn't implement this behaviour
    I think the behaviour you propose is strange.

    The purpose of the popup is to allow the user to make a selection from the items in the combobox's list, not to display the list of items in a dynamic way. How is the user supposed to make a choice when the list she is seeing is constantly changing underneath her mouse? This would be even worse if the combobox had sorted contents - the contents would keep rearranging themselves, making it impossible to choose the one you want. More often than not, you would probably end up with the wrong one because the list changed underneath you and you didn't notice.

    And think how frustrating it would be if each time you opened the popup box, you saw a different list of items. It would be one thing if -you- did something which caused the list to change (like choose a different search term) versus the list changing because the program did it without any input from you.
    <=== 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.

  7. #7
    Join Date
    Jan 2020
    Posts
    37
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: Adding items to QComboBox

    Maybe I did not explain properly what I meant. What I am after is behaviour somewhat similar to that of Wikipedia, where the list opens up as soon as one starts to type text (the lineedit under the cursor do not change) and then the selection in the drop down list is dynamically altered as one types. What's in the list will of cause have be inserted from code. That saves the user from having to manually open the drop-list.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,714
    Thanks
    259
    Thanked 759 Times in 749 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Adding items to QComboBox

    Ah, then what you want is not a plain QComboBox, but a QComboBox or QLineEdit containing a QCompleter in PopupCompletion mode.
    <=== 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.

Similar Threads

  1. QComboBox with a tree model - adding items problem.
    By high_flyer in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2017, 11:33
  2. QComboBox extremely slow on adding a lot of items
    By Buldozer in forum Qt Programming
    Replies: 2
    Last Post: 24th September 2013, 11:54
  3. Replies: 1
    Last Post: 20th June 2013, 07:24
  4. QcomboBox items
    By therealjag in forum Newbie
    Replies: 5
    Last Post: 27th March 2006, 09:21
  5. [QT3] QComboBox: Disable adding items on Enter-keypress
    By BrainB0ne in forum Qt Programming
    Replies: 7
    Last Post: 14th January 2006, 20:43

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.