Qt图形界面的相关类

Qt中与图形界面相关常见的类.


标签 : Qt基础

[TOC]

1.几个常用的类

1.1 QPainter class

The QPainter class performs low-level painting on widgets and other paint devices.

QPainter provides highly optimized functions to do most of the drawing GUI programs require. It can draw everything from simple lines to complex shapes like pies and chords. It can also draw aligned text and pixmaps. Normally, it draws in a "natural" coordinate system, but it can also do view and world transformation. QPainter can operate on any object that inherits the QPaintDevice class.

它主要是提供了一些绘图的方法, 譬如画点,画线,画圆##

常用方法##

    drawArc:画弧线
    drawChord:画弦
    drawConvexPolygon:画凸多边形
    drawEllipse:画椭圆
    drawImage:画QImage表示的图
    drawLine:画线
    drawPath:画路径
    drawPicture:画QPicture表示的图
    drawPixmap:画QPixmap的图
    drawPoint:画点表示
    drawPolygon:画多边形
    drawText:画文字

1.2 QPaintDevice class

提供了QPainter的绘图设备
    QWidget、QImage、QPrinter等等绘图场景都是从    QPaintDevice继承出来的。

1.3 QPainEigine

QPaintEngine对程序员不透明,提供了不同类型设备的接口,由QPaintDevice和QPainter与其进行交互。

1.4 QPen, QBrush

The QPen class defines how a QPainter should draw lines and outlines of shapes.

A pen has a style(), width(), brush(), capStyle() and joinStyle().

画笔和画刷的使用方法##

    //Pen:
    void QPainter::setPen(const QPen & pen)
    void QPainter::setPen(const QColor & color)
    void QPainter::setPen(Qt::PenStyle style)
    //Brush
 //画刷样式
    //  设置方式
    void QPen::setBrush(const QBrush & brush)
    void QPainter::setBrush(const QBrush & brush)
    
    //绘图事件处理函数
    //
    //A paint event is a request to repaint all or part of a widget. It can happen for one of the following reasons:

    //1.repaint() or update() was invoked,
    //2. the widget was obscured and has now been  uncovered, or
    //3.many other reasons.
    void QWidget::paintEvent(QPaintEvent * event)
    //当窗口或是部件需要进行绘制的时候,就是触发一个绘图事件,只要重写事件处理函数,就可以定制图形绘制。
    void QWidget::repaint()
    void QWidget::repaint(int x, int y, int w, int h)
    void QWidget::repaint(const QRect & rect)
    void QWidget::repaint(const QRegion & rgn)

//repaint()可以使paintEvent被调用

//刷新
    update()
    //update()允许Qt来优化速度,并防止闪烁.
    

Note that : 我们每次重新绘图之后,需要调用 repain() 或者是 update来更新画板的图形.

1.5 项目实战 [ 实现画板 ]

//paintarea.cpp

//画板工具实现中包含三要素, 1.画者QPainter, 2.画板设备, 3.画板的控制
//**paintarea.cpp**
#ifndef PAINTAREA_H
#define PAINTAREA_H

#include <QWidget>
#include <QPen>
#include <QBrush>

class PaintArea : public QWidget
{
    Q_OBJECT
public:
    explicit PaintArea(QWidget *parent = 0);
    enum Shape {
        Line,Rectangle,Pixmap,Ellipse
    };
    void setPen(QPen pen);
    void setBrush(QBrush brush);
    void setShape(Shape shape);

signals:

public slots:

protected:
    void paintEvent(QPaintEvent * event);   //重写QWidget中的paintEvent接口,来汇出不同的图形,由此可以知道QWidget也是继承了QPaintDevice

private:
    QPen pen;       //画笔
    QBrush brush;   //画刷
    Shape shape;    //要画的形状
};

#endif // PAINTAREA_H
#include "paintarea.h"
#include <QPainter>
#include <QDebug>

PaintArea::PaintArea(QWidget *parent) : QWidget(parent)
{
    //构造函数先将这块画的区域设置为白色.
    this->setPalette(QPalette(QColor(Qt::white)));
    this->setAutoFillBackground(true);
}

void PaintArea::paintEvent(QPaintEvent * event)
{
    QPainter p(this);
    p.setPen(pen);
    p.setBrush(brush);

    QRect rect(20, 20, 100, 200);

    //根据不同的图形画不同的图形.
    switch(shape) {
    case Line:
        p.drawLine(QPointF(50,50),QPointF(200,200));
        break;
    case Rectangle :
        p.drawRect(rect);
        break;
    case Pixmap :
        p.drawPixmap(100,100,QPixmap("://images/butterfly.png"));
        qDebug() << "." <<endl;
        break;
    case Ellipse:
        p.drawEllipse(rect);
        break;
    default:
        break;
    }
}

void PaintArea::setPen(QPen pen)
{
    this->pen = pen;
    update();   //用于触发一个重绘事件,调用paintEvent函数.
}
void PaintArea::setBrush(QBrush brush)
{
    this->brush = brush;
    update();
}
void PaintArea::setShape(Shape shape)
{
    this->shape = shape;
    update();
}

在Widget中根据不同的槽函数作相应的处理即可.

一般我们设置某个Widget[或者是其子类]的背景色的一般方法是:

        setPalette(QPalette(Qcolor(color)));    //设置调色板
        setAutofillBackground(true);            //设置自动填充

实现效果如下[1]:

2016-12-17_165340.png

1.6 Qt中的坐标系统

Qt图形坐标.png
//QPainter中与坐标系统相关的几个函数.
class QPainter::
{
    rodate();  //顺时针旋转.
    translate(); //偏移(相对)
    scale(); //扩大坐标系
    shear(); //扭曲标准坐标系,一般只修饰一个坐标
    
    save();
    restore(); //注意save和restore是成对出现的.恢复到save时的坐标状态.
}

a. redate(30); 使用实例

2016-12-17_185939.png

)

b. p.translate(50,50);使用实例 [即是把坐标系相对偏移(50,50)]

2016-12-17_190557.png

p.translate(0,50);

2016-12-17_191136.png

c. p.scale(2,3); //从当前开始尺度坐标系 x * 2, y * 3

![Uploading 2016-12-17_192155_135289.png . . .]

d. p.shear(0,1); 使用示例

save();和restore();的函数使用就不啰嗦了.


  1. 简易画板, 只能画特定的图形 = =

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,797评论 0 23
  • 之前在上海一家自闭症机构实习的时候认识的一个同事 年龄相仿 但是她的经历可比我丰富多了 相隔短短的一年多时间 她独...
    素靥阅读 233评论 0 0
  • 现在无论在电脑上还是手机端阅读txt的小说非常方便,比如下载一个QQ阅读之类的软件就可以实现书签和翻页功能, 那么...
    bigtrace阅读 3,964评论 0 3
  • 这几天我一直在想,我想我是喜欢他的。 最近总是强行让自己忘掉想要去找他的念头,可越是这样,他的脸越清晰的浮现在我面...
    轻绾阅读 818评论 5 15