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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM