QLineEdit带有自定义按钮

Arm*_*yan 5 c++ qt qwidget qlineedit qframe

我需要实现LineEdit小部件,可以在文本区域的右端添加工具按钮.我知道有两种方法可以做到这一点但两种解决方案看起来都很丑

1)添加工具按钮作为QLineEdit的子窗口小部件,并处理resizeEvent以正确定位它们.主要缺点是如果文本足够长,它可能出现在工具按钮下.

2)另一个解决方案是将行编辑和按钮放在框架内并覆盖样式以隐藏线条框架并使QFrame看起来像QLineEdit.

我需要一种最好的方法来实现这样的小部件.我的小部件也应该是风格意识.

Mor*_*per 31

从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);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • *这个*.清除按钮(`setClearButtonEnabled`)也很方便. (6认同)

Mar*_*k R 5

原来的博文现在不见了,但奇趣科技曾经发布过一个Qt 4清除按钮的例子

结果

没有文字的行编辑:

带有一些文本的行编辑(出现按钮):

全文本行编辑(不在按钮下方):

来源

行编辑器

/****************************************************************************
**
** 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
Run Code Online (Sandbox Code Playgroud)

行编辑器

/****************************************************************************
**
** 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());
}
Run Code Online (Sandbox Code Playgroud)

  • 这些“链接并运行”答案有什么用?目标页面的链接全部失效,这个答案已经变得毫无用处。请不要只提供链接;也解释一下解决方案。链接腐烂。 (2认同)