用QT编写一个简单的鼠绘程序,只要求能画出线条,该怎么实现程序对鼠标动作的响应呢?
发布网友
发布时间:2022-05-23 14:39
我来回答
共1个回答
热心网友
时间:2023-10-25 11:31
#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"));
}
在网上找到了一个很好的类子,一看就明白
转自:http://www.cppblog.com/qianqian/archive/2012/05/21/121418.html