码迷,mamicode.com
首页 > 其他好文 > 详细

QT 实现在QLabel上画图

时间:2018-04-12 22:17:25      阅读:2977      评论:0      收藏:0      [点我收藏+]

标签:this   raw   let   .com   UI   子类   alt   inter   img   

QT之所以不能再任意控件上绘图是因为QT的事件过滤器把控件的绘图事件给过滤了。

在paintevent()函数中,通常需要设置QPainter对象,创建QPainter对象的同时需要指定绘图设备,即继承自QPainterDevice的子类作为绘图设备,绘制出来的图形将在这个设备上进行显示,Qt一共提供了4个这样的类,分别是QPixmap,QBitmap,QImage,和QPicture。

在控件上绘图又应该怎么办呢,我这以QLabel为例:使用事件过滤器来使QLabel对象捕获QEvent::Paint事件。即,绘图函数不需要放在paintevent()函数中也可以实现绘图。 实现这个功能的主要函数就还是事件过滤器的两个重要函数,即installEventFilter()和eventFileter()#include "widget.h"

#include "ui_widget.h"

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

    ui->label->installEventFilter(this); //这行不能省
}

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

bool Widget::eventFilter(QObject *watched, QEvent *event)   //用过滤器eventFilter()拦截QLabel中的QEvent::Paint事件
{
    if(watched ==ui->label && event->type() == QEvent::Paint)
        paint();

    return QWidget::eventFilter(watched,event);
}

void Widget::paint()     //绘图
{
    QPainter painter(ui->label);
    painter.setPen(Qt::blue);
   // painter.drawLine(100,100,200,200);
    painter.drawEllipse(30,15,50,65);
    painter.drawLine(0,100,111,100);

}

我在ui里添加了QLabel控件命名label,并且用样式表将label的背景变成黑色方便观察,下面是效果图

技术分享图片

 

QT 实现在QLabel上画图

标签:this   raw   let   .com   UI   子类   alt   inter   img   

原文地址:https://www.cnblogs.com/xiaolanchong/p/8810372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!