1. 程式人生 > >Ubuntu下簡單的QT繪圖程序

Ubuntu下簡單的QT繪圖程序

部件 str repaint www. his max 起點 結果 user

原文: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. int main(int argc,char** argv)
  4. {
  5. QApplication a(argc,argv);
  6. MyPainterWidget w(0);
  7. w.show();
  8. return a.exec();
  9. }

這裏的MyPainterWidget類是我們自己編寫的QWidget類的子類,用來實現繪制的窗口部件。

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

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

.h 文件如下

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

.cpp 文件如下

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

3. 運行結果如下

技術分享圖片

Ubuntu下簡單的QT繪圖程序