Qt学习例子14——Interactiveitem

来源:互联网 发布:公安局文职怎么样 知乎 编辑:程序博客网 时间:2024/05/16 16:06

Interactiveitem工程在上个例子的基础之上添加了鼠标事件,从而实现交互。

程序依此图而作:

 Qt学习例子14鈥斺擨nteractiveitem

 

程序代码如下:

 

//main.cpp

 

#include <QtGui/QApplication>
#include <QGraphicsView>
#include <QGraphicsScene>
#include "smileyitem.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsView w;
    QGraphicsScene scene;
    w.setScene(&scene);
    SmileyItem *item = new SmileyItem;
    scene.addItem(item);
    item->setFlag(QGraphicsItem::ItemIsMovable);
    w.setRenderHint(QPainter::Antialiasing);
    w.show();
    w.resize(600,400);
    return a.exec();
}

//smileyitem.h

 

#ifndef SMILEYITEM_H
#define SMILEYITEM_H
#include <QGraphicsItem>
class SmileyItem : public QGraphicsItem
{
public:
    SmileyItem();
    QRectF boundingRect() const;
    QPainterPath shape() const;
    void paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0);
protected:
    void paintEye(QPainter *painter, const QPointF &pt, const QPointF &focus, bool bigEye);
    void paintSmile(QPainter *painter, const QRectF &rect);
    void hoverEnterEvent(QGraphicsSceneHoverEvent *event);
    void hoverMoveEvent(QGraphicsSceneHoverEvent *event);
    void hoverLeaveEvent(QGraphicsSceneHoverEvent *event);
    void mousePressEvent(QGraphicsSceneMouseEvent *event);
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
private:
    QPointF m_focus;
    bool m_bigEyes;
};
#endif // SMILEYITEM_H

 

 

//smileitem.cpp

 

#include "smileyitem.h"
#include <QPainter>
#include <QGraphicsSceneHoverEvent>
#include <cmath>
QPoint computePupil (const QPoint &center, const QPoint &focus, int eyeWidth, int eyeHeight)
{
    QPointF dPoint = focus-center;
    if (dPoint.x() == 0 && dPoint.y() == 0) {
        return center;
    } else {
        double angle = atan2 (dPoint.y(), dPoint.x());
        double cosa = cos (angle);
        double sina = sin (angle);
        double h = hypot (eyeHeight * cosa, eyeWidth * sina);
        double x = (eyeWidth * eyeHeight) * cosa / h;
        double y = (eyeWidth * eyeHeight) * sina / h;
        double dist = hypot (x*0.7, y*0.7); //?
        if (dist > hypot (dPoint.x(), dPoint.y())) {
            return dPoint.toPoint()+center;
        } else {
            return QPoint(dist*cosa+center.x(), dist*sina+center.y());
       }
    }
}
SmileyItem::SmileyItem()
{
    setAcceptHoverEvents(true);
    m_bigEyes = false;
}
QRectF SmileyItem::boundingRect() const {
    return QRectF(-50, -50, 100, 100);
}
QPainterPath SmileyItem::shape() const {
    // enter your code here
    QPainterPath path;
    path.addEllipse(boundingRect());
    return path;
    return QGraphicsItem::shape();
}
void SmileyItem::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget *widget ) {
    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::yellow);
    painter->drawEllipse(boundingRect());
    paintEye(painter, QPointF(-15,-25), m_focus, m_bigEyes);
    paintEye(painter, QPointF(15, -25), m_focus, m_bigEyes);
    paintSmile(painter, QRectF(-33, -12, 66, 50));
}
void SmileyItem::paintEye(QPainter *painter, const QPointF &pt, const QPointF &focus, bool bigEye){
    QPen pen(Qt::black);
    pen.setWidthF(1.5);
    painter->setPen(pen);
    painter->setBrush(Qt::white);
    QSize eyeSize(6,12);
    if(bigEye)
        eyeSize+=QSize(2,4);
    painter->drawEllipse(pt, eyeSize.width(), eyeSize.height());
    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::black);
    QPoint pupilPoint = focus.isNull() ? pt.toPoint() : computePupil(pt.toPoint(),focus.toPoint(),
                                                                     eyeSize.width(),eyeSize.height());
    painter->drawEllipse(pupilPoint, 2, 2);
}
void SmileyItem::paintSmile(QPainter *painter, const QRectF &rect) {
    QPen pen(Qt::black);
    pen.setWidthF(1.5);
   painter->setPen(pen);
    painter->drawArc(rect, 0, -180*16);
}
void SmileyItem::hoverEnterEvent(QGraphicsSceneHoverEvent *event) {
    // enter your code here
   
    m_focus=event->pos();
    update();
}
void SmileyItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) {
    // enter your code here
   
    m_focus=event->pos();
    update();
}
void SmileyItem::hoverLeaveEvent(QGraphicsSceneHoverEvent *event) {
    // enter your code here
   
    m_focus=QPointF();
    update();
}
void SmileyItem::mousePressEvent(QGraphicsSceneMouseEvent *event) {
    // enter your code here
    m_bigEyes=!m_bigEyes;
   
    update();
    QGraphicsItem::mousePressEvent(event);
}
void SmileyItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) {
    // enter your code here
    m_bigEyes=!m_bigEyes;
   
    update();
    QGraphicsItem::mouseReleaseEvent(event);
}

 

 

程序效果图如下:

 Qt学习例子14鈥斺擨nteractiveitem

原创粉丝点击