void paintRow(const QPixmap &pix, QPainter &painter, int begin, int y, int width, int h)
{
while (begin < width-pix.width())
{
if (width-pix.width()-begin > pix.width())
{
painter.drawPixmap(begin, y, pix.width(), h, pix);
}
else
{
painter.drawPixmap(begin, y, width-pix.width()-begin, h, pix);
}
begin += pix.width();
}
}
void paintColumn(const QPixmap &pix, QPainter &painter, int x, int begin, int w, int height)
{
while (begin < height-pix.height())
{
if (height-pix.height()-begin > pix.height())
{
painter.drawPixmap(x, begin, w, pix.height(), pix);
}
else
{
painter.drawPixmap(x, begin, w, height-pix.height()-begin, pix);
}
begin += pix.height();
}
}
/**
* @brief PaintBackground 九宫格贴背景
* @param pWidget
*
* |1|2|3| |0|1|2|
* |4|5|6| |3|4|5|
* |7|8|9| |6|7|8|
*
*/
void PaintBackground(QWidget *pWidget, const QStringList &list)//(QPainter &painter, int width, int height)
{
QPainter painter(pWidget);
int width = pWidget->width();
int height = pWidget->height();
QPixmap pix;
// 四个角贴图
pix.load(list.at(0)); // 左上角
if (pix.isNull())
{
return;
}
painter.drawPixmap(0, 0, pix.width(), pix.height(), pix);
pix.load(list.at(2)); // 右上角
painter.drawPixmap(width-pix.width(), 0, pix.width(), pix.height(), pix);
pix.load(list.at(6)); // 左下角
painter.drawPixmap(0, height-pix.height(), pix.width(), pix.height(), pix);
pix.load(list.at(8)); // 右下角
painter.drawPixmap(width-pix.width(), height-pix.height(), pix.width(), pix.height(), pix);
// 四条边
pix.load(list.at(1)); // 上边
paintRow(pix, painter, pix.width(), 0, width, pix.height());
pix.load(list.at(7)); // 下边
paintRow(pix, painter, pix.width(), height-pix.height(), width, pix.height());
pix.load(list.at(3)); // 左侧边
paintColumn(pix, painter, 0, pix.height(), pix.width(), height);
pix.load(list.at(5)); // 右侧边
paintColumn(pix, painter, width-pix.width(), pix.height(), pix.width(), height);
// 中心区域贴图
pix.load(list.at(4));
int begin = pix.height();
while (begin < height-pix.height())
{
if (height-pix.height()-begin > pix.height())
{
paintRow(pix, painter, pix.width(), begin, width, pix.height());
}
else
{
paintRow(pix, painter, pix.width(), begin, width, height-pix.height()-begin);
}
begin += pix.height();
}
}