Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-71732

Editable ComboBoxes are not Validated by ItemDelegates

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 5.11.2
    • Widgets: Itemviews
    • None
    • OS: Windows 7 Professional 64-bits
      Qt: Qt 5.11.2 MSVC 2015 64-bits
      IDE: Microsoft Visual Studio 2015
    • Windows

    Description

      Introduction
      A QComboBox can be made editable for accepting custom values. Moreover, a QComboBox can use a QValidator to restrict the range of acceptable values.

      The Problem
      In an ItemDelegate, when using editable QComboBoxes with a QValidator, the QComboBox content is not validated by the ItemDelegate. This causes the QComboBox to accept any input as long as the input can be typed into it. The correct behavior is observed when using a QLineEdit instead of a QComboBox.

      Sample Code
      The sample code (see attachment) implements an application that shows a table with two columns. The first column has an ItemDelegate that returns a QComboBox from the QAbstractItemDelegate::createEditor. The ItemDelegate of the second column returns a QLineEdit. Both the editors are validated by a QDoubleValidator.

      If you type a non-acceptable value (e.g., "" or "e") and then press Enter, the column with the editable QComboBox closes accepting the input, while the column with the QLineEdit remains opened rejecting the input.

      Possible Fix
      My best guess is that the bug is in the method QAbstractItemDelegatePrivate::tryFixup (see below). This method checks if the editor is a QLineEdit; if so, then the QLineEdit is validated. If the editor is not a QLineEdit, then its content is accepted trivially.

      // File "qtbase\src\widgets\itemviews\qabstractitemdelegate.cpp", Line 552.
      bool QAbstractItemDelegatePrivate::tryFixup(QWidget *editor) {
        if (QLineEdit *e = qobject_cast<QLineEdit*>(editor)) { // If the editor is a QLineEdit, then the QLineEdit content is validated below.
          if (!e->hasAcceptableInput()) {
            if (const QValidator *validator = e->validator()) {
              QString text = e->text();
              validator->fixup(text);
              e->setText(text);
            }
            return e->hasAcceptableInput();
          }
        }
      
        return true; // The content of all other editors are trivially accepted.
      }
      

      In my understanding, to fix the bug, it should be also regarded that editor can be a QComboBox. If so, then the QLineEdit used to edit items in the QComboBox (accessible through the method QComboBox::lineEdit) should be validated similarly.

       

      Attachments

        1. delegates.h
          1 kB
        2. main.cpp
          0.8 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            sauloapessoa Saulo Pessoa
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes