From 0eaf991d6fff9185b12ed5606ada4c58bd526db2 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 7 Nov 2015 12:34:52 +0800 Subject: [PATCH 1/2] Use QLineEdit built-in ClearButton (Qt5 only) --- src/gui/lineedit/src/lineedit.cpp | 49 +++++++++++++++++++++---------- src/gui/lineedit/src/lineedit.h | 10 +++---- src/searchengine/searchengine.cpp | 2 +- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/gui/lineedit/src/lineedit.cpp b/src/gui/lineedit/src/lineedit.cpp index 80bf67465..d6d8f36f3 100644 --- a/src/gui/lineedit/src/lineedit.cpp +++ b/src/gui/lineedit/src/lineedit.cpp @@ -8,21 +8,28 @@ ****************************************************************************/ #include "lineedit.h" -#include +#include #include -#include +#include +#include LineEdit::LineEdit(QWidget *parent) : QLineEdit(parent) { - searchButton = new QToolButton(this); + int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); + QPixmap pixmap1(":/lineeditimages/search.png"); + searchButton = new QToolButton(this); searchButton->setIcon(QIcon(pixmap1)); searchButton->setIconSize(pixmap1.size()); searchButton->setCursor(Qt::ArrowCursor); searchButton->setStyleSheet("QToolButton { border: none; padding: 2px; }"); - clearButton = new QToolButton(this); + + int clearButtonSizeHintWidth = 0; + int clearButtonSizeHintHeight = 0; +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) QPixmap pixmap2(":/lineeditimages/clear_left.png"); + clearButton = new QToolButton(this); clearButton->setIcon(QIcon(pixmap2)); clearButton->setIconSize(pixmap2.size()); clearButton->setCursor(Qt::ArrowCursor); @@ -31,25 +38,35 @@ LineEdit::LineEdit(QWidget *parent) clearButton->hide(); connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(updateCloseButton(const QString&))); - int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); - setStyleSheet(QString("QLineEdit { padding-right: %1px; padding-left: %2px; }").arg(clearButton->sizeHint().width() + frameWidth + 1).arg(clearButton->sizeHint().width() + frameWidth + 1)); - QSize msz = minimumSizeHint(); - setMinimumSize(qMax(msz.width(), clearButton->sizeHint().width() + searchButton->sizeHint().width() + frameWidth * 2 + 2), - qMax(msz.height(), clearButton->sizeHint().height() + frameWidth * 2 + 2)); + + clearButtonSizeHintWidth = clearButton->sizeHint().width(); + clearButtonSizeHintHeight = clearButton->sizeHint().height(); + setStyleSheet(QString("QLineEdit { padding-left: %1px; padding-right: %2px; }").arg(searchButton->sizeHint().width()).arg(clearButtonSizeHintWidth)); +#else + setClearButtonEnabled(true); + setStyleSheet(QString("QLineEdit { padding-left: %1px; }").arg(searchButton->sizeHint().width())); // padding between text and widget borders +#endif + + QSize msz = sizeHint(); + setMinimumSize(qMax(msz.width(), searchButton->sizeHint().width() + clearButtonSizeHintWidth), + std::max({ msz.height(), searchButton->sizeHint().height(), clearButtonSizeHintHeight }) + frameWidth * 2); } -void LineEdit::resizeEvent(QResizeEvent *) +void LineEdit::resizeEvent(QResizeEvent *e) { - QSize sz = searchButton->sizeHint(); int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); - searchButton->move(rect().left() + frameWidth, (rect().bottom() + 2 - sz.height())/2); - sz = clearButton->sizeHint(); - clearButton->move(rect().right() - frameWidth - sz.width(), - (rect().bottom() + 2 - sz.height())/2); + + QSize sz = searchButton->sizeHint(); + searchButton->move(frameWidth, (e->size().height() - sz.height()) / 2); +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) + QSize cz = clearButton->sizeHint(); + clearButton->move((e->size().width() - frameWidth - cz.width()), (e->size().height() - sz.height()) / 2); +#endif } void LineEdit::updateCloseButton(const QString& text) { +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) clearButton->setVisible(!text.isEmpty()); +#endif } - diff --git a/src/gui/lineedit/src/lineedit.h b/src/gui/lineedit/src/lineedit.h index df3cbe590..77342ade3 100644 --- a/src/gui/lineedit/src/lineedit.h +++ b/src/gui/lineedit/src/lineedit.h @@ -12,26 +12,26 @@ #include -QT_BEGIN_NAMESPACE class QToolButton; -QT_END_NAMESPACE class LineEdit : public QLineEdit { Q_OBJECT public: - LineEdit(QWidget *parent = 0); + LineEdit(QWidget *parent); protected: - void resizeEvent(QResizeEvent *); + void resizeEvent(QResizeEvent *e); private slots: void updateCloseButton(const QString &text); private: - QToolButton *clearButton; QToolButton *searchButton; +#if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) + QToolButton *clearButton; +#endif }; #endif // LIENEDIT_H diff --git a/src/searchengine/searchengine.cpp b/src/searchengine/searchengine.cpp index f96c7eea8..54e50c035 100644 --- a/src/searchengine/searchengine.cpp +++ b/src/searchengine/searchengine.cpp @@ -64,7 +64,7 @@ /*SEARCH ENGINE START*/ SearchEngine::SearchEngine(MainWindow* parent) : QWidget(parent) - , search_pattern(new LineEdit) + , search_pattern(new LineEdit(this)) , mp_mainWindow(parent) { setupUi(this); From ccec11e568553379a2e265a32356094d17df9460 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 7 Nov 2015 16:24:16 +0800 Subject: [PATCH 2/2] Follow project coding style. Issue #2192. --- src/gui/lineedit/src/lineedit.cpp | 70 +++++++++++++++---------------- src/gui/lineedit/src/lineedit.h | 12 +++--- 2 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/gui/lineedit/src/lineedit.cpp b/src/gui/lineedit/src/lineedit.cpp index d6d8f36f3..8de914ab0 100644 --- a/src/gui/lineedit/src/lineedit.cpp +++ b/src/gui/lineedit/src/lineedit.cpp @@ -14,59 +14,59 @@ #include LineEdit::LineEdit(QWidget *parent) - : QLineEdit(parent) + : QLineEdit(parent) { - int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); + int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); - QPixmap pixmap1(":/lineeditimages/search.png"); - searchButton = new QToolButton(this); - searchButton->setIcon(QIcon(pixmap1)); - searchButton->setIconSize(pixmap1.size()); - searchButton->setCursor(Qt::ArrowCursor); - searchButton->setStyleSheet("QToolButton { border: none; padding: 2px; }"); + QPixmap pixmap1(":/lineeditimages/search.png"); + searchButton = new QToolButton(this); + searchButton->setIcon(QIcon(pixmap1)); + searchButton->setIconSize(pixmap1.size()); + searchButton->setCursor(Qt::ArrowCursor); + searchButton->setStyleSheet("QToolButton { border: none; padding: 2px; }"); - int clearButtonSizeHintWidth = 0; - int clearButtonSizeHintHeight = 0; + int clearButtonSizeHintWidth = 0; + int clearButtonSizeHintHeight = 0; #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) - QPixmap pixmap2(":/lineeditimages/clear_left.png"); - clearButton = new QToolButton(this); - clearButton->setIcon(QIcon(pixmap2)); - clearButton->setIconSize(pixmap2.size()); - clearButton->setCursor(Qt::ArrowCursor); - clearButton->setStyleSheet("QToolButton { border: none; padding: 2px; }"); - clearButton->setToolTip(tr("Clear the text")); - clearButton->hide(); - connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); - connect(this, SIGNAL(textChanged(const QString&)), this, SLOT(updateCloseButton(const QString&))); + QPixmap pixmap2(":/lineeditimages/clear_left.png"); + clearButton = new QToolButton(this); + clearButton->setIcon(QIcon(pixmap2)); + clearButton->setIconSize(pixmap2.size()); + clearButton->setCursor(Qt::ArrowCursor); + clearButton->setStyleSheet("QToolButton { border: none; padding: 2px; }"); + clearButton->setToolTip(tr("Clear the text")); + clearButton->hide(); + connect(clearButton, SIGNAL(clicked()), this, SLOT(clear())); + connect(this, SIGNAL(textChanged(const QString &)), this, SLOT(updateCloseButton(const QString &))); - clearButtonSizeHintWidth = clearButton->sizeHint().width(); - clearButtonSizeHintHeight = clearButton->sizeHint().height(); - setStyleSheet(QString("QLineEdit { padding-left: %1px; padding-right: %2px; }").arg(searchButton->sizeHint().width()).arg(clearButtonSizeHintWidth)); + clearButtonSizeHintWidth = clearButton->sizeHint().width(); + clearButtonSizeHintHeight = clearButton->sizeHint().height(); + setStyleSheet(QString("QLineEdit { padding-left: %1px; padding-right: %2px; }").arg(searchButton->sizeHint().width()).arg(clearButtonSizeHintWidth)); #else - setClearButtonEnabled(true); - setStyleSheet(QString("QLineEdit { padding-left: %1px; }").arg(searchButton->sizeHint().width())); // padding between text and widget borders + setClearButtonEnabled(true); + setStyleSheet(QString("QLineEdit { padding-left: %1px; }").arg(searchButton->sizeHint().width())); // padding between text and widget borders #endif - QSize msz = sizeHint(); - setMinimumSize(qMax(msz.width(), searchButton->sizeHint().width() + clearButtonSizeHintWidth), - std::max({ msz.height(), searchButton->sizeHint().height(), clearButtonSizeHintHeight }) + frameWidth * 2); + QSize msz = sizeHint(); + setMinimumSize(qMax(msz.width(), searchButton->sizeHint().width() + clearButtonSizeHintWidth), + std::max({ msz.height(), searchButton->sizeHint().height(), clearButtonSizeHintHeight }) + frameWidth * 2); } void LineEdit::resizeEvent(QResizeEvent *e) { - int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); + int frameWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth); - QSize sz = searchButton->sizeHint(); - searchButton->move(frameWidth, (e->size().height() - sz.height()) / 2); + QSize sz = searchButton->sizeHint(); + searchButton->move(frameWidth, (e->size().height() - sz.height()) / 2); #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) - QSize cz = clearButton->sizeHint(); - clearButton->move((e->size().width() - frameWidth - cz.width()), (e->size().height() - sz.height()) / 2); + QSize cz = clearButton->sizeHint(); + clearButton->move((e->size().width() - frameWidth - cz.width()), (e->size().height() - sz.height()) / 2); #endif } -void LineEdit::updateCloseButton(const QString& text) +void LineEdit::updateCloseButton(const QString &text) { #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) - clearButton->setVisible(!text.isEmpty()); + clearButton->setVisible(!text.isEmpty()); #endif } diff --git a/src/gui/lineedit/src/lineedit.h b/src/gui/lineedit/src/lineedit.h index 77342ade3..74ee9b423 100644 --- a/src/gui/lineedit/src/lineedit.h +++ b/src/gui/lineedit/src/lineedit.h @@ -16,21 +16,21 @@ class QToolButton; class LineEdit : public QLineEdit { - Q_OBJECT + Q_OBJECT public: - LineEdit(QWidget *parent); + LineEdit(QWidget *parent); protected: - void resizeEvent(QResizeEvent *e); + void resizeEvent(QResizeEvent *e); private slots: - void updateCloseButton(const QString &text); + void updateCloseButton(const QString &text); private: - QToolButton *searchButton; + QToolButton *searchButton; #if (QT_VERSION < QT_VERSION_CHECK(5, 0, 0)) - QToolButton *clearButton; + QToolButton *clearButton; #endif };