Posted on 2010-07-27 22:00
千张 阅读(3729)
评论(2) 编辑 收藏 引用 所属分类:
QT学习
贴下代码:
#ifndef MYWIDGET_H
#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 <QtGui/QApplication>
#include "mainwindow.h"
#include "mywidget.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget widget;
widget.show();
return a.exec();
}
#include "mywidget.h"
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"));
}