QT 绘制像unity中状态模快的网格背景图

QT 绘制像unity中状态模快的网格背景图_第1张图片

 效果如上:

代码如下:

代码中的图片是一张1024*1024的纯黑色背景图

void XXXXScene::drawBackground(QPainter *painter, const QRectF &rect)
{
        int gridW;
		int gridH;
		int startX;
		int startY;

		gridW = 15;
		gridH = 15;
		startX = rect.x();
		startY = rect.y();

        // static const QPixmap pixBackGround = QPixmap(":/images/background.png");
        QString strPixmapPath = ":/images/background.png";   //背景图
		painter->drawPixmap(QRect(startX, startY, 4000, 4000), QPixmap(strPixmapPath));

		QPen pen2(QColor(34, 34, 34));   //虚线
		pen2.setWidth(1);
		painter->setPen(pen2);

		QPen pen3(QColor(25, 25, 25));  //实线
		pen3.setWidth(1);

		for (int i = 0; i < 300; i++)
		{
			if (i % 10 == 0)
			{
				painter->setPen(pen3);
			}
			else
			{
				painter->setPen(pen2);
			}
			painter->drawLine(startX, startY + i * gridH, gridW * 1000 + startX, startY + i * gridH);		//画横线
			painter->drawLine(startX + gridW * i, startY, startX + i * gridW, startY + gridH * 1000);       //画竖线
		}

		painter->drawRect(rect);

		QPixmap pix(10, 10);
		pix.fill(Qt::transparent);
		setBackgroundBrush(pix);
}

你可能感兴趣的:(qt,qt,开发语言)