Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add button within LineEdit

  • Loading branch information...
commit b0c236d5a59b259e4d4bcf6603653da82a1f5f31 1 parent a0f2216
Penk Chen authored
View
BIN  skeleton/archive/qtouchweb/fileclose.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
37 skeleton/archive/qtouchweb/mainwindow.cpp
@@ -44,6 +44,41 @@
#include "mainwindow.h"
#include "qtouchweb.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());
+}
+
MainWindow::MainWindow()
{
progress = 0;
@@ -60,7 +95,7 @@ MainWindow::MainWindow()
connect(view, SIGNAL(loadProgress(int)), SLOT(setProgress(int)));
connect(view, SIGNAL(loadFinished(bool)), SLOT(finishLoading(bool)));
- locationEdit = new QLineEdit(this);
+ locationEdit = new LineEdit(this);
locationEdit->setSizePolicy(QSizePolicy::Expanding, locationEdit->sizePolicy().verticalPolicy());
connect(locationEdit, SIGNAL(returnPressed()), SLOT(changeLocation()));
View
22 skeleton/archive/qtouchweb/mainwindow.h
@@ -40,12 +40,32 @@
****************************************************************************/
#include <QtGui>
+#include <QLineEdit>
class QWebView;
QT_BEGIN_NAMESPACE
class QLineEdit;
QT_END_NAMESPACE
+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;
+};
+
class MainWindow : public QMainWindow
{
Q_OBJECT
@@ -63,6 +83,6 @@ protected slots:
private:
QWebView *view;
- QLineEdit *locationEdit;
+ LineEdit *locationEdit;
int progress;
};

1 comment on commit b0c236d

Please sign in to comment.
Something went wrong with that request. Please try again.