注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

一路

To find the final symmetry & beauty

 
 
 

日志

 
 
 
 

Qt实现的简单图形学学习小工具类  

2011-06-14 23:58:01|  分类: source code |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

简单的栅格显示与同步放大量示类,可以把窗口存为图片。效果如下。

Qt实现的简单图形学学习小工具类 - saturnman - 一路
 

 

 

 

 

//filename screen.h

//created by saturnnman 20110520

#ifndef SCREEN_H

#define SCREEN_H

#include <QWidget>

#include <QColor>

#include <QImage>

#include <QString>

#include <QRect>

#include "ui_screen.h"

 

class Screen : public QWidget

{

    Q_OBJECT

public:

    const static int WINDOW_WIDTH = 800;

    const static int WINDOW_HEIGHT = 600;

    const static int LEFT_EDGE = 10;

    const static int RIGHT_EDGE = 10;

    const static int UPPER_EDGE = 10;

    const static int LOWER_EDGE = 10;

public:

    Screen(QWidget *parent = 0);

    QSize minimumSize();

    QSize sizeHint() const;

    void setPenColor(const QColor& newColor);  //set pen color for next drawing

    QColor penColor() const {return curColor;} //get current pen color

    void setImage(const QImage& newImage);     //set image display

    QImage getImage() const {return curImage;} //get image display

    bool save(const QString& filename)const;   //set image to file

    void setPixel(int x,int y);                //set pixel valued

    ~Screen();

protected:

    void paintEvent(QPaintEvent* event);

private:

    Ui::Screen ui;

    QImage curImage;

    QColor curColor;

private:

    QRect pixelRect(int x,int y)const;

};

 

#endif // SCREEN_H

 

 

//filename screen.cpp

//created by saturnnman 20110520

#include<QPainter>

#include<QRect>

#include<QPaintEvent>

#include "screen.h"

Screen::Screen(QWidget *parent)

    : QWidget(parent)

{

    ui.setupUi(this);

    setAttribute(Qt::WA_StaticContents);

    setSizePolicy(QSizePolicy::Minimum,QSizePolicy::Minimum);

    this->curColor = Qt::black;

    curImage = QImage(100,100,QImage::Format_ARGB32);

    curImage.fill(qRgba(255,255,255,255));

}

QSize Screen::minimumSize()

{

    return QSize(WINDOW_WIDTH,WINDOW_HEIGHT);

}

QSize Screen::sizeHint()const

{

    return QSize(WINDOW_WIDTH,WINDOW_HEIGHT);

}

void Screen::setPenColor(const QColor& newColor)

{

    this->curColor = newColor;

}

void Screen::setImage(const QImage& newImage)

{

    if(curImage != newImage)

    {

        this->curImage=newImage.convertToFormat(QImage::Format_ARGB32);

        update();

        updateGeometry();

    }

}

void Screen::setPixel(int x,int y)

{

    QPainter painter(this);

    painter.setPen(curColor);

    if(x<0 || x>curImage.width() || y<0 || y>curImage.height())

    {

        return ;

    }

    this->curImage.setPixel(x,y,penColor().rgba());

    painter.fillRect(pixelRect(x,y),curColor);

}

bool Screen::save(const QString& filename) const

{

    return this->curImage.save(filename);

}

QRect Screen::pixelRect(int x,int y)const

{

    return QRect(LEFT_EDGE+5*x+1,UPPER_EDGE+5*y+1,4,4);

}

void Screen::paintEvent(QPaintEvent * event)

{

    this->resize(this->minimumSize());

    QPainter painter(this);

    this->curColor = Qt::black;

    QRect border = QRect(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);

    QRect pad = QRect(1,1,WINDOW_WIDTH-2,WINDOW_HEIGHT-2);

    QColor black = Qt::black;

    QColor white = Qt::white;

    painter.fillRect(border,black);

    painter.fillRect(pad,white);

    for(int i=0;i<=curImage.width();++i)

    {

        painter.drawLine(LEFT_EDGE+5*i,UPPER_EDGE+0,LEFT_EDGE+5*i,UPPER_EDGE+5*curImage.height());

    }

    for(int i=0;i<=curImage.height();++i)

    {

        painter.drawLine(LEFT_EDGE+0,i*5+UPPER_EDGE,5*curImage.width()+LEFT_EDGE,i*5+UPPER_EDGE);

    }

    for(int i=0;i<curImage.width();++i)

    {

        for(int j=0;j<curImage.height();++j)

        {

            QRect rect = pixelRect(i,j);

            if(!event->region().intersect(rect).isEmpty())

            {

                QColor color = QColor::fromRgba(curImage.pixel(i,j));

                painter.fillRect(rect,color);

            }

        }

    }

    painter.drawLine(LEFT_EDGE+5*curImage.width()+5,UPPER_EDGE+5,LEFT_EDGE+5*curImage.width()+5,UPPER_EDGE+curImage.height()+5);

    painter.drawLine(LEFT_EDGE+5*curImage.width()+5,UPPER_EDGE+5,LEFT_EDGE+5*curImage.width()+5+curImage.width()+5,UPPER_EDGE+5);

    painter.drawImage(QPoint(LEFT_EDGE+5*curImage.width()+10,UPPER_EDGE+10),curImage);

}

Screen::~Screen()

{

 

}

 

  评论这张
 
阅读(940)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017