九宫格画法

 

如上图所示,是一张四周带阴影的图片,且右侧还有一个尖尖的箭头,但如果直接用样式表贴图,就会产生四周变形的问题。如下图所示:

 为了防止图像变形,且能随着内容拉伸,可以用九宫格画。

九宫格原理:

就是把图片分割成 9 个部分

  • 4 个角的大小不变
  • 左、右部分宽度不变,进行垂直拉伸或平铺绘制
  • 上、下部分高度不变,进行水平拉伸或平铺绘制
  • 中间部分进行拉伸或平铺绘制
#ifndef JIUGONGGE_H
#define JIUGONGGE_H


#include <QWidget>
#include <QPushButton>
#include <QLayout>
#include <QObject>

class JiuGongGe : public QWidget
{
    Q_OBJECT
public:
    explicit JiuGongGe(QWidget *parent = nullptr);

    void setText(QString text);

protected:
    void paintEvent(QPaintEvent * event);

private slots:
    void Slot_addText();

private:
    QPixmap m_bgPixmap;
    QString m_strText;
};

#endif // JIUGONGGE_H

#include "jiugongge.h"
#include <QPainter>
#include <QtMath>
#include <QDebug>

JiuGongGe::JiuGongGe(QWidget *parent) : QWidget(parent)
{
    m_bgPixmap = QPixmap("D:/XjQtWork/config/warning_bg.png");

    QPushButton * button = new QPushButton("添加一句");
    connect(button,SIGNAL(clicked(bool)),this,SLOT(Slot_addText()));

    QVBoxLayout * layout = new QVBoxLayout(this);
    layout->addStretch();
    layout->addWidget(button);
}

void JiuGongGe::setText(QString text)
{
    m_strText.append(text);
    update();
}

void JiuGongGe::paintEvent(QPaintEvent *event)
{
     QPainter painter(this);
     QFont font;
     font.setPixelSize(15);
     painter.setFont(font);

     QFontMetrics fm(font);
     int textWidth = fm.width(m_strText);
     int textHeight = fm.height();

     int width = rect().width() - 50;  //左右间距
     int row = 1;
     if(textWidth > width)
         row = qCeil(textWidth / width);
     textHeight = row * 22 + 50;


     //上面三个部分
     painter.drawPixmap(QRect(0, 0, 25, 50),m_bgPixmap, QRect(0, 0, 25, 50));
     painter.drawPixmap(QRect(25, 0, this->width() - 50, 50),m_bgPixmap, QRect(25, 0, 25, 50));
     painter.drawPixmap(QRect(this->width() - 25, 0, 25, 50),m_bgPixmap, QRect(m_bgPixmap.width() - 25, 0, 25, 50));

     //中间三个部分
     painter.drawPixmap(QRect(0, 50, 25, textHeight - 75),m_bgPixmap, QRect(0, 50, 25, 5));
     painter.drawPixmap(QRect(25,50, this->width() - 50, textHeight - 75),m_bgPixmap, QRect(25, 50, 25, 5));
     painter.drawPixmap(QRect(this->width() - 25, 50, 25, textHeight - 75),m_bgPixmap, QRect(m_bgPixmap.width() - 25, 50, 25, 5));

     //下面三个部分
     painter.drawPixmap(QRect(0, textHeight - 25, 25, 25),m_bgPixmap, QRect(0, m_bgPixmap.height() - 25, 25, 25));
     painter.drawPixmap(QRect(25,textHeight - 25, this->width() - 50, 25),m_bgPixmap, QRect(25, m_bgPixmap.height() - 25, 25, 25));
     painter.drawPixmap(QRect(this->width() - 25, textHeight - 25, 25, 25),m_bgPixmap, QRect(m_bgPixmap.width() - 25, m_bgPixmap.height() - 25, 25, 25));

     //画文本
     painter.save();
     painter.setPen(Qt::black);
     painter.drawText(QRect(30, 20, this->width() - 50,row * 25),Qt::AlignVCenter | Qt::AlignLeft | Qt::TextWordWrap, m_strText);
     painter.restore();
}

void JiuGongGe::Slot_addText()
{
    setText("这是一条提示呀呀呀呀");
}

效果如下:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值