QT draws a grid background image like the state model in unity

 The effect is as follows:

code show as below:

The picture in the code is a 1024*1024 pure black background picture

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);
}

Guess you like

Origin blog.csdn.net/qq_30377315/article/details/128344438