Qt QPainter滑鼠繪製線條 矩形 多邊形

2021-10-16 19:56:17 字數 3581 閱讀 5657

qt通過滑鼠繪製線條、矩形、多邊形本質都是根據滑鼠的座標位移,使用qpainter的自帶的函式進行繪製。具體**如下:

graphicspainter.h

#ifndef graphicspainter_h

#define graphicspainter_h

#include

#include

"shape.h"

#include

"paintrectline.h"

class

graphicspainter

:public qwidget

;#endif

// graphicspainter_h

graphicspainter.cpp

graphicspainter::

graphicspainter

(qwidget *parent)

:qwidget

(parent)

,shape

(nullptr

)void graphicspainter::

setdraw

(bool bdraw)

void graphicspainter::

setcurrcode

(shape::code code)

case shape::rect:

case shape::polygon:}if

(shape !=

null)}

//重新實現paintevent

void graphicspainter::

paintevent

(qpaintevent *

)//繪製當前的線條

if(shape)}}

//按下

void graphicspainter::

mousepressevent

(qmouseevent *event)

case shape::rect:

case shape::polygon:}if

(shape !=

null)}

if(bdraw)

//繪製}}

//移動

void graphicspainter::

mousemoveevent

(qmouseevent *event)

case shape::rect:

//畫矩形

case shape::polygon:

//畫多邊形}}

}//鬆開

void graphicspainter::

mousereleaseevent

(qmouseevent *e)

}//雙擊斷開連線

void graphicspainter::

mousedoubleclickevent

(qmouseevent *event)

}

繪製圖形的基類shape.h

#ifndef shape_h

#define shape_h

#include

class

shape

;shape()

;void

setstart

(qpoint s)

;void

setend

(qpoint e)

;void

setmovepoint

(qpoint e)

;void

setpointf

(qpointf point_)

; qpoint startpoint()

; qpoint endpoint()

;void

setmovestatus

(bool status)

;virtual

void

paint

(qpainter & painter)=0

;protected

: qpoint start;

qpoint end;

qpoint movepoint;

qvector pointlist;

bool move_status;};

#endif

// shape_h

shape.cpp

#include

"shape.h"

shape::

shape()

void shape::

setstart

(qpoint s)

void shape::

setend

(qpoint e)

void shape::

setmovepoint

(qpoint e)

void shape::

setpointf

(qpointf point_)

qpoint shape::

startpoint()

qpoint shape::

endpoint()

void shape::

setmovestatus

(bool status )

具體繪製圖形的子類paintrectline.h

#ifndef paintrectline_h

#define paintrectline_h

#include

"shape.h"

class

line

:public shape

;class

rect

:public shape

;class

polygon_

:public shape

;#endif

// paintrectline_h

#include

"paintrectline.h"

line ::

line()

void line ::

paint

(qpainter &painter)

rect ::

rect()

void rect ::

paint

(qpainter &painter)

polygon_::

polygon_()

void polygon_::

paint

(qpainter &painter)

//if(bmove&&pointlist.size()>0)

if(move_status&&pointlist.

size()

>0)

painter.

drawlines

(lines)

;}

js實現滑鼠繪製層拖拽特效

首先非常感謝濤濤的無私分享,此作品是一款他今天剛寫的原創js實現滑鼠繪製層拖拽功能,暫時只實現了滑鼠繪製圖層功能,拖拽功能還沒加上,是他在藍色理想看見有這樣乙個特效,沒有用jquery 來寫的,是利用js物件導向簡單模仿實現的,此作品主要是提供給們學習用。如下 01functiondragdrwa ...

Halcon用滑鼠繪製及顯示矩形

運算元 draw rectangle1 windowhandle row1,column1,row2,column2 功能 畫乙個與座標軸平行的矩形。操作該函式時,只要用滑鼠操作,按下右鍵即可獲取該矩形對應的座標。如果滑鼠不做任何操作,直接按下右鍵,將不會返回資料,可能會產生意想不到的後果。輸入引數...

Halcon用滑鼠繪製及顯示橢圓

運算元 draw ellipse windowhandle row,column,phi,radius1,radius2 功能 乙個橢圓的互動繪圖。操作該函式時,只要用滑鼠操作,按下右鍵即可獲取該橢圓所對應的座標和兩個半徑。如果滑鼠不做任何操作,直接按下右鍵,將不返回資料,可能產生意想不到的後果。輸...