Qt学习例子14——Interactiveitem
来源:互联网 发布:公安局文职怎么样 知乎 编辑:程序博客网 时间:2024/05/16 16:06
Interactiveitem工程在上个例子的基础之上添加了鼠标事件,从而实现交互。
程序依此图而作:
程序代码如下:
//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 ¢er, 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——Interactiveitem
- Qt学习例子1——HelloWorld
- Qt学习例子2——CalculatorInterface
- Qt学习例子3——test
- Qt学习例子4——Test2_1
- Qt学习例子5——calculatorwidget
- Qt学习例子6——customtype
- Qt学习例子7——fileaccess
- Qt学习例子8——filechooser
- Qt学习例子9——smile3
- Qt学习例子10——Compsegraphicsitem
- Qt学习例子11——Transformedsmiley
- Qt学习例子12——Movablesimley
- Qt学习例子13——Customitem
- Qt学习例子15——objectitem
- Qt学习例子15——objectitem
- Qt学习笔记——一些样式表例子
- Qt学习—1
- 平板电脑价格4年后可降一半
- 欧几里德算法(辗转相除法)
- Qt学习例子13——Customitem
- iPhone 5的消息
- 开始学习Objective-C一些代码细节解释
- Qt学习例子14——Interactiveitem
- 如何做一个称职的程序员
- JDK1.6官方下载_JDK6官方下载地址
- Qt学习例子15——objectitem
- Qt学习例子15——objectitem
- c文件操作(网上转载)
- MS algorithm interview (2) Stack with min function
- label显示换行
- 我的面向对象