Qt绘制网格

Qt绘制网格

mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPainter>
#include <QDebug>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    protected:
    void paintEvent(QPaintEvent *);

private:
    Ui::MainWindow *ui;

    
};

#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    

}

MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::paintEvent(QPaintEvent *)
{
    /*
    //当你运行程序时,该函数会被自动调用,前面重绘函数部分已经说过了哦。
    //现在我们第一步是要有画笔,我们建立一个画笔对象
    QPainter painter(this);
    //此时painter就是我们自己的画笔了,我们开始画画,现在我要先画一个点,QPainter 给我们提供了丰富的方法,画一个点void drawPoint(int x, int y)
    painter.drawPoint(100,100);//
    //t同理直线
    // void drawLine(const QPoint &p1,const QPoint $p2)
    painter.drawLine(QPoint(1,1),QPoint(100,101));//QPoint代表什么,如果你是初学者百度QPoint,百度大神会详细的告诉你

    painter.drawRect(1,1,100,100);//绘制矩形

    */

    int win_width = this->geometry().width();
    int win_height = this->geometry().height();

    QPainter painter(this);

    qDebug()<<"win size:"<<win_width<<"   "<<win_height;



    for(int x=0;x<win_width;x+=25)
    {
        painter.drawLine(QPoint(x,1),QPoint(x,win_height));
    }

    for(int y = 0;y<win_height;y+=25)
    {
        painter.drawLine(QPoint(1,y),QPoint(win_width,y));
    }


}



/////////////////////

###########################

QQ 3087438119
原文地址:https://www.cnblogs.com/herd/p/14724286.html