QLineEdit实现定制化的QLineEdit,QLineEdit中间可输入文本,两边加入图标点缀的实现思路

QLineEdit实现定制化的QLineEdit,QLineEdit中间可输入文本,两边加入图标点缀的实现思路,继承QlineEdit,定制属于自己的QLineEdit,方便入门级理解,直接上实现代码,显示效果大致如下

QLineEdit实现定制化的QLineEdit,QLineEdit中间可输入文本,两边加入图标点缀的实现思路_第1张图片

//.h

#pragma once
#include
#include
#include
#include
class  specialLineEdit : public QLineEdit
{
    Q_OBJECT  //槽函数使用,需要这个宏

public:
    explicit specialLineEdit(QWidget *parent = 0, int with = 285, int height = 26);
    ~specialLineEdit();
    void setLeftIcon(const QString &normal);
    void setRightIcon(const QString &normal);
    void setLeftIconSize(int width, int height);
    void setRightIconSize(int width, int height);
    void getLeftIconSize(int &width, int &height);
    void getRightIconSize(int &width, int &height);
protected slots:
    void slotsRightButtonClicked();

protected:
    void keyPressEvent(QKeyEvent *event);

signals:
    void SigRigthButtonClicked();

private:
    void initUI();
    void freshTextAlign();
private:
    QLabel *m_pLabel;
    QPushButton *m_pRightButton;
    QHBoxLayout *m_pHBoxLayout;
};

//.cpp

#include "lineEdit.h"
#include

static const int nTextLeftPadding = 3;
static const int nTextRightPadding = 6;
specialLineEdit::specialLineEdit(QWidget *parent, int width, int height)
    :QLineEdit(parent)
{
    resize(width, height);
    this->setFixedHeight(height);
    initUI();

}

specialLineEdit::~specialLineEdit()
{

}

void specialLineEdit::initUI()
{
    
    //左侧图标
    m_pLabel = new QLabel();
    m_pLabel->setFixedSize(this->height()*0.6, this->height()*0.6);
    m_pLabel->setHidden(true);
    m_pLabel->setFocusPolicy(Qt::NoFocus);

    //右侧按钮
    m_pRightButton = new QPushButton(this);
    m_pRightButton->setCursor(Qt::PointingHandCursor);
    m_pRightButton->setFixedSize(this->height()*0.6, this->height()*0.6);
    m_pRightButton->setHidden(true);
    m_pRightButton->setFocusPolicy(Qt::NoFocus);

    m_pHBoxLayout = new QHBoxLayout();
    m_pHBoxLayout->setMargin(0);
    m_pHBoxLayout->addWidget(m_pLabel);

    QSpacerItem *pSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
    m_pHBoxLayout->addItem(pSpacer);

    m_pHBoxLayout->addWidget(m_pRightButton);
    m_pHBoxLayout->setSpacing(0);
    m_pHBoxLayout->setContentsMargins(nTextLeftPadding, 0, nTextRightPadding, 0);

    this->setLayout(m_pHBoxLayout);

    freshTextAlign();

    connect(m_pRightButton, SIGNAL(clicked()), this, SLOT(slotsRightButtonClicked()));
}

void specialLineEdit::setLeftIcon(const QString &normal)
{
    m_pLabel->setHidden(false);
    QString strQss = QString("QLabel{border-image:url(%1); background:transparent;}").arg(normal);
    m_pLabel->setStyleSheet(strQss);
    freshTextAlign();
}

void specialLineEdit::setRightIcon(const QString &normal)
{
    m_pRightButton->setHidden(false);
    QString strQss = QString("QPushButton{image:url(%1); background:transparent; border: none;}").arg(normal);
    m_pRightButton->setStyleSheet(strQss);
    freshTextAlign();
}

void specialLineEdit::freshTextAlign()
{
    //设置完图片以后需要计算一下文本
    QMargins margins = this->textMargins();
    int nLabelWidth(0);
    if (!m_pLabel->isHidden())
    {
        int nLeft = m_pHBoxLayout->contentsMargins().left();
        nLabelWidth = nLeft + m_pLabel->width();
    }
    int nButtonWidth = 0;
    if (!m_pRightButton->isHidden())
    {
        int nRight = m_pHBoxLayout->contentsMargins().right();
        nButtonWidth = nRight + m_pRightButton->width() - 2;
    }
    this->setTextMargins(nLabelWidth + nTextLeftPadding, margins.top(), nButtonWidth + nTextRightPadding, margins.bottom());
}

void specialLineEdit::setLeftIconSize(int width, int height)
{
    m_pLabel->setFixedSize(width, height);
}

void specialLineEdit::setRightIconSize(int width, int height)
{
    m_pRightButton->setFixedSize(width, height);
}

void specialLineEdit::getLeftIconSize(int &width, int &height)
{
    width = m_pLabel->width();
    height = m_pLabel->height();
}

void specialLineEdit::getRightIconSize(int &width, int &height)
{
    width = m_pRightButton->width();
    height = m_pRightButton->height();
}

void specialLineEdit::slotsRightButtonClicked()
{
    emit SigRigthButtonClicked();
}

void specialLineEdit::keyPressEvent(QKeyEvent * event)
{
    if (echoMode() == QLineEdit::Password)
    {
        if (event->matches(QKeySequence::SelectAll))
        {
            return;   //如果lineEdit设置成密码的形式,不可以全选
        }
        else if (event->matches(QKeySequence::Copy))
        {
            return;  //如果lineEdit设置成密码的形式,不可以拷贝
        }
        else if (event->matches(QKeySequence::Paste))
        {
            return; //如果lineEdit设置成密码的形式,不可以粘贴
        }
    }
    return QLineEdit::keyPressEvent(event);
}

你可能感兴趣的:(Qt,C++,界面,qt,c++)