zoukankan      html  css  js  c++  java
  • 处理鼠标响应事件(最简单控件 good)

    贴下代码:

    #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/2010/07/27/121418.html

  • 相关阅读:
    C++友元
    C++类与static
    C++const
    reinterpret_cast应用
    学习软件工程课的心得上
    学习软件工程课的心得下
    项目总结报告之假如历史重新再来一次
    人月神话读后感
    团队任务考核
    冲刺周期会议十一
  • 原文地址:https://www.cnblogs.com/findumars/p/5805090.html
Copyright © 2011-2022 走看看