簡單的QT繪圖程式

來源:互聯網
上載者:User

當初在學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

    添加如下代碼

#include <QApplication>#include <mypainterwidget.h>int main(int argc,char** argv){    QApplication a(argc,argv);    MyPainterWidget w(0);    w.show();    return a.exec();}

這裡的MyPainterWidget類是我們自己編寫的QWidget類的子類,用來實現繪製的視窗組件。

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

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

     .h 檔案如下         

#ifndef MYPAINTERWIDGET_H#define MYPAINTERWIDGET_H#include <QWidget>#include <QPoint>#include<vector>using namespace std;//線段typedef struct myLine{    QPoint startPnt;    QPoint endPnt;}myLine;class MyPainterWidget: public QWidget{public:    MyPainterWidget(QWidget* parent);    ~MyPainterWidget();    //繼承    void paintEvent(QPaintEvent* p);    void mousePressEvent(QMouseEvent *e);    void mouseMoveEvent(QMouseEvent *e);    void mouseReleaseEvent(QMouseEvent *e);    QPoint startPnt;   //起點    QPoint endPnt;     //終點    bool isPressed;    //滑鼠是否按下    vector<myLine*> lines; //存放所有的線段};#endif // MYPAINTERWIDGET_H

   .cpp 檔案如下

#include "mypainterwidget.h"#include <QString>#include <QMessageBox>#include <QPainter>#include <QPen>#include <QMouseEvent>MyPainterWidget::MyPainterWidget(QWidget* parent)     :QWidget(parent){    setMinimumSize(240,120);    setMaximumSize(480,240);    this->setMouseTracking(true);    this->isPressed = false;}MyPainterWidget::~MyPainterWidget(){}void MyPainterWidget::paintEvent(QPaintEvent*p){    QPainter painter(this);    QPen pen;                                 //建立一個畫筆    pen.setColor(Qt::darkCyan);    pen.setWidth(5);    painter.setPen(pen);    for(int i = 0;i<lines.size();i++){        myLine* pLine = lines[i];        painter.drawLine(pLine->startPnt,pLine->endPnt);    }}void MyPainterWidget::mousePressEvent(QMouseEvent *e){    setCursor(Qt::PointingHandCursor);    startPnt = e->pos();    endPnt = e->pos();    this->isPressed = true;    //QString msg ="("+QString::number(e->x())+","+QString::number(e->y())+")";    //QMessageBox::warning(this,tr("Warning"),msg,QMessageBox::Ok);}void MyPainterWidget::mouseMoveEvent(QMouseEvent *e){    if(this->isPressed){        endPnt = e->pos();        myLine* line = new myLine;  //put the new line into vector        line->startPnt = startPnt;        line->endPnt = endPnt;        this->lines.push_back(line);        update();                                    //repainter,call paintEvent        startPnt = endPnt;    }}void MyPainterWidget::mouseReleaseEvent(QMouseEvent *e){    setCursor(Qt::ArrowCursor);    this->isPressed = false;}

3. 運行結果如下

 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.