我需要实现LineEdit
小部件,可以在文本区域的右端添加工具按钮。我知道这样做的两种方法,但两种解决方案似乎都很难看。
1) 添加工具按钮作为子部件QLineEdit
和句柄resizeEvent
以正确定位它们。主要缺点是如果文本足够长,它可能会出现在工具按钮下。
2)另一种解决方案是将行编辑和按钮放在框架内并覆盖样式以隐藏lineEdits
框架并使其QFrame
看起来像QLineEdit
。
我需要一种最好的方法来实现这样的小部件。我的小部件也应该具有风格意识。
从 Qt 5.2 开始,可以使用QLineEdit::addAction(...)
插入自定义按钮。(Qt 文档)
示例(假设我们在 MyClass 的定义中):
QLineEdit *myLineEdit = new QLineEdit(this);
QAction *myAction = myLineEdit->addAction(QIcon("test.png"), QLineEdit::TrailingPosition);
connect(myAction, &QAction::triggered, this, &MyClass::onActionTriggered);
原来的博文现在已经不见了,但奇趣科技曾经发布了一个Qt 4清除按钮的示例。
没有文本的行编辑:
使用一些文本进行行编辑(出现按钮):
行编辑全文本(不在按钮下方):
行编辑.h
/****************************************************************************
**
** 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
行编辑.cpp
/****************************************************************************
**
** 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());
}
一旦我为此实施了这样的解决方案:
// LineEdit.h
#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:
QToolButton *furfurIcon;
};
#endif // LINEEDIT_H
// LineEdit.cpp
#include "lineedit.h"
#include <QToolButton>
#include <QStyle>
LineEdit::LineEdit(QWidget *parent)
: QLineEdit(parent)
{
furfurIcon = new QToolButton(this);
QPixmap pixmap(":/root/your_icon");
furfurIcon->setIcon(QIcon(pixmap));
furfurIcon->setIconSize(pixmap.size());
furfurIcon->setCursor(Qt::ArrowCursor);
furfurIcon->setStyleSheet("QToolButton
"{"
"border: none; padding: 0px;"
"}");
setStyleSheet(QString("QLineEdit"
"{"
"border: 1px solid;"
"border-color: rgb(148, 168, 199);"
"border-radius: 10px;"
"background: white;"
"padding-left: %1px;"
"}").arg(furfurIcon->sizeHint().width() - 4));
setMinimumSize(0, 25);
}
void LineEdit::resizeEvent(QResizeEvent *)
{
QSize sz = furfurIcon->sizeHint();
furfurIcon->move(rect().left(), (rect().bottom() + 1 - sz.height()) / 2);
}
QToolButton 的位置在 resizeEvent 中处理。如果有多个,则必须调整它们的坐标。您也可以修改它以使用布局。这里没有重叠的文本。