QLineEdit not clear when I selected an item after get focus in second time
-
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!
-
-
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!
@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. -
-
@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.@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!
-
@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!