我设计的界面:窗口中央的label属于从QLabel提升的QPaintLabel类
运行时的情况:并没有显示label
请问组件提升后不显示,是什么原因呢?
本人刚学QT,有所不知,还请大神相助。
QPaintLabel类的代码如下
## QPaintLabel.h
#pragma once
#include <qlabel.h>
#include <QtWidgets/QScrollArea>
#include <qpainter.h>
#include <QMouseEvent>
#include <QtWidgets/QMainWindow>
class QPaintLabel: public QLabel
{
Q_OBJECT
private:
QList<QRect> rects; //记录点的坐标大小
public:
QPaintLabel(QWidget* parent = 0) :QLabel(parent) //构造函数
{
}
~QPaintLabel() {}
protected:
void mousePressEvent(QMouseEvent* event); //鼠标点击,画圆
void mouseMoveEvent(QMouseEvent* event); //鼠标移动,连续画
void paintEvent(QPaintEvent* event) ; //画圆
};
## QPaintLabel.c
void QPaintLabel::paintEvent(QPaintEvent*)
{
QPainter painter(this);
QList<QRect>::iterator it;
for (it=rects.begin();it<rects.end();it++)
{
painter.drawEllipse(*it); //画圆
}
painter.end();
}
void QPaintLabel::mousePressEvent(QMouseEvent* event)
{
QPoint point;
if (event->button() == Qt::LeftButton)
{
point.setX(event->x());
point.setY(event->y());
rects.append(QRect(QPoint(point), QSize(10, 10)));
this->repaint();
}
}
void QPaintLabel::mouseMoveEvent(QMouseEvent* event)
{
QPoint point;
point.setX(event->x());
point.setY(event->y());
rects.append(QRect(QPoint(point), QSize(10, 10)));
this->repaint();
}