1.Qt绘图基础
(1)绘图需画笔和画布:
- QPainter 相当于Qt中的一个画笔,绘制时需要一块画布,
- Qt中扮演画布角色的组件为QPaintDevice和他的各个子类,如: QWidget, QPixmap, QPixture...
- QPainter、QPaintDevice之间使用 QPaintEngine 进行通讯(也就是翻译 QPainter 的指令的意思)
(2)怎么画
- 通常都是要在哪个 widget 上绘图,就需要在它的
paintEvent()
函数里绘图,即重写 paintEvent() 函数
(3)实例
PaintTest.h
1 #ifndef PAINTTEST_H 2 #define PAINTTEST_H 3 4 #include <QWidget> 5 #include <QLabel> 6 #include <QPainter> 7 class PaintTest : public QWidget 8 { 9 Q_OBJECT 10 private: 11 QLabel *la; 12 public: 13 explicit PaintTest(QWidget *parent = 0); 14 void paintEvent(QPaintEvent *); 15 16 17 18 }; 19 #endif // PAINTTEST_H
PaintTest.cpp
1 #include "PaintTest.h" 2 #include <QLabel> 3 #include <QEvent > 4 PaintTest::PaintTest(QWidget *parent) : 5 QWidget(parent),la(new QLabel(this)) 6 { 7 la->setText("PaintTest Label"); 8 la->resize(100,190); 9 10 la->installEventFilter(this); 11 } 12 void PaintTest::paintEvent(QPaintEvent *) { 13 QPainter painter(this); // this 是 PaintTest 的指针 14 painter.setPen(Qt::gray); 15 painter.setBrush(Qt::green); 16 painter.drawRect(10, 10, 50, 50); 17 }
main.cpp
1 #include <QApplication> 2 3 #include "PaintTest.h" 4 5 int main(int argc,char* argv[]) 6 { 7 QApplication a(argc, argv); 8 9 PaintTest pt; 10 pt.show(); 11 12 return a.exec(); 13 }
2.遇到的问题
如果行绘制的图形是在Qwidget的子的组件上应该怎么绘制在哪里绘制?
一般想到的是直接在paintEvent()更改
1 void PaintTest::paintEvent(QPaintEvent *) { 2 // QPainter painter(this); // this 是 PaintTest 的指针 3 QPainter painter(this->la); 4 painter.setPen(Qt::gray); 5 painter.setBrush(Qt::green); 6 painter.drawRect(10, 10, 50, 50); 7 }
运行程序,结果并没有在 Label 上绘制出矩形,而且还输出了下面的错误:
QPainter::begin: Paint device returned engine == 0, type: 1 QPainter::setPen: Painter not active QPainter::setBrush: Painter not active QPainter::drawRects: Painter not active QPainter::begin: Paint device returned engine == 0, type: 1
上面1(2)提到了想要在哪个 widget 上绘图,就需要在它的 paintEvent() 函数里绘图,这里我们是想在这个Qlabel中绘图,但是paintEvent()函数是PainterTest的,所以绘制出了问题绘制不成功。
因此想要在QLabel中绘制图形,就必须新建一个类继承自QLAbel,然后在它的 paintEvent() 里绘图,如果单独就这个问题创建一个新的类就会有点小题大作了
3.解决
在事件过滤器 eventFilter()
中拦截 QLabel 的 QEvent::Paint
事件,用 QLabel 创建 QPainter,就可以在 QLabel 上绘图了
代码为
PaintTest.h
1 #ifndef PAINTTEST_H 2 #define PAINTTEST_H 3 4 #include <QWidget> 5 #include <QLabel> 6 #include <QPainter> 7 class PaintTest : public QWidget 8 { 9 Q_OBJECT 10 private: 11 QLabel *la; 12 public: 13 explicit PaintTest(QWidget *parent = 0); 14 // void paintEvent(QPaintEvent *); 15 bool eventFilter(QObject *watched, QEvent *event) ; 16 void magicTime(); 17 signals: 18 19 public slots: 20 21 }; 22 #endif // PAINTTEST_H
PaintTest.cpp
1 #include "PaintTest.h" 2 #include <QLabel> 3 #include <QEvent > 4 PaintTest::PaintTest(QWidget *parent) : 5 QWidget(parent),la(new QLabel(this)) 6 { 7 //la=new QLabel(this); 8 9 la->setText("PaintTest Label"); 10 la->resize(100,190); 11 12 la->installEventFilter(this); 13 } 14 /*void PaintTest::paintEvent(QPaintEvent *) { 15 QPainter painter(this); // this 是 PaintTest 的指针 16 QPainter painter(this->la); 17 painter.setPen(Qt::gray); 18 painter.setBrush(Qt::green); 19 painter.drawRect(10, 10, 50, 50); 20 }*/ 21 22 bool PaintTest::eventFilter(QObject *watched, QEvent *event) { 23 if (watched == this->la && event->type() == QEvent::Paint) { 24 magicTime(); 25 } 26 27 return QWidget::eventFilter(watched, event); 28 } 29 30 void PaintTest::magicTime() { 31 QPainter painter(this->la ); 32 painter.setPen(Qt::gray); 33 painter.setBrush(Qt::green); 34 painter.drawRect(10, 10, 50, 50); 35 }