add lineedit class with clear button
This commit is contained in:
@@ -439,6 +439,64 @@ void AccelLineEdit::keyPressEvent ( QKeyEvent * e)
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
#if QT_VERSION >= 0x050200
|
||||
ClearLineEdit::ClearLineEdit (QWidget * parent)
|
||||
: QLineEdit(parent), clearButton(nullptr)
|
||||
{
|
||||
clearAction = this->addAction(QIcon(QString::fromLatin1(":/icons/edit-cleartext.svg")),
|
||||
QLineEdit::TrailingPosition);
|
||||
connect(clearAction, SIGNAL(triggered()), this, SLOT(clear()));
|
||||
connect(this, SIGNAL(textChanged(const QString&)),
|
||||
this, SLOT(updateClearButton(const QString&)));
|
||||
}
|
||||
|
||||
void ClearLineEdit::resizeEvent(QResizeEvent *e)
|
||||
{
|
||||
QLineEdit::resizeEvent(e);
|
||||
}
|
||||
|
||||
void ClearLineEdit::updateClearButton(const QString& text)
|
||||
{
|
||||
clearAction->setVisible(!text.isEmpty());
|
||||
}
|
||||
#else
|
||||
ClearLineEdit::ClearLineEdit (QWidget * parent)
|
||||
: QLineEdit(parent), clearAction(nullptr)
|
||||
{
|
||||
clearButton = new QToolButton(this);
|
||||
QPixmap pixmap(BitmapFactory().pixmapFromSvg(":/icons/edit-cleartext.svg", QSize(18, 18)));
|
||||
clearButton->setIcon(QIcon(pixmap));
|
||||
clearButton->setIconSize(pixmap.size());
|
||||
clearButton->setCursor(Qt::ArrowCursor);
|
||||
clearButton->setStyleSheet(QString::fromLatin1("QToolButton { border: none; padding: 0px; }"));
|
||||
clearButton->hide();
|
||||
connect(clearButton, SIGNAL(clicked()), this, SLOT(clear()));
|
||||
connect(this, SIGNAL(textChanged(const QString&)),
|
||||
this, SLOT(updateClearButton(const QString&)));
|
||||
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
||||
setStyleSheet(QString::fromLatin1("QLineEdit { padding-right: %1px; } ")
|
||||
.arg(clearButton->sizeHint().width() + frameWidth + 1));
|
||||
QSize msz = minimumSizeHint();
|
||||
setMinimumSize(qMax(msz.width(), clearButton->sizeHint().height() + frameWidth * 2 + 2),
|
||||
qMax(msz.height(), clearButton->sizeHint().height() + frameWidth * 2 + 2));
|
||||
}
|
||||
|
||||
void ClearLineEdit::resizeEvent(QResizeEvent *)
|
||||
{
|
||||
QSize sz = clearButton->sizeHint();
|
||||
int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth);
|
||||
clearButton->move(rect().right() - frameWidth - sz.width(),
|
||||
(rect().bottom() + 1 - sz.height())/2);
|
||||
}
|
||||
|
||||
void ClearLineEdit::updateClearButton(const QString& text)
|
||||
{
|
||||
clearButton->setVisible(!text.isEmpty());
|
||||
}
|
||||
#endif
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
/* TRANSLATOR Gui::CheckListDialog */
|
||||
|
||||
/**
|
||||
|
||||
@@ -134,6 +134,30 @@ private:
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* The ClearLineEdit class adds a clear button at the right side.
|
||||
* http://stackoverflow.com/questions/21232224/qlineedit-with-custom-button
|
||||
*/
|
||||
class GuiExport ClearLineEdit : public QLineEdit
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ClearLineEdit (QWidget * parent=0);
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent *);
|
||||
|
||||
private Q_SLOTS:
|
||||
void updateClearButton(const QString &text);
|
||||
|
||||
private:
|
||||
QAction *clearAction;
|
||||
QToolButton *clearButton;
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
typedef QPair<QString, bool> CheckListItem;
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user