diff options
author | Roopesh Chander <roop@forwardbias.in> | 2009-11-02 13:26:17 +0530 |
---|---|---|
committer | Roopesh Chander <roop@forwardbias.in> | 2009-11-02 13:26:17 +0530 |
commit | 62f9735598f9c33ece7f1206cd17175dcd67f3bb (patch) | |
tree | 292c724b262001d89d81eb3786dde2fe40175e0a | |
parent | 5e4370121cabc9677657f9b16944567ccf13a943 (diff) |
add Girish's lineedit from git://git.forwardbias.in/lineeditclearbutton.git
-rw-r--r-- | WebScraps.pro | 6 | ||||
-rw-r--r-- | lineedit.cpp | 46 | ||||
-rw-r--r-- | lineedit.h | 35 |
3 files changed, 85 insertions, 2 deletions
diff --git a/WebScraps.pro b/WebScraps.pro index 0ae2a76..87566cd 100644 --- a/WebScraps.pro +++ b/WebScraps.pro @@ -10,12 +10,14 @@ SOURCES += main.cpp \ webscrap.cpp \ graphicsview.cpp \ graphicstoolbar.cpp \ - resizeuihelper.cpp + resizeuihelper.cpp \ + lineedit.cpp HEADERS += mainwindow.h \ webview.h \ webscrap.h \ graphicsview.h \ graphicstoolbar.h \ - resizeuihelper.h + resizeuihelper.h \ + lineedit.h FORMS += mainwindow.ui RESOURCES += webscraps.qrc diff --git a/lineedit.cpp b/lineedit.cpp new file mode 100644 index 0000000..a4db10f --- /dev/null +++ b/lineedit.cpp @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (c) 2007 Trolltech ASA <info@trolltech.com> +** +** Use, modification and distribution is allowed without limitation, +** warranty, liability or support of any kind. +** +****************************************************************************/ + +#include "lineedit.h" +#include <QToolButton> +#include <QStyle> + +LineEdit::LineEdit(QWidget *parent) + : QLineEdit(parent) +{ + clearButton = new QToolButton(this); + QPixmap pixmap("fileclose.png"); + clearButton->setIcon(QIcon(pixmap)); + clearButton->setIconSize(pixmap.size()); + clearButton->setCursor(Qt::ArrowCursor); + clearButton->setStyleSheet("QToolButton { border: none; padding: 0px; }"); + 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; } ").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 LineEdit::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 LineEdit::updateCloseButton(const QString& text) +{ + clearButton->setVisible(!text.isEmpty()); +} + + diff --git a/lineedit.h b/lineedit.h new file mode 100644 index 0000000..7a96a52 --- /dev/null +++ b/lineedit.h @@ -0,0 +1,35 @@ +/**************************************************************************** +** +** Copyright (c) 2007 Trolltech ASA <info@trolltech.com> +** +** Use, modification and distribution is allowed without limitation, +** warranty, liability or support of any kind. +** +****************************************************************************/ + +#ifndef LINEEDIT_H +#define LINEEDIT_H + +#include <QLineEdit> + +class QToolButton; + +class LineEdit : public QLineEdit +{ + Q_OBJECT + +public: + LineEdit(QWidget *parent = 0); + +protected: + void resizeEvent(QResizeEvent *); + +private slots: + void updateCloseButton(const QString &text); + +private: + QToolButton *clearButton; +}; + +#endif // LIENEDIT_H + |