Qt那些年自定义的控件--仿微信消息提示

伴随着产品经理的突发奇想。qt原生的控件已经不能满足日常开荒的需要。往往好多控件都需要程序员自己去绘制。当然能用贴图解决的尽量贴图。不能的只能自己绘制处理事件,万变不离其宗。不会绘制控件的程序员不是好码农。话不多说。进入正题

1、背景。设置图片就行

2、数字。这个动态的肯定要绘制

3、文字红色背景也是需要绘制

4、本文的红点是不会动的。类似qq。拖拽消失。有能力的同学可以开发修改。

下面直接上关键代码

 Q_OBJECT
    Q_PROPERTY(QString textFontFamily READ getTextFontFamily WRITE setTextFontFamily)
    Q_PROPERTY(int textFontSize READ getTextFontSize WRITE setTextFontSize)
    Q_PROPERTY(QColor textFontColor READ getTextFontColor WRITE setTextFontColor)
    Q_PROPERTY(QString redDotFontFamily READ getRedDotFontFamily WRITE setRedDotFontFamily)
    Q_PROPERTY(int redDotFontSize READ getRedDotFontSize WRITE setRedDotFontSize)
    Q_PROPERTY(QColor redDotFontColor READ getRedDotFontColor WRITE setRedDotFontColor)
    Q_PROPERTY(QColor redDotColor READ getRedDotColor WRITE setRedDotColor)
	Q_PROPERTY(QColor backgroundColor READ getBackgroundColor WRITE setBackgroundColor)
	Q_PROPERTY(QColor selectedBackgroundColor READ getSelectedBackgroundColor WRITE setSelectedBackgroundColor)
	Q_PROPERTY(QColor mouseEnterBackgroundColor READ getMouseEnterBackgroundColor WRITE setMouseEnterBackgroundColor)
	Q_PROPERTY(int boardSize READ getBoardSize WRITE setBoardSize)
	Q_PROPERTY(QString boardColors READ getBoardColor WRITE setBoardColor)
public:
    explicit HeRedDotWidget(QWidget *parent = nullptr);
    bool showRedDot(bool show);
    enum eRedDotLocation
    {
        eRedDot_LeftTop = 0,
        eRedDot_RightTop,
        eRedDot_RightCenter,
    };
public:
	QString getTextFontFamily() const;
	int getTextFontSize();
	QColor getTextFontColor();
	QString getRedDotFontFamily() const;
	int getRedDotFontSize();
	QColor getRedDotFontColor();
	QColor getRedDotColor();
	QColor getBackgroundColor();
	QColor getSelectedBackgroundColor();
	QColor getMouseEnterBackgroundColor();
	int getBoardSize();
	QString getBoardColor() const;

    void setShowRedDot(bool show);
    void setTextFontFamily(const QString &family);
    void setTextFontSize(int size);
    void setTextFontColor(QColor color);
    void setRedDotFontFamily(const QString &family);
    void setRedDotFontSize(int size);
    void setRedDotFontColor(QColor color);
    void setRedDotColor(QColor color);
    void setRedDotLocation(eRedDotLocation location);
	void setBackgroundColor(QColor color);
	void setSelectedBackgroundColor(QColor color);
	void setMouseEnterBackgroundColor(QColor color);
	void setTextAlignment(Qt::AlignmentFlag align);
	void setBoardSize(int size);
	void setBoardColor(const QString &colors);
public:
    void setText(const QString &text);
    QString text() const;
    void setRedDotText(const QString &text);
    QString redDotText() const;
	void setPrePix(const QString &pix);
	void setPixmap(const QString &pix);
	void setSelected(bool select);

 

void HeRedDotWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing);
	QColor backcolor = mBackgroundColor;
	if (mMouseEnter)
		backcolor = mMouseMoveBackgroundColor;
	else if (mSelected)
		backcolor = mSelectBackgroundColor;
	else
		backcolor = mBackgroundColor;
	painter.fillRect(0, 0, width(), height(), backcolor);

	QVector<QPoint> points;
	QLinearGradient grd;
	QStringList colors = mBoardColors.split(",");
	for each (QString color in colors)
	{
		QStringList col = color.split(" ");
		if(col.size() >= 2)
			grd.setColorAt(col.first().toInt(), QColor(col.last()));
	}
	QPen pen;
	pen.setBrush(QBrush(grd));
	pen.setWidth(mBoardSize);
	painter.setPen(pen);
	painter.drawLine(0, 0, this->width() , 0);
	painter.drawLine(0, 0, 0, this->height() );
	painter.drawLine(this->width() , 0, this->width(), this->height() );
	painter.drawLine(0, this->height() , this->width() , this->height() );

	drawPreImage(&painter);
	if (!mPix.isEmpty())
		drawPixmap(&painter);
	else if(!mText.isEmpty())
		drawText(&painter);
    if(mShowRedDot)
        drawTips(&painter);
}
void HeRedDotWidget::drawText(QPainter *painter)
{
    QPen pen(mTextFontColor);
    painter->setPen(pen);
    QFont font(mTextFontFamily,mTextFontSize);
    painter->setFont(font);
    QFontMetrics fm(font);
    QString text = fm.elidedText(mText,Qt::ElideRight,this->width());
    int width = fm.width(text);
	double x = 0;
	switch (mAlignment)
	{
	case Qt::AlignLeft:
		if (!mPixPre.isEmpty())
			x = mPixSize.x();
		else x = 10;
		break;
	case Qt::AlignHCenter:
		x = (this->width() - width) / 2;
		break;
	default:
		break;
	}
	painter->drawText(QRectF(x, (this->height() - fm.height()) / 2, width, fm.height()), text);
    //painter->drawText(QRectF((this->width()-width)/2,(this->height()-fm.height())/2,width,fm.height()),text);
}
void HeRedDotWidget::drawTips(QPainter *painter)
{
	QFont fontTip(mRedDotFontFamily, mRedDotFontSize);
	painter->setFont(fontTip);
	painter->setPen(QPen(mRedDotColor));
	QFontMetrics fmTip(fontTip);
	double tipWidht = fmTip.width(mTip);
	double tipheight = fmTip.height();
	painter->setBrush(mRedDotColor);
	QRectF rect;
	double height = tipheight;
	if (mTip.isEmpty())
	{
		tipWidht = 2.5;
		height = tipWidht*2;
	}
	QRectF tipRect;
	switch (mRedDotLocation) {
	case eRedDot_LeftTop:
		rect = QRectF(0, 1, tipWidht * 2, height);
		tipRect = QRectF(tipWidht / 2, 1, tipWidht, tipheight);
		break;
	case eRedDot_RightTop:
		rect = QRectF(this->width() - tipWidht * 2, 1, tipWidht * 2, height);
		tipRect = QRectF(this->width() - tipWidht / 2 * 3, 1, tipWidht, tipheight);
		break;
	case eRedDot_RightCenter:
		rect = QRectF(this->width() - tipWidht * 2, (this->height() - tipheight) / 2.0, tipWidht * 2, height);
		tipRect = QRectF(this->width() - tipWidht / 2 * 3, (this->height() - tipheight) / 2, tipWidht, tipheight);
		break;
	}
	painter->drawEllipse(rect);

	QPen pen(mRedDotFontColor);
	painter->setPen(pen);

	painter->drawText(tipRect, mTip);
}

以上没经过严格整理。欢迎盆友们指正,交流。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值