簡單的QT繪圖程序(把全部的點都記錄下來,然后在paintEvent里使用drawLine函數進行繪制,貌似效率很低。。。)


[cpp]  view plain  copy
 
  1.   
當初在學MFC時,最經典的入門實例就是繪圖程序,其作用相當於Console Application 下的Hello World了吧。

 

如今入手QT,不免懷舊,於是也寫了一個繪圖程序,雖然簡單,卻也是入門必備啊。

 

環境

OS : Ubuntu 11.04

IDE :Qt Creator 2.2.1 

Qt : 4.7.4 (32bit)  

Complier: gcc 

 

1.  新建一個空白Qt工程

     文件--> 新建工程或項目-->其它項目-->空的Qt項目

     比如命名為Qt_Instance_Example

 

2. 添加一個C++源文件

    比如命名為main.cpp

    添加如下代碼

 

[html]  view plain  copy
 
  1. #include <QApplication>  
  2. #include <mypainterwidget.h>  
  3.   
  4. int main(int argc,char** argv)  
  5. {  
  6.     QApplication a(argc,argv);  
  7.   
  8.     MyPainterWidget w(0);  
  9.     w.show();  
  10.     return a.exec();  
  11. }  
這里的MyPainterWidget類是我們自己編寫的QWidget類的子類,用來實現繪制的窗口部件。

 

     下面我們添加這個類並編寫其代碼。

3.  添加C++類,命名為MyPainterWidget

     .h 文件如下         

[cpp]  view plain  copy
 
  1. #ifndef MYPAINTERWIDGET_H  
  2. #define MYPAINTERWIDGET_H  
  3.   
  4. #include <QWidget>  
  5. #include <QPoint>  
  6. #include<vector>  
  7.   
  8. using namespace std;  
  9.   
  10.   
  11. //線段  
  12. typedef struct myLine{  
  13.     QPoint startPnt;  
  14.     QPoint endPnt;  
  15. }myLine;  
  16.   
  17. class MyPainterWidget: public QWidget  
  18. {  
  19. public:  
  20.     MyPainterWidget(QWidget* parent);  
  21.     ~MyPainterWidget();  
  22.   
  23.     //繼承  
  24.     void paintEvent(QPaintEvent* p);  
  25.     void mousePressEvent(QMouseEvent *e);  
  26.     void mouseMoveEvent(QMouseEvent *e);  
  27.     void mouseReleaseEvent(QMouseEvent *e);  
  28.   
  29.     QPoint startPnt;   //起點  
  30.     QPoint endPnt;     //終點  
  31.     bool isPressed;    //鼠標是否按下  
  32.   
  33.     vector<myLine*> lines; //存放所有的線段  
  34. };  
  35.   
  36. #endif // MYPAINTERWIDGET_H  
   .cpp 文件如下

 

 

[cpp]  view plain  copy
 
  1. #include "mypainterwidget.h"  
  2. #include <QString>  
  3. #include <QMessageBox>  
  4. #include <QPainter>  
  5. #include <QPen>  
  6. #include <QMouseEvent>  
  7.   
  8.   
  9. MyPainterWidget::MyPainterWidget(QWidget* parent)  
  10.      :QWidget(parent){  
  11.     setMinimumSize(240,120);  
  12.     setMaximumSize(480,240);  
  13.     this->setMouseTracking(true);  
  14.     this->isPressed = false;  
  15. }  
  16.   
  17. MyPainterWidget::~MyPainterWidget(){  
  18.   
  19. }  
  20.   
  21. void MyPainterWidget::paintEvent(QPaintEvent*p){  
  22.     QPainter painter(this);  
  23.     QPen pen;                                 //創建一個畫筆  
  24.     pen.setColor(Qt::darkCyan);  
  25.     pen.setWidth(5);  
  26.     painter.setPen(pen);  
  27.   
  28.     for(int i = 0;i<lines.size();i++){  
  29.         myLine* pLine = lines[i];  
  30.         painter.drawLine(pLine->startPnt,pLine->endPnt);  
  31.     }  
  32. }  
  33.   
  34. void MyPainterWidget::mousePressEvent(QMouseEvent *e){  
  35.     setCursor(Qt::PointingHandCursor);  
  36.     startPnt = e->pos();  
  37.     endPnt = e->pos();  
  38.     this->isPressed = true;  
  39.     //QString msg ="("+QString::number(e->x())+","+QString::number(e->y())+")";  
  40.     //QMessageBox::warning(this,tr("Warning"),msg,QMessageBox::Ok);  
  41. }  
  42.   
  43. void MyPainterWidget::mouseMoveEvent(QMouseEvent *e){  
  44.     if(this->isPressed){  
  45.         endPnt = e->pos();  
  46.   
  47.         myLine* line = new myLine;  //put the new line into vector  
  48.         line->startPnt = startPnt;  
  49.         line->endPnt = endPnt;  
  50.         this->lines.push_back(line);  
  51.   
  52.         update();                                    //repainter,call paintEvent  
  53.         startPnt = endPnt;  
  54.     }  
  55. }  
  56.   
  57. void MyPainterWidget::mouseReleaseEvent(QMouseEvent *e){  
  58.     setCursor(Qt::ArrowCursor);  
  59.     this->isPressed = false;  
  60. }  

3. 運行結果如下

 

 

http://blog.csdn.net/jarvischu/article/details/6705127


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM