WSearchLineEdit::WSearchLineEdit(QWidget* pParent) : QLineEdit(pParent), WBaseWidget(this) { setAcceptDrops(false); m_clearButton = new QToolButton(this); QPixmap pixmap(":/skins/cross.png"); m_clearButton->setIcon(QIcon(pixmap)); m_clearButton->setIconSize(pixmap.size()); m_clearButton->setCursor(Qt::ArrowCursor); m_clearButton->setToolTip(tr("Clear input" , "Clear the search bar input field")); m_clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }"); m_clearButton->hide(); m_place = true; showPlaceholder(); setFocusPolicy(Qt::ClickFocus); QShortcut *setFocusShortcut = new QShortcut( QKeySequence(tr("Ctrl+F", "Search|Focus")), this); connect(setFocusShortcut, SIGNAL(activated()), this, SLOT(setFocus())); connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(slotTextChanged(const QString&))); // Set up a timer to search after a few hundred milliseconds timeout. This // stops us from thrashing the database if you type really fast. m_searchTimer.setSingleShot(true); connect(&m_searchTimer, SIGNAL(timeout()), this, SLOT(triggerSearch())); connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(slotSetupTimer(const QString&))); // When you hit enter, it will trigger the search. connect(this, SIGNAL(returnPressed()), this, SLOT(triggerSearch())); connect(m_clearButton, SIGNAL(clicked()), this, SLOT(onSearchTextCleared())); // Forces immediate update of tracktable connect(m_clearButton, SIGNAL(clicked()), this, SLOT(triggerSearch())); connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(updateCloseButton(const QString&))); // The width of the frame for the widget based on the styling. int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); // Ensures the text does not obscure the clear image. setStyleSheet(QString("QLineEdit { padding-right: %1px; } "). arg(m_clearButton->sizeHint().width() + frameWidth + 1)); }
void WSearchLineEdit::slotTextChanged(const QString& text) { if (text.isEmpty()) { triggerSearch(); emit(searchCleared()); } }