Qt realization draw on QLabel

Qt that he could not draw on any control, because Qt event filter to control the drawing events filtered.

If you want to draw on the controls and how should I do it, I QLabel to this example: using an event filter to capture the target QLabel QEvent :: Paint event. Function does not require drawing on paintEvent () function can also be implemented drawing. The main function is to achieve this function on two important functions or event filters, that installEventFilte () and eventFileter ().

First create QtGui application, the project name is "labelPaint", select the class name "QWidget", other options remain the default. Add the QLabel labelPaint.ui a file, label style sheet to facilitate observation of the background becomes black, and then modify labelPaint.cpp following documents (labelPaint.h file content not posted):

#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 *obj, QEvent *event)
{
    if(obj==ui->label && event->type()==QEvent::Paint)
        labelPaint();

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

void Widget::labelPaint()
{
    QPainter painter(ui->label);
    painter.setPen(Qt::blue);
    painter.drawEllipse(25, 25, 50, 50);
}


Results are as follows:


reference:

Drawing on the QT achieve QLabel


Guess you like

Origin www.cnblogs.com/linuxAndMcu/p/11263902.html