您好,登錄后才能下訂單哦!
本篇內容介紹了“Qt如何實現繪制網格背景”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
使用滾輪縮放。
縮放到達一定閾值后恢復網格大小。
窗口大小調整時網格背景也自動調整重繪。
#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
#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(); }
#include "window.h" #include <QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); Window w; w.show(); return a.exec(); }
“Qt如何實現繪制網格背景”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。