Qt如何实现绘制网格背景
短信预约 -IT技能 免费直播动态提醒
本篇内容介绍了“Qt如何实现绘制网格背景”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
现有功能
使用滚轮缩放。
缩放到达一定阈值后恢复网格大小。
窗口大小调整时网格背景也自动调整重绘。
运行结果
源码
window.h
#ifndef WINDOW_H#define WINDOW_H#include <QWidget>#include <QPen>#include <QPainter>#include <QPaintEvent>class Window : public QWidget{ Q_OBJECTpublic: 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();}
“Qt如何实现绘制网格背景”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341