Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QLineEdit not clear when I selected an item after get focus in second time
QtWS25 Last Chance

QLineEdit not clear when I selected an item after get focus in second time

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 619 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kaguro
    wrote on 27 Apr 2023, 11:08 last edited by
    #1

    Hi Guys!
    Sorry for the long title but I couldn't compress my problem title :(
    I have a QLineEdit with QCompleter and I have a couple of main goals:

    • When I Select the correct text from QCompleter ( with keyboard OR mouse click ) then hide the completer listview CLEAR the QLineEdit. ( I will deal with the selected text elsewhere and it does not affect this )

    • When I type in QLineEdit and hit enter, select the first item from ListView, the listview hide and clear the QLineEdit.

    But which is very strange to me and I really don't understand: First time my solution works. But when i click on another widget (fe: to a QPushButton) so LineEdit lose focus, and i click back to the lineedit (get focus again) it breaks, and when it should clear the lineedit, the selected element from QCompleter remains in it (it only disappears if I press an extra enter). here is my class from QLineEdit:

    searchbar_tetelek::searchbar_tetelek(QWidget *parent):m_parent(parent)
    {
        this->installEventFilter(this);
        this->setMaxLength(45);
        this->setObjectName("itemsearchb");
        this->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
        this->setAlignment(Qt::AlignVCenter);
    
        font.setItalic(true);
        font.setPointSize(10);
        font.setBold(true);
        this->setFont(font);
    
        this->setPlaceholderText("Search");
        this->setFrame(false);
    
        connect(this, &QLineEdit::textChanged, [=]() {
            if(this->text().isEmpty())
            {
                font.setItalic(true);
                this->setFont(font);
            }
            else
            {
                font.setItalic(false);
                this->setFont(font);
            }
        });
        this->adjustSize();
    
        this->view = new QListView;
        this->completer = new QCompleter;
    
        completer->setCaseSensitivity(Qt::CaseInsensitive);
        completer->setFilterMode(Qt::MatchContains);
        completer->setPopup(view);
        completer->setCompletionMode(QCompleter::PopupCompletion);
        connect(this,&QLineEdit::returnPressed,[=]{Entered(); });
        this->setCompleter(completer);
        this->setContentsMargins(0,0,0,0);
    
        QHBoxLayout *hMain = new QHBoxLayout(this);
        ClickableLabel *tutu = new ClickableLabel();
        tutu->setPixmap(QIcon(":/RES/ico_1base_multiply.svg").pixmap(16,16));
        tutu->setFixedWidth(40);
        tutu->setAlignment(Qt::AlignCenter);
        tutu->setObjectName("searchside");
        tutu->setStyleSheet("#searchside{border-left:0.09em solid rgb(249, 249, 249);background:transparent;} QLineEdit[text=\"\"]{font-style: italic;color:red;}");
        connect(tutu,&ClickableLabel::clicked, [=]{this->setFocus();this->setText("");});
        connect(completer, SIGNAL(activated(const QString&)),this, SLOT(clear()),Qt::QueuedConnection);
        tutu->setCursor(Qt::PointingHandCursor);
        hMain->setAlignment(Qt::AlignRight);
        hMain->addWidget(tutu);
    }
    
    void searchbar_tetelek::SetItems(QStringList itemList)
    {
        this->items = itemList;
        completer->setModel( new QStringListModel(items) );
    }
    
    void searchbar_tetelek::Entered()
    {
        if(view->isVisible())
        {
            if(view->currentIndex().row()==-1)
            {
                view->setCurrentIndex(view->model()->index(0,0));
            }
        }
        else if(this->text()=="")
        {
            completer->setCompletionPrefix("");
            completer->complete();
        }
    }
    
    void searchbar_tetelek::showEvent(QShowEvent *)
    {
        this->setFocus();
    }
    
    bool searchbar_tetelek::eventFilter(QObject *sender, QEvent *event)
    {
        if(sender == this)
        {
            QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
    
            if( keyEvent->key() == Qt::Key_Escape)
            {   
                this->setText("");
                return true;
            }
            else
                return false;
    
        }
        return QWidget::eventFilter(sender,event);
    }
    

    And the implementation:
    (the AddtoBill function is just do something with the selected text)

    this->searchTetelek = new searchbar_tetelek(this);
    connect(searchTetelek,&QLineEdit::returnPressed,[=] {SearchAdd();});
    connect(searchTetelek->view,&QListView::clicked,[=] {SearchAdd();});
    
    void mywidgetv2::SearchAdd()
    {
        QString string = searchTetelek->view->currentIndex().data().toString();
    
        for(int i = 0;i < itemGroups->count();i++)
        {
            for(int j=0;j<itemGroups->at(i).items.count();j++)
            {
                if(itemGroups->at(i).items.at(j).CIKNEV == string)
                {
                    AddtoBill(itemGroups->at(i).items.at(j),true,Newpc);
                    searchTetelek->view->setCurrentIndex(searchTetelek->view->model()->index(-1,0));
                    searchTetelek->setText("");
                    return;
                }
            }
        }
        searchTetelek->setText("");
    }
    

    I dont know where are my mistakes. Thank you for your help!

    A 1 Reply Last reply 29 Apr 2023, 07:30
    0
    • K Kaguro
      27 Apr 2023, 11:08

      Hi Guys!
      Sorry for the long title but I couldn't compress my problem title :(
      I have a QLineEdit with QCompleter and I have a couple of main goals:

      • When I Select the correct text from QCompleter ( with keyboard OR mouse click ) then hide the completer listview CLEAR the QLineEdit. ( I will deal with the selected text elsewhere and it does not affect this )

      • When I type in QLineEdit and hit enter, select the first item from ListView, the listview hide and clear the QLineEdit.

      But which is very strange to me and I really don't understand: First time my solution works. But when i click on another widget (fe: to a QPushButton) so LineEdit lose focus, and i click back to the lineedit (get focus again) it breaks, and when it should clear the lineedit, the selected element from QCompleter remains in it (it only disappears if I press an extra enter). here is my class from QLineEdit:

      searchbar_tetelek::searchbar_tetelek(QWidget *parent):m_parent(parent)
      {
          this->installEventFilter(this);
          this->setMaxLength(45);
          this->setObjectName("itemsearchb");
          this->setSizePolicy(QSizePolicy::Preferred,QSizePolicy::Preferred);
          this->setAlignment(Qt::AlignVCenter);
      
          font.setItalic(true);
          font.setPointSize(10);
          font.setBold(true);
          this->setFont(font);
      
          this->setPlaceholderText("Search");
          this->setFrame(false);
      
          connect(this, &QLineEdit::textChanged, [=]() {
              if(this->text().isEmpty())
              {
                  font.setItalic(true);
                  this->setFont(font);
              }
              else
              {
                  font.setItalic(false);
                  this->setFont(font);
              }
          });
          this->adjustSize();
      
          this->view = new QListView;
          this->completer = new QCompleter;
      
          completer->setCaseSensitivity(Qt::CaseInsensitive);
          completer->setFilterMode(Qt::MatchContains);
          completer->setPopup(view);
          completer->setCompletionMode(QCompleter::PopupCompletion);
          connect(this,&QLineEdit::returnPressed,[=]{Entered(); });
          this->setCompleter(completer);
          this->setContentsMargins(0,0,0,0);
      
          QHBoxLayout *hMain = new QHBoxLayout(this);
          ClickableLabel *tutu = new ClickableLabel();
          tutu->setPixmap(QIcon(":/RES/ico_1base_multiply.svg").pixmap(16,16));
          tutu->setFixedWidth(40);
          tutu->setAlignment(Qt::AlignCenter);
          tutu->setObjectName("searchside");
          tutu->setStyleSheet("#searchside{border-left:0.09em solid rgb(249, 249, 249);background:transparent;} QLineEdit[text=\"\"]{font-style: italic;color:red;}");
          connect(tutu,&ClickableLabel::clicked, [=]{this->setFocus();this->setText("");});
          connect(completer, SIGNAL(activated(const QString&)),this, SLOT(clear()),Qt::QueuedConnection);
          tutu->setCursor(Qt::PointingHandCursor);
          hMain->setAlignment(Qt::AlignRight);
          hMain->addWidget(tutu);
      }
      
      void searchbar_tetelek::SetItems(QStringList itemList)
      {
          this->items = itemList;
          completer->setModel( new QStringListModel(items) );
      }
      
      void searchbar_tetelek::Entered()
      {
          if(view->isVisible())
          {
              if(view->currentIndex().row()==-1)
              {
                  view->setCurrentIndex(view->model()->index(0,0));
              }
          }
          else if(this->text()=="")
          {
              completer->setCompletionPrefix("");
              completer->complete();
          }
      }
      
      void searchbar_tetelek::showEvent(QShowEvent *)
      {
          this->setFocus();
      }
      
      bool searchbar_tetelek::eventFilter(QObject *sender, QEvent *event)
      {
          if(sender == this)
          {
              QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
      
              if( keyEvent->key() == Qt::Key_Escape)
              {   
                  this->setText("");
                  return true;
              }
              else
                  return false;
      
          }
          return QWidget::eventFilter(sender,event);
      }
      

      And the implementation:
      (the AddtoBill function is just do something with the selected text)

      this->searchTetelek = new searchbar_tetelek(this);
      connect(searchTetelek,&QLineEdit::returnPressed,[=] {SearchAdd();});
      connect(searchTetelek->view,&QListView::clicked,[=] {SearchAdd();});
      
      void mywidgetv2::SearchAdd()
      {
          QString string = searchTetelek->view->currentIndex().data().toString();
      
          for(int i = 0;i < itemGroups->count();i++)
          {
              for(int j=0;j<itemGroups->at(i).items.count();j++)
              {
                  if(itemGroups->at(i).items.at(j).CIKNEV == string)
                  {
                      AddtoBill(itemGroups->at(i).items.at(j),true,Newpc);
                      searchTetelek->view->setCurrentIndex(searchTetelek->view->model()->index(-1,0));
                      searchTetelek->setText("");
                      return;
                  }
              }
          }
          searchTetelek->setText("");
      }
      

      I dont know where are my mistakes. Thank you for your help!

      A Online
      A Online
      Axel Spoerl
      Moderators
      wrote on 29 Apr 2023, 07:30 last edited by
      #2

      @Kaguro
      While I understand the approach, I believe shoehorning that into a QCompleter is risky business. As I the name indicates, the completer wants to complete the line edit.
      If that behaviour is unwanted, it’s maybe better to instantiate a QListView with a suitable model, display it over the line edit when a fitting key is hit and then implement what’s needed.

      Software Engineer
      The Qt Company, Oslo

      K 1 Reply Last reply 2 May 2023, 06:59
      1
      • A Axel Spoerl
        29 Apr 2023, 07:30

        @Kaguro
        While I understand the approach, I believe shoehorning that into a QCompleter is risky business. As I the name indicates, the completer wants to complete the line edit.
        If that behaviour is unwanted, it’s maybe better to instantiate a QListView with a suitable model, display it over the line edit when a fitting key is hit and then implement what’s needed.

        K Offline
        K Offline
        Kaguro
        wrote on 2 May 2023, 06:59 last edited by
        #3

        @Axel-Spoerl said in QLineEdit not clear when I selected an item after get focus in second time:

        While I understand the approach, I believe shoehorning that into a QCompleter is risky business. As I the name indicates, the completer wants to complete the line edit.
        If that behaviour is unwanted, it’s maybe better to instantiate a QListView with a suitable model, display it over the line edit when a fitting key is hit and then implement what’s needed.

        Okay thanks for your answer! But I don't understand why it works fine when it first gets focus, but then it doesn't for the rest. Why could this be? Because I would be happy if I could avoid this problem and then I would be ready. Any idea why this might be? Thanks your answer again!

        A 1 Reply Last reply 3 May 2023, 05:21
        0
        • K Kaguro
          2 May 2023, 06:59

          @Axel-Spoerl said in QLineEdit not clear when I selected an item after get focus in second time:

          While I understand the approach, I believe shoehorning that into a QCompleter is risky business. As I the name indicates, the completer wants to complete the line edit.
          If that behaviour is unwanted, it’s maybe better to instantiate a QListView with a suitable model, display it over the line edit when a fitting key is hit and then implement what’s needed.

          Okay thanks for your answer! But I don't understand why it works fine when it first gets focus, but then it doesn't for the rest. Why could this be? Because I would be happy if I could avoid this problem and then I would be ready. Any idea why this might be? Thanks your answer again!

          A Online
          A Online
          Axel Spoerl
          Moderators
          wrote on 3 May 2023, 05:21 last edited by
          #4

          @Kaguro
          It's a bit hard to tell, what the a QCompleterand a corresponding QLineEditdo, if their connection is intercepted. There is a bug which we currently work on, which has to do with focus setting on a completer. Maybe it's related. You could follow it up, it's gonna be merged soon.

          Software Engineer
          The Qt Company, Oslo

          1 Reply Last reply
          1

          1/4

          27 Apr 2023, 11:08

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved