贴下代码:
#define MYWIDGET_H
#include <QWidget>
#include <QtGui>
#include <QMouseEvent>
class MyWidget : public QWidget
{
public:
MyWidget();
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void paintEvent(QPaintEvent *event);
private:
QPoint m_PointStart;
QPoint m_PointEnd;
};
#endif // MYWIDGET_H
#include "mainwindow.h"
#include "mywidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget widget;
widget.show();
return a.exec();
}
MyWidget::MyWidget()
{
resize(240,320);
}
void MyWidget::mousePressEvent(QMouseEvent *event)
{
m_PointStart = event->pos();
}
void MyWidget::mouseMoveEvent(QMouseEvent *event)
{
//m_PointEnd = event->pos();
//update();
}
void MyWidget::mouseReleaseEvent(QMouseEvent *event)
{
m_PointEnd = event->pos();
update();
}
void MyWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setBrush(QBrush(QColor(255,0,0)));
painter.drawPixmap(0,0,240,320,QPixmap("images/frame1.jpg"));
if(m_PointStart.x() < m_PointEnd.x())
painter.drawPixmap(0,0,240,320,QPixmap("images/frame2.jpg"));
else if(m_PointStart.x() > m_PointEnd.x())
painter.drawPixmap(0,0,240,320,QPixmap("images/frame3.jpg"));
}
http://www.cppblog.com/qianqian/archive/2010/07/27/121418.html