1. 程式人生 > 其它 >Qt 繪製貝塞爾曲線例程

Qt 繪製貝塞爾曲線例程

main.cpp

 1 #include <QApplication>  
 2 #include "mywidget.h"  
 3   
 4 int main(int argc, char *argv[])  
 5 {  
 6     QApplication a(argc, argv);  
 7   
 8     MyWidget w;  
 9     w.resize(300, 300);  
10     w.show();  
11   
12     return a.exec();  
13 }  

mywidget.h

 1 #ifndef MYWIDGET_H  
2 #define MYWIDGET_H 3 4 #include <QWidget> 5 #include <QLabel> 6 #include <QMouseEvent> 7 #include <QLabel> 8 9 class MyWidget : public QWidget 10 { 11 Q_OBJECT 12 public: 13 explicit MyWidget(QWidget *parent = 0); 14 15 QLabel *startLabel;
16 QLabel *ctrlLabel1; 17 QLabel *ctrlLabel2; 18 QLabel *endLabel; 19 20 QLabel *ctrlLabel3; 21 QLabel *ctrlLabel4; 22 QLabel *endLabel2; 23 24 QWidget *selectedWidget; 25 26 private: 27 void paintEvent(QPaintEvent *event); 28 void mousePressEvent(QMouseEvent *event
); 29 void mouseMoveEvent(QMouseEvent *event); 30 void mouseReleaseEvent(QMouseEvent *event); 31 32 }; 33 34 #endif // MYWIDGET_H

mywidget.cpp

 1 #include "mywidget.h"  
 2 #include <QPainter>  
 3 #include <QPainterPath>  
 4 #include <QDebug>  
 5   
 6 MyWidget::MyWidget(QWidget *parent) :  
 7     QWidget(parent)  
 8 {  
 9   
10     startLabel=new QLabel(this);  
11     startLabel->resize(10, 10);  
12     startLabel->setStyleSheet("QLabel{background-color:red;}");  
13     startLabel->move(100, 110);  
14   
15     ctrlLabel1=new QLabel(this);  
16     ctrlLabel1->resize(10, 10);  
17     ctrlLabel1->setStyleSheet("QLabel{background-color:green;}");  
18     ctrlLabel1->move(120, 120);  
19   
20     ctrlLabel2=new QLabel(this);  
21     ctrlLabel2->resize(10, 10);  
22     ctrlLabel2->setStyleSheet("QLabel{background-color:blue;}");  
23     ctrlLabel2->move(140, 130);  
24   
25     endLabel=new QLabel(this);  
26     endLabel->resize(10, 10);  
27     endLabel->setStyleSheet("QLabel{background-color:black;}");  
28     endLabel->move(160, 140);  
29   
30     ctrlLabel3=new QLabel(this);  
31     ctrlLabel3->resize(10, 10);  
32     ctrlLabel3->setStyleSheet("QLabel{background-color:yellow;}");  
33     ctrlLabel3->move(180, 150);  
34   
35     ctrlLabel4=new QLabel(this);  
36     ctrlLabel4->resize(10, 10);  
37     ctrlLabel4->setStyleSheet("QLabel{background-color:cyan;}");  
38     ctrlLabel4->move(200, 160);  
39   
40     endLabel2=new QLabel(this);  
41     endLabel2->resize(10, 10);  
42     endLabel2->setStyleSheet("QLabel{background-color:#AAAAEE;}");  
43     endLabel2->move(220, 170);  
44   
45     selectedWidget=NULL;  
46 }  
47   
48   
49 void MyWidget::paintEvent(QPaintEvent *event)  
50 {  
51     QPainter painter(this);  
52     painter.setPen(Qt::black);  
53     painter.drawLine(startLabel->pos(), ctrlLabel1->pos());  
54     painter.drawLine(ctrlLabel2->pos(), endLabel->pos());  
55     painter.drawLine(endLabel->pos(), ctrlLabel3->pos());  
56     painter.drawLine(ctrlLabel4->pos(), endLabel2->pos());  
57   
58     QPainterPath myPath;  
59     myPath.moveTo(startLabel->pos());  
60     myPath.cubicTo(ctrlLabel1->pos(), ctrlLabel2->pos(), endLabel->pos());  
61     myPath.cubicTo(ctrlLabel3->pos(), ctrlLabel4->pos(), endLabel2->pos());  
62     painter.setPen(Qt::red);  
63     painter.drawPath(myPath);  
64 }  
65   
66   
67 void MyWidget::mousePressEvent(QMouseEvent *event)  
68 {  
69     QWidget *w=this->childAt(event->pos());  
70     if(w==NULL) return;  
71     if(w->inherits("QLabel")){  
72         selectedWidget=w;  
73     }  
74 }  
75   
76 void MyWidget::mouseReleaseEvent(QMouseEvent *event)  
77 {  
78     selectedWidget=NULL;  
79 }  
80   
81 void MyWidget::mouseMoveEvent(QMouseEvent *event)  
82 {  
83     if(selectedWidget!=NULL){  
84         selectedWidget->move(event->pos());  
85         this->update();  
86     }  
87 }