zoukankan      html  css  js  c++  java
  • Ubuntu下简单的QT绘图程序

    原文:http://www.linuxidc.com/Linux/2011-08/41220.htm

    当初在学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

    添加如下代码

    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 文件如下        

    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; //存放所有的线段www.linuxidc.com   
    34. };  
    35.   
    36. #endif // MYPAINTERWIDGET_H  

    .cpp 文件如下

    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. 运行结果如下

     

     
  • 相关阅读:
    3D Bezier Curve, Matlab Code
    Matlab Bezier
    python与matlab混合编程
    Matlab P文件——加快Matlab程序,保护你的算法(z)
    Simens NX (原UG)内部代码逆向分析 / Inner code Reverse analysis of NX software
    Matlab扩展编程
    《数值分析及其MATLAB实现》(任玉杰)扫描版[PDF]
    NURBS Toolbox by D.M. Spink (matlab)
    贝塞尔曲线
    BSpline & NURBS (Matlab Code)
  • 原文地址:https://www.cnblogs.com/hahalala/p/8028801.html
Copyright © 2011-2022 走看看