Results 1 to 11 of 11

Thread: QComboBox with custom model clears text in its lineEdit

  1. #1
    Join Date
    Sep 2021
    Posts
    7
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Question QComboBox with custom model clears text in its lineEdit

    I encountered very strange issue when implemented QComboBox with custom model (QAbstractListModel subclass). I noticed that sometimes after getting lineEdit's editingFinished signal the combox's currentText() is empty and lineEdit()->isModified() returns false. After investigating this issue I discovered that this somehow connected to custom subclass model I use. If to comment out setModel installed for the QComboBox, this problem is not observed. Moreover, this problem happens only when editing is completed with Enter pressed. If the editsimply loses focus after editing, all work as expected.
    I use Qt v5.9.8. Do you have any suggestion?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox with custom model clears text in its lineEdit

    You should probably look at your model's setData() implementation. My guess is that when the user edits the combobox, this method is called. You may not be calling the methods needed (beginWhatever / endWhatever) to signal the combobox to update its contents and set the current index.
    Last edited by d_stranz; 20th September 2021 at 18:43.
    <=== 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.

  3. The following user says thank you to d_stranz for this useful post:

    redsoft (23rd September 2021)

  4. #3
    Join Date
    Sep 2021
    Posts
    7
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QComboBox with custom model clears text in its lineEdit

    Hi d_stranz, many thanks for your help. It was the exact issue: I have not implemented setData at all. Adding setData {return true; } solved the issue.
    I am still thinking that there is a design hole in this Qt mechanism. The problem that edit completed using Enter and lost focus has different handlers. And this is strange.


    Added after 21 minutes:


    Sorry, but I need to add new post, that revokes the previous one. setData not helped, simply I forgot to remove my workaround code, which was installed inside textEdited signal.
    The setData function is not called when user press Enter to finalize the editing.
    Last edited by redsoft; 23rd September 2021 at 13:10.

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox with custom model clears text in its lineEdit

    Sorry, but I need to add new post, that revokes the previous one.
    Well, without some minimal example code that reproduces the problem, it is hard to know what is going wrong. In any case, if you are using a model for the combo box, entering something new into the line edit needs to add it to the model so the combo box can update and display the new item as the current one. When a model is used, everything the combo box displays has to come from the model.
    <=== 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.

  6. #5
    Join Date
    Sep 2021
    Posts
    7
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QComboBox with custom model clears text in its lineEdit

    It is not a problem to post here the code snippet. It's very simple. There are 2 classes in the picture: TabWidgetQuickFind (derived from QComboBox) and FindHistoryModel (derived from QAbstractListModel):

    Qt Code:
    1. class TabWidgetQuickFind : public QComboBox
    2. {
    3. Q_OBJECT
    4. friend class TabTable;
    5.  
    6. public:
    7. TabWidgetQuickFind (TabTable* tab);
    8.  
    9. protected slots:
    10. void OnTextEdited (const QString& t) {
    11. // this is the bug workaround code
    12. Edited = true;
    13. EditedTxt = t;
    14. }
    15.  
    16. protected:
    17. TabTable* TabDoc; // Owner doc
    18. bool Edited; // Temp bug workaround: true only in the case QComboBox's QLineEdit started be manually edited, cleared at next signal processed
    19. QString EditedTxt; // Temp bug workaround
    20. };
    21.  
    22. inline TabWidgetQuickFind::TabWidgetQuickFind (TabTable* tab) : TabDoc(tab), Edited(false)
    23. {
    24. setEditable (true);
    25. setMinimumWidth (180);
    26. setMaximumWidth (300);
    27. setModel (&FindHistory::This->Model); // if not to set this model, the problem doesn't happen
    28. (void) connect (this, SIGNAL(currentIndexChanged(int)), TabDoc, SLOT(OnQuickFindIndexChanged(int)));
    29. (void) connect (lineEdit(), SIGNAL(editingFinished()), TabDoc, SLOT(OnQuickFindEditingFinished()));
    30. (void) connect (lineEdit(), SIGNAL(textEdited(const QString&)), this, SLOT(OnTextEdited(const QString&)));
    31. // After installing the custom model for strange reason (Qt behaviour?) the 1st model's row added also to edited text
    32. // and the current index is set to this row number. May be cleared by 2 ways: clearEditText() or setCurrentIndex(-1)
    33. setCurrentIndex(-1);
    34. }
    35.  
    36. class FindHistoryModel : public QAbstractListModel
    37. {
    38. friend class FindHistory;
    39.  
    40. public:
    41. FindHistoryModel (QObject* parent) : QAbstractListModel(parent) {}
    42. int rowCount (const QModelIndex& parent) const override;
    43. QVariant data (const QModelIndex &index, int role) const override;
    44.  
    45. bool setData (const QModelIndex& /*index*/, const QVariant& /*value*/, int /*role*/) override
    46. {
    47. return true; // this code is never called
    48. }
    49. };
    50.  
    51.  
    52. int FindHistoryModel::rowCount (const QModelIndex& /*parent*/) const
    53. {
    54. return (int) FindHistory::This->Count();
    55. }
    56.  
    57.  
    58. QVariant FindHistoryModel::data (const QModelIndex &index, int role) const
    59. {
    60. switch (role) {
    61. case Qt::EditRole:
    62. case Qt::DisplayRole: return FindHistory::This->GetByIndex(index.row())->Text;
    63. }
    64. return QVariant();
    65. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox with custom model clears text in its lineEdit

    Sorry, but there is too little code to really know what is going on. Basically, if you are not updating your model in response to the editingFinished() signal, then the combo box has no idea that the text has changed and throws away the edit.

    The problem with the edit entry doesn't happen when you don't use a model because QComboBox is using its own internal model in that case, and it does the right thing when new text is entered manually - the model is updated and the new text gets set as the current item.

    Maybe this stackoverflow post will help you get on the right track. Your editingFinished() signal needs to be connected to some slot (maybe a custom slot in the model) that adds the new text to the history list, but also executes the beginInsertRows() / endInsertRows() calls so the combo box knows to update itself.
    <=== 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.

  8. #7
    Join Date
    Sep 2021
    Posts
    7
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QComboBox with custom model clears text in its lineEdit

    I had some progress when discovered that implementing `QAbstractListModel::insertRows` (that returns true) in addition to `QAbstractListModel::setData` does the work of bringing setData. Unfortunately, this still doesn't solve the initial problem: for some reason QLineEdit is cleared at last editingFinished signal called (I noticed that when pressing Enter it fires twice).
    Using setData I slightly optimized my workaround code, which restores QLineEdit content using QComboBox::setCurrentText. It still looks ugly, but better than textEdited signal usage, which fires after each char button pressed. BTW, playing with beginInsertRows() / endInsertRows() and together with setData returning true doesn't help.
    Concerning the data model, I have one data model for many QComboBox's, I want all they will have same history, but each one can have own editing content. It may be put into the history or not. Actually, it seems I miss some logic in QLineEdit-QAbstractListModel design, and probably I need to turn to Qt source code to understand it.

  9. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox with custom model clears text in its lineEdit

    Concerning the data model, I have one data model for many QComboBox's, I want all they will have same history, but each one can have own editing content. It may be put into the history or not. Actually, it seems I miss some logic in QLineEdit-QAbstractListModel design, and probably I need to turn to Qt source code to understand it.
    Yes, you are missing the point about how a custom model works. The idea of a model is that it can be shared among many different views (comboboxes, list views, table views, etc.) where the contents of the model is the same everywhere, but the way it is displayed can be different. A combobox might display only single column, whereas a table view might display everything.

    But the point you are missing is that you can't have a different content for each combobox if you use your own data model. If you have a single model, each combobox displays all of it, and the model is the combobox's only source of data. You cannot combine manually edited text and text from the model, unless that text is added to the model. So what you are trying to do with different comboboxes having different content but using the same model everywhere is impossible using just the data model alone.

    The only way you can accomplish what you want is to have a single model that contains the text entered from every combobox that uses it. If you want some comboboxes to display a different subset of that content, then you must implement a proxy model (derived from QSortFilterProxyModel) to sit between your main model and the combobox. The proxy is responsible for examining each item in the model and deciding whether it should be given to that particular combobox for display. It does this by implementing the rule in an override of the QSortFilterProxyModel::filterAcceptsRow() method.

    You could accomplish the filtering by giving each combobox an ID of some type, and associating that ID with each item in the model. Items that should appear in every box might have the ID = 0, other items have an ID unique to the combobox where the text was entered. Each combobox needs its own instance of the proxy model, and the proxy model has a member variable that is set to the ID it wants to filter on. The filterAcceptsRow() method examines the ID for each row it is given, and if the ID matches, it returns true and the item gets displayed, otherwise false and the item is ignored.
    <=== 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.

  10. #9
    Join Date
    Sep 2021
    Posts
    7
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QComboBox with custom model clears text in its lineEdit

    you are missing the point about how a custom model works. The idea of a model is that it can be shared among many different views (comboboxes, list views, table views, etc.) where the contents of the model is the same everywhere
    I am not missing this. This is exactly I wanted to implement - one model for many comboboxes + list widget

    But the point you are missing is that you can't have a different content for each combobox if you use your own data model
    But this your statement, IMHO, is wrong, in the case we are talking about QComboBox. See: Qt documentation explains that QComboBox actually combines 2 things: QLineEdit which can be freely edited by a user, and the popup list, which uses the model/view framework. QLineEdit class member may contain different content at QComboBox objects. It's true, that Qt tries synchronize this content with the popup list. But it's still separate object allocated per each QComboBox. When editable QComboBox's line edit contains some text not listed in the completer's list, its index == -1. And I have tested this my statement, it works this way, at least in my Qt version 5.9.8

    What is only confuses, why Qt QComboBox handling is different when user presses "Enter" when completes his editing or, e.g., press some another GUI button instead. This is some design problem or even a bug, I think. Currently the workaround I implemented works for me.

  11. #10
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QComboBox with custom model clears text in its lineEdit

    From qcombobox.cpp. This method is called with a new QLineEdit when QComboBox:setEditable() is called with "true". Note the signal-slot connection to the QLineEdit::returnPressed() and QLineEdit::editingFinished() signals.

    Qt Code:
    1. void QComboBox::setLineEdit(QLineEdit *edit)
    2. {
    3. Q_D(QComboBox);
    4. if (Q_UNLIKELY(!edit)) {
    5. qWarning("QComboBox::setLineEdit: cannot set a 0 line edit");
    6. return;
    7. }
    8.  
    9. if (edit == d->lineEdit)
    10. return;
    11.  
    12. edit->setText(currentText());
    13. delete d->lineEdit;
    14.  
    15. d->lineEdit = edit;
    16. #ifndef QT_NO_IM
    17. qt_widget_private(d->lineEdit)->inheritsInputMethodHints = 1;
    18. #endif
    19. if (d->lineEdit->parent() != this)
    20. d->lineEdit->setParent(this);
    21. connect(d->lineEdit, SIGNAL(returnPressed()), this, SLOT(_q_returnPressed()));
    22. connect(d->lineEdit, SIGNAL(editingFinished()), this, SLOT(_q_editingFinished()));
    23. connect(d->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(editTextChanged(QString)));
    24. connect(d->lineEdit, SIGNAL(textChanged(QString)), this, SIGNAL(currentTextChanged(QString)));
    25. // ... more
    To copy to clipboard, switch view to plain text mode 

    Here is the code for handling the editingFinished() signal() signal:

    Qt Code:
    1. void QComboBoxPrivate::_q_editingFinished()
    2. {
    3. Q_Q(QComboBox);
    4. if (lineEdit && !lineEdit->text().isEmpty() && itemText(currentIndex) != lineEdit->text()) {
    5. const int index = q_func()->findText(lineEdit->text(), matchFlags());
    6. if (index != -1) {
    7. q->setCurrentIndex(index);
    8. emitActivated(currentIndex);
    9. }
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    Basically, if the text is not already in the combobox list, it is thrown away.

    Here is the code for the returnPressed() handler:

    Qt Code:
    1. void QComboBoxPrivate::_q_returnPressed()
    2. {
    3. Q_Q(QComboBox);
    4.  
    5. // The insertion code below does not apply when the policy is QComboBox::NoInsert.
    6. // In case a completer is installed, item activation via the completer is handled
    7. // in _q_completerActivated(). Otherwise _q_editingFinished() updates the current
    8. // index as appropriate.
    9. if (insertPolicy == QComboBox::NoInsert)
    10. return;
    11.  
    12. if (lineEdit && !lineEdit->text().isEmpty()) {
    13. if (q->count() >= maxCount && !(this->insertPolicy == QComboBox::InsertAtCurrent))
    14. return;
    15. lineEdit->deselect();
    16. lineEdit->end(false);
    17. QString text = lineEdit->text();
    18. // check for duplicates (if not enabled) and quit
    19. int index = -1;
    20. if (!duplicatesEnabled) {
    21. index = q->findText(text, matchFlags());
    22. if (index != -1) {
    23. q->setCurrentIndex(index);
    24. emitActivated(currentIndex);
    25. return;
    26. }
    27. }
    28. switch (insertPolicy) {
    29. case QComboBox::InsertAtTop:
    30. index = 0;
    31. break;
    32. case QComboBox::InsertAtBottom:
    33. index = q->count();
    34. break;
    35. case QComboBox::InsertAtCurrent:
    36. case QComboBox::InsertAfterCurrent:
    37. case QComboBox::InsertBeforeCurrent:
    38. if (!q->count() || !currentIndex.isValid())
    39. index = 0;
    40. else if (insertPolicy == QComboBox::InsertAtCurrent)
    41. q->setItemText(q->currentIndex(), text);
    42. else if (insertPolicy == QComboBox::InsertAfterCurrent)
    43. index = q->currentIndex() + 1;
    44. else if (insertPolicy == QComboBox::InsertBeforeCurrent)
    45. index = q->currentIndex();
    46. break;
    47. case QComboBox::InsertAlphabetically:
    48. index = 0;
    49. for (int i=0; i< q->count(); i++, index++ ) {
    50. if (text.toLower() < q->itemText(i).toLower())
    51. break;
    52. }
    53. break;
    54. default:
    55. break;
    56. }
    57. if (index >= 0) {
    58. q->insertItem(index, text);
    59. q->setCurrentIndex(index);
    60. emitActivated(currentIndex);
    61. }
    62. }
    63. }
    To copy to clipboard, switch view to plain text mode 

    If the insert policy is NoInsert, the text is thrown away. If duplicates are not allowed and the text matches an existing list item, then that item is made current. If insertion is permitted, then the new text will be inserted at the location specified by the insertion policy, with a call to QComboBox::insertItem(). That code is below:

    Qt Code:
    1. void QComboBox::insertItem(int index, const QIcon &icon, const QString &text, const QVariant &userData)
    2. {
    3. Q_D(QComboBox);
    4. int itemCount = count();
    5. index = qBound(0, index, itemCount);
    6. if (index >= d->maxCount)
    7. return;
    8.  
    9. // For the common case where we are using the built in QStandardItemModel
    10. // construct a QStandardItem, reducing the number of expensive signals from the model
    11. if (QStandardItemModel *m = qobject_cast<QStandardItemModel*>(d->model)) {
    12. QStandardItem *item = new QStandardItem(text);
    13. if (!icon.isNull()) item->setData(icon, Qt::DecorationRole);
    14. if (userData.isValid()) item->setData(userData, Qt::UserRole);
    15. m->insertRow(index, item);
    16. ++itemCount;
    17. } else {
    18. d->inserting = true;
    19. if (d->model->insertRows(index, 1, d->root)) {
    20. QModelIndex item = d->model->index(index, d->modelColumn, d->root);
    21. if (icon.isNull() && !userData.isValid()) {
    22. d->model->setData(item, text, Qt::EditRole);
    23. } else {
    24. QMap<int, QVariant> values;
    25. if (!text.isNull()) values.insert(Qt::EditRole, text);
    26. if (!icon.isNull()) values.insert(Qt::DecorationRole, icon);
    27. if (userData.isValid()) values.insert(Qt::UserRole, userData);
    28. if (!values.isEmpty()) d->model->setItemData(item, values);
    29. }
    30. d->inserting = false;
    31. d->_q_rowsInserted(d->root, index, index);
    32. ++itemCount;
    33. } else {
    34. d->inserting = false;
    35. }
    36. }
    37.  
    38. if (itemCount > d->maxCount)
    39. d->model->removeRows(itemCount - 1, itemCount - d->maxCount, d->root);
    40. }
    To copy to clipboard, switch view to plain text mode 

    The code concerning custom models derived from QAbstractItemModel starts in line18. The first call that is made is to QAbstractItemModel::insertRows(). If your custom model does not implement this method, then the base class implementation is called. Documentation for this says:

    Note: The base class implementation of this function does nothing and returns false.

    ...

    If you implement your own model, you can reimplement this function if you want to support insertions. Alternatively, you can provide your own API for altering the data. In either case, you will need to call beginInsertRows() and endInsertRows() to notify other components that the model has changed.
    So whether you think so or not, everything displayed in the QComboBox must be contained in the model if you set a custom model for the box to use, and your model must issue the correct signals (caused by the calls to beginInsertRows / endInsertRows) so the combobox can update its contents.

    The default implementation of an editable QComboBox uses a QStandardItemModel (line 11 above) and inserts the new item at the appropriate index. This will emit the signals that the combobox uses to update.

    continued...

    When you set a custom model, the combobox will connect to all of the model's signals that get emitted when the model changes:

    Qt Code:
    1. void QComboBox::setModel(QAbstractItemModel *model)
    2. {
    3. Q_D(QComboBox);
    4.  
    5. if (Q_UNLIKELY(!model)) {
    6. qWarning("QComboBox::setModel: cannot set a 0 model");
    7. return;
    8. }
    9.  
    10. if (model == d->model)
    11. return;
    12.  
    13. #ifndef QT_NO_COMPLETER
    14. if (d->lineEdit && d->lineEdit->completer()
    15. && d->lineEdit->completer() == d->completer)
    16. d->lineEdit->completer()->setModel(model);
    17. #endif
    18. if (d->model) {
    19. disconnect(d->model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
    20. this, SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
    21. disconnect(d->model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
    22. this, SLOT(_q_updateIndexBeforeChange()));
    23. disconnect(d->model, SIGNAL(rowsInserted(QModelIndex,int,int)),
    24. this, SLOT(_q_rowsInserted(QModelIndex,int,int)));
    25. disconnect(d->model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
    26. this, SLOT(_q_updateIndexBeforeChange()));
    27. disconnect(d->model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
    28. this, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
    29. disconnect(d->model, SIGNAL(destroyed()),
    30. this, SLOT(_q_modelDestroyed()));
    31. disconnect(d->model, SIGNAL(modelAboutToBeReset()),
    32. this, SLOT(_q_updateIndexBeforeChange()));
    33. disconnect(d->model, SIGNAL(modelReset()),
    34. this, SLOT(_q_modelReset()));
    35. if (d->model->QObject::parent() == this)
    36. delete d->model;
    37. }
    38.  
    39. d->model = model;
    40.  
    41. connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
    42. this, SLOT(_q_dataChanged(QModelIndex,QModelIndex)));
    43. connect(model, SIGNAL(rowsAboutToBeInserted(QModelIndex,int,int)),
    44. this, SLOT(_q_updateIndexBeforeChange()));
    45. connect(model, SIGNAL(rowsInserted(QModelIndex,int,int)),
    46. this, SLOT(_q_rowsInserted(QModelIndex,int,int)));
    47. connect(model, SIGNAL(rowsAboutToBeRemoved(QModelIndex,int,int)),
    48. this, SLOT(_q_updateIndexBeforeChange()));
    49. connect(model, SIGNAL(rowsRemoved(QModelIndex,int,int)),
    50. this, SLOT(_q_rowsRemoved(QModelIndex,int,int)));
    51. connect(model, SIGNAL(destroyed()),
    52. this, SLOT(_q_modelDestroyed()));
    53. connect(model, SIGNAL(modelAboutToBeReset()),
    54. this, SLOT(_q_updateIndexBeforeChange()));
    55. connect(model, SIGNAL(modelReset()),
    56. this, SLOT(_q_modelReset()));
    57.  
    58. if (d->container) {
    59. d->container->itemView()->setModel(model);
    60. connect(d->container->itemView()->selectionModel(),
    61. SIGNAL(currentChanged(QModelIndex,QModelIndex)),
    62. this, SLOT(_q_emitHighlighted(QModelIndex)), Qt::UniqueConnection);
    63. }
    64.  
    65. setRootModelIndex(QModelIndex());
    66.  
    67. bool currentReset = false;
    68.  
    69. const int rowCount = count();
    70. for (int pos=0; pos < rowCount; pos++) {
    71. if (d->model->index(pos, d->modelColumn, d->root).flags() & Qt::ItemIsEnabled) {
    72. setCurrentIndex(pos);
    73. currentReset = true;
    74. break;
    75. }
    76. }
    77.  
    78. if (!currentReset)
    79. setCurrentIndex(-1);
    80.  
    81. d->modelChanged();
    82. }
    To copy to clipboard, switch view to plain text mode 

    So if you have a custom model and your model doesn't emit any of the appropriate signals when it is edited, then the combobox never updates. Editing the text in the line edit requires the model to support insertRows(), and in this method, the model must call the beginInsertRows() / endInsertRows() methods in order to ensure the combobox knows to update.
    <=== 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.

  12. The following user says thank you to d_stranz for this useful post:

    redsoft (28th September 2021)

  13. #11
    Join Date
    Sep 2021
    Posts
    7
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QComboBox with custom model clears text in its lineEdit

    Here is the code for the returnPressed() handler:
    Wow! looking into this handler I understood what happened. Qt tries to add edited text to the model in this handler, and this, in my case, breaks my another handler of editingFinished signal. What was needed to do only to add `setInsertPolicy (QComboBox::NoInsert)`, that's all. I even don't need to implement QAbstractListModel::setData/insertRows.
    I need NoInsert policy, because of my model is rebuilt in my own code invoked from editingFinished slot and other places. And I call beginInsertRows / endInsertRows by my own.
    Also this explains why pressing Enter behaves in different way than lost focus.
    Now I tested my code with NoInsert policy and removed my workaround code, and all works as expected! Many thanks!

  14. The following user says thank you to redsoft for this useful post:

    d_stranz (28th September 2021)

Similar Threads

  1. Replies: 1
    Last Post: 20th May 2015, 12:21
  2. QTableView line edit clears the text on edit
    By PlasticJesus in forum Qt Programming
    Replies: 5
    Last Post: 14th March 2015, 20:06
  3. Replies: 7
    Last Post: 5th February 2014, 12:20
  4. Keypress never fired on lineedit qcombobox
    By stef13013 in forum Qt Programming
    Replies: 0
    Last Post: 6th August 2012, 17:46
  5. Custom LineEdit - need help!
    By ArlexBee-871RBO in forum Qt Programming
    Replies: 8
    Last Post: 15th September 2010, 18:10

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.