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

QLineEdit/QCompleter popup shows in top-level window

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.8
    • None
    • Fedora 41, KDE Plasma, Wayland
    • Linux/Wayland

    Description

      Consider the following code

       

      #include <QApplication>
      #include <QLineEdit>
      #include <QDialog>
      #include <QVBoxLayout>
      #include <QCompleter>
      #include <QStandardItemModel>
      #include <QIdentityProxyModel>
      
      class AdaptiveFilterProxy : public QIdentityProxyModel
      {
          Q_OBJECT
      public:
          using QIdentityProxyModel::QIdentityProxyModel;
      
          static constexpr int SearchRole = 2000;
      
          void setText(const QString &text)
          {
              if (text == m_text)
                  return;
              m_text = text;
      
              // This signal must be emitted to invalidate the completer, and force it to filter its rows again.
      
              emit dataChanged(index(0, 0), index(rowCount() - 1, 0), {SearchRole});
          }
      
          QVariant data(const QModelIndex &index, int role) const override
          {
              if (role != SearchRole)
                  return QIdentityProxyModel::data(index, role);
      
              if (!m_text.isEmpty() && data(index, Qt::DisplayRole).toString().contains(m_text, Qt::CaseInsensitive)) {
                  return m_text;
              } else {
                  return "";
              }
      
              return QVariant();
          }
      
      private:
          QString m_text;
      };
      
      int main(int argc, char**argv) {
          QApplication app(argc, argv);
          auto window = new QDialog;
          auto layout = new QVBoxLayout(window);
      
          auto lineEdit = new QLineEdit;
          layout->addWidget(lineEdit);
      
          auto model = new QStandardItemModel(window);
          model->appendRow(new QStandardItem("Hello"));
          model->appendRow(new QStandardItem("Hello 2"));
          model->appendRow(new QStandardItem("Hello 3"));
      
          auto completer = new QCompleter(window);
          lineEdit->setCompleter(completer);
      
          auto searchFilter = new AdaptiveFilterProxy(window);
          searchFilter->setSourceModel(model);
      
          QObject::connect(lineEdit, &QLineEdit::textChanged, searchFilter, &AdaptiveFilterProxy::setText);
      
          completer->setModel(searchFilter);
          completer->setFilterMode(Qt::MatchStartsWith);
          completer->setCompletionMode(QCompleter::PopupCompletion);
          completer->setCompletionRole(AdaptiveFilterProxy::SearchRole);
      
          window->show(); 
      
         return app.exec();
      }
      
      #include "main.moc" 

      When typing "H" into the lineedit the completion popup appears as expected. When then pressing "e" the popup turns into a top-level window with decoration and is no longer attached to the lineedit

       

      Attachments

        For Gerrit Dashboard: QTBUG-130474
        # Subject Branch Project Status CR V

        Activity

          People

            qt.team.quick.subscriptions Qt Quick and Widgets Team
            nicolasfella Nicolas Fella
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There is 1 open Gerrit change