欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Qt實現繪制網格背景的示例代碼

 更新時間:2022年06月07日 14:06:01   作者:la_vie_est_belle  
這篇文章主要介紹了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();
}

以上就是Qt實現繪制網格背景的示例代碼的詳細內容,更多關于Qt繪制網格背景的資料請關注腳本之家其它相關文章!

相關文章

  • C++多態(tài)特性之派生與虛函數與模板詳細介紹

    C++多態(tài)特性之派生與虛函數與模板詳細介紹

    這篇文章主要介紹了C++多態(tài)的特性派生與虛函數與模板,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-09-09
  • C++/C 回文字符串的實例詳解

    C++/C 回文字符串的實例詳解

    這篇文章主要介紹了C++ 回文字符串的實例詳解的相關資料,需要的朋友可以參考下
    2017-07-07
  • 虛函數表-C++多態(tài)的實現原理解析

    虛函數表-C++多態(tài)的實現原理解析

    這篇文章主要介紹了虛函數表-C++多態(tài)的實現原理,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • 淺談c++性能測試工具google benchmark

    淺談c++性能測試工具google benchmark

    本文將會介紹如何使用模板以及參數生成器來批量生成測試用例,簡化繁瑣的性能測試代碼
    2021-06-06
  • C++ 實現線程安全的頻率限制器(推薦)

    C++ 實現線程安全的頻率限制器(推薦)

    這篇文章主要介紹了在 C++ 中實現一個線程安全的頻率限制器,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-05-05
  • MFC中exe圖標修改的方法

    MFC中exe圖標修改的方法

    修改窗口標題圖標可通過導入圖標,然后在CMainFrame.:OnCreate函數中加載圖標即可, 代碼如下:
    2013-04-04
  • C++實現簡易圖書館管理系統(tǒng)

    C++實現簡易圖書館管理系統(tǒng)

    這篇文章主要為大家詳細介紹了C++實現簡易圖書館管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 深入探究C++ string的內部究竟是什么樣的

    深入探究C++ string的內部究竟是什么樣的

    這篇文章主要給大家介紹了關于C++ string的內部究竟是什么樣的,文中通過示例代碼的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-01-01
  • Qt5連接并操作PostgreSQL數據庫的實現示例

    Qt5連接并操作PostgreSQL數據庫的實現示例

    本文主要介紹了Qt5連接并操作PostgreSQL數據庫的實現示例,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 詳解C++編程中的sizeof運算符與typeid運算符

    詳解C++編程中的sizeof運算符與typeid運算符

    這篇文章主要介紹了C++編程中的sizeof運算符與typeid運算符,是C++入門學習中的基礎知識,需要的朋友可以參考下
    2016-01-01

最新評論