QLineEdit 自定义 左侧图标,中间可输入文字,右侧图标按钮

2022/2/22 6:23:33

本文主要是介绍QLineEdit 自定义 左侧图标,中间可输入文字,右侧图标按钮,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

 

//.h

#pragma once
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QLabel>
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 <QKeyEvent>

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);
}
————————————————
版权声明:本文为CSDN博主「Inkred」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012377293/article/details/108807355



这篇关于QLineEdit 自定义 左侧图标,中间可输入文字,右侧图标按钮的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程