关闭

urllineedit.cpp

95人阅读 评论(0) 收藏 举报
分类:
#include "urllineedit.h"
#include <QToolButton>
#include <QUrl>

UrlLineEdit::UrlLineEdit(QWidget *parent)
    : QLineEdit(parent)
    , m_favButton(new QToolButton(this))
    , m_clearButton(new QToolButton(this))
{
    m_clearButton->setIcon(QIcon(QStringLiteral(":closetab.png")));
    m_clearButton->setVisible(false);
    m_clearButton->setCursor(Qt::ArrowCursor);
    QString style(QStringLiteral("QToolButton { border: none; padding: 1px; }"));
    m_clearButton->setStyleSheet(style);
    m_favButton->setStyleSheet(style);
    setStyleSheet(QStringLiteral("QLineEdit { padding-left: %1px; padding-right: %2px;  } ")
                  .arg(m_clearButton->sizeHint().width())
                  .arg(m_favButton->sizeHint().width()));
    int minIconHeight = qMax(m_favButton->sizeHint().height(), m_clearButton->sizeHint().height());
    setMinimumSize(minimumSizeHint().width() +
                   m_favButton->sizeHint().width() +
                   m_clearButton->sizeHint().width(),
                   qMax(minimumSizeHint().height(), minIconHeight));

    connect(m_clearButton, &QToolButton::clicked, this, &QLineEdit::clear);
    connect(this, &QLineEdit::textChanged, [this](const QString &text) {
        m_clearButton->setVisible(!text.isEmpty() && !isReadOnly());
    });
}

QUrl UrlLineEdit::url() const
{
    return QUrl::fromUserInput(text());
}

void UrlLineEdit::setUrl(const QUrl &url)
{
    setText(url.toString());
    setCursorPosition(0);
}

void UrlLineEdit::setFavIcon(const QIcon &icon)
{
    QPixmap pixmap = icon.pixmap(16, 16);
    m_favButton->setIcon(pixmap);
}

void UrlLineEdit::resizeEvent(QResizeEvent *event)
{
    QLineEdit::resizeEvent(event);
    QSize clearButtonSize = m_clearButton->sizeHint();
    m_clearButton->move(rect().right() - clearButtonSize.width(),
                        (rect().bottom() - clearButtonSize.height()) / 2);
    m_favButton->move(rect().left(), (rect().bottom() - m_favButton->sizeHint().height()) / 2);
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24830次
    • 积分:355
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:73篇
    • 译文:0篇
    • 评论:0条