1. 程式人生 > >qt 自定義視窗顯示滑鼠劃過的軌跡

qt 自定義視窗顯示滑鼠劃過的軌跡

滑鼠事件分為四種:

1.按下

2.擡起

3.移動

4.雙擊

滑鼠事件繼承與QWidget

void mouseDoubleClickEvent(QMouseEvent *event)
void mouseMoveEvent(QMouseEvent *event)
void mousePressEvent(QMouseEvent *event)
void mouseReleaseEvent(QMouseEvent *event)

重寫這四個函式,就能夠讓自定義控制元件支援滑鼠操作。

QT中用QMouseEvent類來表達一個滑鼠事件

x,y :座標

globleX,globleY:全域性座標

buttun:滑鼠的按鍵

例子: 當滑鼠按下的時候,畫出滑鼠移動的軌跡,直到鬆開

(1)按下時

m_pressFlag=true;

  (2)移動時

記錄軌跡

(3)鬆開時

m_pressFlag=false;

circlewidget.h

#ifndef CIRCLAWIDGET_H
#define CIRCLAWIDGET_H
#include <QFrame>
#include<QVector>
#include<QMouseEvent>

class CircleWidget:public QFrame
{
    Q_OBJECT
public: CircleWidget(QWidget *parent); ~CircleWidget(); private: void paintEvent(QPaintEvent *event); void mouseDoubleClickEvent(QMouseEvent *event); void mouseMoveEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event); void mouseReleaseEvent(QMouseEvent *event
); private: bool m_pressFlag; QVector<QPoint>m_points; }; #endif // CIRCLAWIDGET_H

circlewidget.cpp

# include"circlewidget.h"
#include<QPainter>
#include<QDebug>

CircleWidget::CircleWidget(QWidget *parent):QFrame(parent)
{
   m_pressFlag=false;
}
CircleWidget::~CircleWidget()
{

}
void CircleWidget::paintEvent(QPaintEvent *event)
{
        QPainter painter(this);
       int width=this->width();
       int height=this->height();
       painter.setBrush(QColor(0,0,0));
       painter.drawRect(0,0,width,height);
       QPen pen(QColor(0xFF,0,0));
       painter.setPen(pen);
       if(m_pressFlag)
       {
           for(int i=0;i<m_points.size()-1;i++)
           {
               QPoint & p1=m_points[i];
               QPoint & p2=m_points[i+1];
               painter.drawLine(p1,p2);
           }
       }



}

void CircleWidget::mouseDoubleClickEvent(QMouseEvent *event)
{

}
void CircleWidget::mouseMoveEvent(QMouseEvent *event)
{
  if(m_pressFlag)
  {
      QPoint pos=event->pos();
      m_points.append(pos);
      qDebug("(%d,%d)",pos.x(),pos.y());
      update();
  }
}
void CircleWidget::mousePressEvent(QMouseEvent *event)
{
    m_pressFlag=true;

}
void CircleWidget::mouseReleaseEvent(QMouseEvent *event)
{
    m_pressFlag=false;
}

結果:

#include"circlewidget.h"
#include<QPainter>
#include<QDebug>
 
CircleWidget::CircleWidget(QWidget*parent):QFrame(parent)
{
m_pressFlag=false;
}
CircleWidget::~CircleWidget()
{
 
}
voidCircleWidget::paintEvent(QPaintEvent*event)
{
QPainterpainter(this);
intwidth=this->width();
intheight=this->height();
painter.setBrush(QColor(0,0,0));
painter.drawRect(0,0,width,height);
QPenpen(QColor(0xFF,0,0));
painter.setPen(pen);
if(m_pressFlag)
{
for(inti=0;i<m_points.size()-1;i++)
{
QPoint&p1=m_points[i];
QPoint&p2=m_points[i+1];
painter.drawLine(p1,p2);
}
}
 
 
 
}
 
voidCircleWidget::mouseDoubleClickEvent(QMouseEvent*event)
{
 
}
voidCircleWidget::mouseMoveEvent(QMouseEvent*event)
{
if(m_pressFlag)
{
QPointpos=event->pos();
m_points.append(pos);
qDebug("(%d,%d)",pos.x(),pos.y());
update();
}
}
voidCircleWidget::mousePressEvent(QMouseEvent*event)
{
m_pressFlag=true;
 
}
voidCircleWidget::mouseReleaseEvent(QMouseEvent*event)
{
m_pressFlag=false;
}