我需要实现LineEdit
小部件,可以在文本区域的右端添加工具按钮。我知道有两种方法可以做到,但这两种解决方案看起来都很难看。
1)将工具按钮添加为QLineEdit
的子窗口小部件并处理resizeEvent
以正确定位它们。主要缺点是如果文本足够长,它可能会出现在工具按钮下。
2)另一个解决方案是将行编辑和按钮放在框架内并覆盖样式以隐藏lineEdits
框架,并使QFrame
看起来像QLineEdit
。
我需要一种最好的方法来实现这样的小部件。我的小部件也应该是风格意识。
答案 0 :(得分:26)
从Qt 5.2开始,可以使用QLineEdit::addAction(...)
插入自定义按钮。 (Qt Docs)
示例(假设我们在MyClass的定义中):
QLineEdit *myLineEdit = new QLineEdit(this);
QAction *myAction = myLineEdit->addAction(QIcon("test.png"), QLineEdit::TrailingPosition);
connect(myAction, &QAction::triggered, this, &MyClass::onActionTriggered);
答案 1 :(得分:4)
原始博客文章现已消失,但{4}为Qt 4。
没有文字的行编辑:
使用一些文本进行行编辑(出现按钮):
完整的文字行编辑(不在按钮下方):
<强> lineedit.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
<强> lineedit.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());
}
答案 2 :(得分:1)
我为此实施了这样的解决方案:
// 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中处理。如果有多个,则必须调整其坐标。您也可以修改它以使用布局。这里没有文字重叠。