Qt / PyQt 绘制网格背景源码
Posted la_vie_est_belle
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt / PyQt 绘制网格背景源码相关的知识,希望对你有一定的参考价值。
目录
现有功能
- 使用滚轮缩放。
- 缩放到达一定阈值后恢复网格大小。
- 窗口大小调整时网格背景也自动调整重绘。
运行结果
Qt源码
window.h
#ifndef WINDOW_H
#define WINDOW_H
#include <QWidget>
#include <QPen>
#include <QPainter>
#include <QPaintEvent>
class Window : public QWidget
Q_OBJECT
public:
Window(QWidget *parent = nullptr);
~Window();
protected:
void paintEvent(QPaintEvent *event);
void wheelEvent(QWheelEvent *event);
void resizeEvent(QResizeEvent *event);
private:
void setBackgroundColor(); // 设置窗口背景颜色
void drawRowLines(QPainter *painter); // 绘制横线
void drawColLines(QPainter *painter); // 绘制竖线
private:
int minGap; // 最小线间隔
int maxGap; // 最大线间隔
float gap; // 当前线间隔
float startX; // 竖线初始x坐标(从窗口中间开始)
float startY; // 横线初始y坐标(从窗口中间开始)
QPen lightPen; // 细一点的画笔
QPen darkPen; // 粗一点的画笔
;
#endif // WINDOW_H
window.cpp
#include "window.h"
#include <QColor>
#include <QPalette>
#include <QPointF>
Window::Window(QWidget *parent)
: QWidget(parent)
this->minGap = 4;
this->maxGap = 14;
this->gap = (this->minGap + this->maxGap) / 2;
this->startX = this->width() / 2;
this->startY = this->height() / 2;
this->lightPen = QPen(QColor(222, 222, 222));
this->darkPen = QPen(QColor(222, 222, 222));
this->darkPen.setWidth(2);
this->setBackgroundColor();
Window::~Window()
void Window::setBackgroundColor()
QPalette palette;
palette.setColor(QPalette::Background, QColor(250, 250, 250));
this->setPalette(palette);
this->setAutoFillBackground(true);
void Window::drawRowLines(QPainter *painter)
int lineCount = 0;
float biggerY = this->startY;
float smallerY = this->startY;
painter->setPen(this->darkPen);
while (true)
painter->drawLine(QPointF(0.0, biggerY), QPointF(this->width(), biggerY));
painter->drawLine(QPointF(0.0, smallerY), QPointF(this->width(), smallerY));
biggerY += this->gap;
smallerY -= this->gap;
if (smallerY <= 0 || biggerY >= this->height())
break;
// 每间隔一定数量的线,就画一条粗一点的横线
lineCount += 1;
if (lineCount == 10)
painter->setPen(this->darkPen);
lineCount = 0;
else
painter->setPen(this->lightPen);
void Window::drawColLines(QPainter *painter)
int lineCount = 0;
float biggerX = this->startX;
float smallerX = this->startX;
painter->setPen(this->darkPen);
while (true)
painter->drawLine(QPointF(biggerX, 0.0), QPointF(biggerX, this->height()));
painter->drawLine(QPointF(smallerX, 0.0), QPointF(smallerX, this->height()));
biggerX += this->gap;
smallerX -= this->gap;
if (smallerX <= 0 || biggerX >= this->width())
break;
// 每间隔一定数量的线,就画一条粗一点的竖线
lineCount += 1;
if (lineCount == 10)
painter->setPen(this->darkPen);
lineCount = 0;
else
painter->setPen(this->lightPen);
void Window::paintEvent(QPaintEvent *event)
QWidget::paintEvent(event);
QPainter painter(this);
this->drawRowLines(&painter);
this->drawColLines(&painter);
void Window::wheelEvent(QWheelEvent *event)
QWidget::wheelEvent(event);
if (event->angleDelta().y() > 0)
this->gap += 0.1;
else if (event->angleDelta().y() < 0)
this->gap -= 0.1;
if (this->gap >= this->maxGap)
this->gap = this->minGap;
else if (this->gap <= this->minGap)
this->gap = this->maxGap;
this->update();
void Window::resizeEvent(QResizeEvent *event)
QWidget::resizeEvent(event);
this->startX = this->width() / 2;
this->startY = this->height() / 2;
this->update();
main.cpp
#include "window.h"
#include <QApplication>
int main(int argc, char *argv[])
QApplication a(argc, argv);
Window w;
w.show();
return a.exec();
PyQt源码
window.py
import sys
from PyQt5.QtCore import QPointF
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QPainter, QPen, QColor, QPalette
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.resize(600, 400)
self.min_gap = 4 # 最小线间隔
self.max_gap = 14 # 最大线间隔
self.gap = (self.min_gap + self.max_gap) / 2 # 当前线间隔
self.start_x = self.width() / 2 # 竖线初始x坐标(从窗口中间开始)
self.start_y = self.height() / 2 # 横线初始y坐标(从窗口中间开始)
self.light_pen = QPen(QColor(222, 222, 222)) # 两只粗细不同的画笔
self.dark_pen = QPen(QColor(222, 222, 222))
self.dark_pen.setWidth(2)
self.set_background_color()
def set_background_color(self):
"""设置窗口背景颜色"""
palette = QPalette()
palette.setColor(QPalette.Background, QColor(250, 250, 250))
self.setPalette(palette)
self.setAutoFillBackground(True)
def draw_row_lines(self, painter):
"""画横线"""
line_count = 0
bigger_y = self.start_y
smaller_y = self.start_y
painter.setPen(self.dark_pen)
while True:
painter.drawLine(QPointF(0.0, bigger_y), QPointF(self.width(), bigger_y))
painter.drawLine(QPointF(0.0, smaller_y), QPointF(self.width(), smaller_y))
bigger_y += self.gap
smaller_y -= self.gap
if smaller_y <= 0 or bigger_y >= self.height():
break
# 每间隔一定数量的线,就画一条粗一点的横线
line_count += 1
if line_count == 10:
painter.setPen(self.dark_pen)
line_count = 0
else:
painter.setPen(self.light_pen)
def draw_col_lines(self, painter):
"""画竖线"""
line_count = 0
bigger_x = self.start_x
smaller_x = self.start_x
painter.setPen(self.dark_pen)
while True:
painter.drawLine(QPointF(bigger_x, 0.0), QPointF(bigger_x, self.height()))
painter.drawLine(QPointF(smaller_x, 0.0), QPointF(smaller_x, self.height()))
bigger_x += self.gap
smaller_x -= self.gap
if smaller_x <= 0 or bigger_x >= self.width():
break
# 每间隔一定数量的线,就画一条粗一点的竖线
line_count += 1
if line_count == 10:
painter.setPen(self.dark_pen)
line_count = 0
else:
painter.setPen(self.light_pen)
def paintEvent(self, event):
super(Window, self).paintEvent(event)
painter = QPainter(self)
self.draw_row_lines(painter)
self.draw_col_lines(painter)
def wheelEvent(self, event):
"""滚轮控制线间隔"""
super(Window, self).wheelEvent(event)
if event.angleDelta().y() > 0:
self.gap += 0.1
elif event.angleDelta().y() < 0:
self.gap -= 0.1
if self.gap >= self.max_gap:
self.gap = self.min_gap
elif self.gap <= self.min_gap:
self.gap = self.max_gap
self.update()
def resizeEvent(self, event):
super(Window, self).resizeEvent(event)
self.start_x = self.width() / 2
self.start_y = self.height() / 2
self.update()
if __name__ == '__main__':
app = QApplication([])
area = Window()
area.show()
sys.exit(app.exec())
github地址
GitHub - la-vie-est-belle/Qt-PyQt
以上是关于Qt / PyQt 绘制网格背景源码的主要内容,如果未能解决你的问题,请参考以下文章