#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
m_canvasPix = QPixmap(this->width(),this->height());
m_canvasPix.fill(Qt::white);
m_bIsDrawing = false;
m_timerId1 = startTimer(1000);
m_timerId2 = startTimer(1000);
m_timerId3 = startTimer(1000);
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(updateTimer()));
timer->start(1000);
}
Widget::~Widget()
{
}
void Widget::updateTimer()
{
qDebug("style 1: hello world .....");
}
void Widget::paintEvent(QPaintEvent *event)
{
int x,y,w,h;
x = m_lastPt.x();
y = m_lastPt.y();
w = m_endPt.x() - x;
h = m_endPt.y() - y;
QPainter painter(this);
//! 如果正在绘图,就在辅助画布上绘制
if(m_bIsDrawing)
{
//! 将以前pix中的内容复制到tempPix中,保证以前的内容不消失
m_tmpCanvasPix = m_canvasPix;
QPainter pp(&m_tmpCanvasPix);
pp.drawRect(x,y,w,h);
painter.drawPixmap(0, 0, m_tmpCanvasPix);
} else {
QPainter pp(&m_canvasPix);
pp.drawRect(x,y,w,h);
painter.drawPixmap(0,0,m_canvasPix);
}
}
void Widget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton){
m_lastPt = event->pos();
m_bIsDrawing = true; //正在绘图
}
}
void Widget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton){
m_endPt = event->pos();
m_bIsDrawing = false;
update();
}
}
void Widget::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() == Qt::LeftButton){
m_endPt = event->pos();
update();
}
}
void Widget::keyPressEvent(QKeyEvent *event)
{
int x,y;
if (event->modifiers() == Qt::ControlModifier) {
switch (event->key()) {
case Qt::Key_Down:
x = m_endPt.x()+10;
y = m_endPt.y()+10;
m_endPt.setX(x);
m_endPt.setY(y);
update();
m_bIsDrawing = true;
break;
case Qt::Key_Up:
x = m_endPt.x()-10;
y = m_endPt.y()-10;
m_endPt.setX(x);
m_endPt.setY(y);
update();
m_bIsDrawing = true;
break;
}
}
}
void Widget::keyReleaseEvent(QKeyEvent *event)
{
switch (event->key()) {
case Qt::Key_Down:
m_bIsDrawing = false;
update();
break;
case Qt::Key_Up:
m_bIsDrawing = false;
update();
break;
}
}
void Widget::timerEvent(QTimerEvent *event)
{
if (event->timerId() == m_timerId1) {
qDebug("style 2:timerId %d,hello world...",event->timerId());
}
else if (event->timerId() == m_timerId2){
qDebug("style 2:timerId %d,hello world...",event->timerId());
}
else if (event->timerId() == m_timerId3){
qDebug("style 2:timerId %d,hello world...",event->timerId());
}
}
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <qlayout.h>
#include <QPainter>
#include <QMouseEvent>
#include <QKeyEvent>
#include <QTimer>
#include <QTime>
#include <QTimerEvent>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
private:
QPixmap m_canvasPix;
QPoint m_lastPt;
QPoint m_endPt;
QPixmap m_tmpCanvasPix; //! 辅助画布
bool m_bIsDrawing; //! 标志是否正在绘图
int m_timerId1; //! 定时器1
int m_timerId2; //! 定时器2
int m_timerId3; //! 定时器3
private slots:
void updateTimer();
protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void keyPressEvent(QKeyEvent *event);
void keyReleaseEvent(QKeyEvent *event);
void timerEvent(QTimerEvent *event);
};
#endif // WIDGET_H
#ifndef MYAPPLICATION_H
#define MYAPPLICATION_H
#include<QApplication>
#include<QMouseEvent>
#include<QDebug>
class myApplication : public QApplication
{
public:
myApplication(int argc, char *argv[]);
protected:
bool eventFilter(QObject *obj, QEvent *event);
};
#endif // MYAPPLICATION_H
#include "myapplication.h"
myApplication::myApplication(int argc, char *argv[]):QApplication(argc,argv)
{
}
bool myApplication::eventFilter(QObject *obj, QEvent *event)
{
static int k=1;
if(event->type()==QEvent::MouseButtonPress||event->type()==QEvent::MouseButtonDblClick)
{
qDebug()<<"Click"<<k;
k++;
return true;
}
return QApplication::eventFilter(obj,event);
}
#include "widget.h"
#include "myapplication.h"
#include <QApplication>
int main(int argc, char *argv[])
{
myApplication a(argc, argv);
a.installEventFilter(&a);
Widget w;
w.show();
return a.exec();
}