我想绘制自定义QLineEdit
,尤其是舍入QlineEdit
。
这是我的代码,但是当我执行该应用时,我无法在自定义QlineEdit
中撰写。
linedit.h:
#ifndef LINEDIT_H
#define LINEDIT_H
#include <QWidget>
#include <QLineEdit>
#include <QPainter>
#include <QStyleOptionFrame>
#include <QRect>
class linedit : public QLineEdit
{
Q_OBJECT
public:
explicit linedit(QWidget *parent = 0);
enum LineEditState{STATE_NOEMPTY, STATE_EMPTY};
void setHintText(const QString &msg);
void setPixmap(QString _pix_);
QPixmap pixmap();
protected:
void paintEvent (QPaintEvent *event);
void focusInEvent (QFocusEvent *event);
void focusOutEvent (QFocusEvent *event);
signals:
public slots:
private:
LineEditState l_State;
QString hintText;
};
#endif // LINEDIT_H
linedit.cpp
#include "linedit.h"
linedit::linedit(QWidget *parent) : QLineEdit(parent)
{
l_State = STATE_EMPTY;
}
void linedit::setPixmap(QString _pix_)
{
}
QPixmap linedit::pixmap()
{
}
void linedit::setHintText(const QString &msg)
{
hintText = msg;
l_State = STATE_EMPTY;
update();
}
void linedit::paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
{
/********** LineEdit's Font **********/
QFont f;
f.setPixelSize(21);
f.setFamily("Josefin Sans");
f.setBold(true);
/************************************/
QPainter painterContour(this);
/************************************/
QLineEdit qLine;
QStyleOptionFrame option;
initStyleOption(&option);
option.initFrom(&qLine);
//option.state |= QStyle::State_KeyboardFocusChange;
option.rect = QRect(10,10,width()-20,height()-20);
//option.lineWidth = style()->pixelMetric(QStyle::PM_DefaultFrameWidth,&option,&qLine);
//option.midLineWidth = 0;
//option.state |= QStyle::State_Sunken;
//option.features = QStyleOptionFrameV2::None;
/************************************/
painterContour.setFont(f);
painterContour.setPen(QPen(QColor(171,173,174)));
painterContour.setRenderHint(QPainter::Antialiasing);
switch(l_State)
{
case STATE_EMPTY:
{
painterContour.setBrush(QBrush(QColor(255,255,255)));
painterContour.drawRoundedRect(QRect(5,5,width()-10,height()-10),15,15);
painterContour.drawText(option.rect,Qt::AlignLeft|Qt::AlignVCenter, hintText);
}break;
case STATE_NOEMPTY:
{
//painterContour.setBrush(QBrush(QColor(255,255,255)));
//painterContour.drawRoundedRect(QRect(5,5,width()-10,height()-10),15,15);
}break;
}
painterContour.end();
style()->drawPrimitive(QStyle::PE_PanelLineEdit, &option, &painterContour, &qLine);
}
}
void linedit::focusInEvent(QFocusEvent *event)
{
QLineEdit::focusInEvent(event);
//QWidget::focusInEvent(event);
if(l_State == STATE_EMPTY)
{
l_State = STATE_NOEMPTY;
}
update();
}
void linedit::focusOutEvent(QFocusEvent *event)
{
QLineEdit::focusOutEvent(event);
if(l_State == STATE_EMPTY)
{
l_State = STATE_NOEMPTY;
}
update();
}
请你帮助我,非常感谢