首页 > 其他分享 >QT画机器人

QT画机器人

时间:2022-12-28 17:06:49浏览次数:44  
标签:20 QT QPropertyAnimation 机器人 animation new event painter


#include <QtWidgets>

#include "robot.h"


RobotPart::RobotPart(QGraphicsItem *parent)
: QGraphicsObject(parent), color(Qt::lightGray), dragOver(false)
{
setAcceptDrops(true);
}

void RobotPart::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
if (event->mimeData()->hasColor()) {
event->setAccepted(true);
dragOver = true;
update();
} else {
event->setAccepted(false);
}
}

void RobotPart::dragLeaveEvent(QGraphicsSceneDragDropEvent *event)
{
Q_UNUSED(event);
dragOver = false;
update();
}

void RobotPart::dropEvent(QGraphicsSceneDragDropEvent *event)
{
dragOver = false;
if (event->mimeData()->hasColor())
color = qvariant_cast<QColor>(event->mimeData()->colorData());
update();
}

RobotHead::RobotHead(QGraphicsItem *parent)
: RobotPart(parent)
{
}

QRectF RobotHead::boundingRect() const
{
return QRectF(-15, -50, 30, 50);
}

void RobotHead::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);
if (pixmap.isNull()) {
painter->setBrush(dragOver ? color.light(130) : color);
painter->drawRoundedRect(-10, -30, 20, 30, 25, 25, Qt::RelativeSize);
painter->setBrush(Qt::white);
painter->drawEllipse(-7, -3 - 20, 7, 7);
painter->drawEllipse(0, -3 - 20, 7, 7);
painter->setBrush(Qt::black);
painter->drawEllipse(-5, -1 - 20, 2, 2);
painter->drawEllipse(2, -1 - 20, 2, 2);
painter->setPen(QPen(Qt::black, 2));
painter->setBrush(Qt::NoBrush);
painter->drawArc(-6, -2 - 20, 12, 15, 190 * 16, 160 * 16);
} else {
painter->scale(.2272, .2824);
painter->drawPixmap(QPointF(-15 * 4.4, -50 * 3.54), pixmap);
}
}

void RobotHead::dragEnterEvent(QGraphicsSceneDragDropEvent *event)
{
if (event->mimeData()->hasImage()) {
event->setAccepted(true);
dragOver = true;
update();
} else {
RobotPart::dragEnterEvent(event);
}
}

void RobotHead::dropEvent(QGraphicsSceneDragDropEvent *event)
{
if (event->mimeData()->hasImage()) {
dragOver = false;
pixmap = qvariant_cast<QPixmap>(event->mimeData()->imageData());
update();
} else {
RobotPart::dropEvent(event);
}
}


RobotTorso::RobotTorso(QGraphicsItem *parent)
: RobotPart(parent)
{
}

QRectF RobotTorso::boundingRect() const
{
return QRectF(-30, -20, 60, 60);
}

void RobotTorso::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);

painter->setBrush(dragOver ? color.light(130) : color);
painter->drawRoundedRect(-20, -20, 40, 60, 25, 25, Qt::RelativeSize);
painter->drawEllipse(-25, -20, 20, 20);
painter->drawEllipse(5, -20, 20, 20);
painter->drawEllipse(-20, 22, 20, 20);
painter->drawEllipse(0, 22, 20, 20);
}

RobotLimb::RobotLimb(QGraphicsItem *parent)
: RobotPart(parent)
{
}

QRectF RobotLimb::boundingRect() const
{
return QRectF(-5, -5, 40, 10);
}

void RobotLimb::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(option);
Q_UNUSED(widget);

painter->setBrush(dragOver ? color.light(130) : color);
painter->drawRoundedRect(boundingRect(), 50, 50, Qt::RelativeSize);
painter->drawEllipse(-5, -5, 10, 10);
}


Robot::Robot(QGraphicsItem *parent)
: RobotPart(parent)
{
setFlag(ItemHasNoContents);

QGraphicsObject *torsoItem = new RobotTorso(this);
QGraphicsObject *headItem = new RobotHead(torsoItem);
QGraphicsObject *upperLeftArmItem = new RobotLimb(torsoItem);
QGraphicsObject *lowerLeftArmItem = new RobotLimb(upperLeftArmItem);
QGraphicsObject *upperRightArmItem = new RobotLimb(torsoItem);
QGraphicsObject *lowerRightArmItem = new RobotLimb(upperRightArmItem);
QGraphicsObject *upperRightLegItem = new RobotLimb(torsoItem);
QGraphicsObject *lowerRightLegItem = new RobotLimb(upperRightLegItem);
QGraphicsObject *upperLeftLegItem = new RobotLimb(torsoItem);
QGraphicsObject *lowerLeftLegItem = new RobotLimb(upperLeftLegItem);

headItem->setPos(0, -18);
upperLeftArmItem->setPos(-15, -10);
lowerLeftArmItem->setPos(30, 0);
upperRightArmItem->setPos(15, -10);
lowerRightArmItem->setPos(30, 0);
upperRightLegItem->setPos(10, 32);
lowerRightLegItem->setPos(30, 0);
upperLeftLegItem->setPos(-10, 32);
lowerLeftLegItem->setPos(30, 0);

QParallelAnimationGroup *animation = new QParallelAnimationGroup(this);

QPropertyAnimation *headAnimation = new QPropertyAnimation(headItem, "rotation");
headAnimation->setStartValue(20);
headAnimation->setEndValue(-20);
QPropertyAnimation *headScaleAnimation = new QPropertyAnimation(headItem, "scale");
headScaleAnimation->setEndValue(1.1);
animation->addAnimation(headAnimation);
animation->addAnimation(headScaleAnimation);


QPropertyAnimation *upperLeftArmAnimation = new QPropertyAnimation(upperLeftArmItem, "rotation");
upperLeftArmAnimation->setStartValue(190);
upperLeftArmAnimation->setEndValue(180);
animation->addAnimation(upperLeftArmAnimation);

QPropertyAnimation *lowerLeftArmAnimation = new QPropertyAnimation(lowerLeftArmItem, "rotation");
lowerLeftArmAnimation->setStartValue(50);
lowerLeftArmAnimation->setEndValue(10);
animation->addAnimation(lowerLeftArmAnimation);

QPropertyAnimation *upperRightArmAnimation = new QPropertyAnimation(upperRightArmItem, "rotation");
upperRightArmAnimation->setStartValue(300);
upperRightArmAnimation->setEndValue(310);
animation->addAnimation(upperRightArmAnimation);

QPropertyAnimation *lowerRightArmAnimation = new QPropertyAnimation(lowerRightArmItem, "rotation");
lowerRightArmAnimation->setStartValue(0);
lowerRightArmAnimation->setEndValue(-70);
animation->addAnimation(lowerRightArmAnimation);

QPropertyAnimation *upperLeftLegAnimation = new QPropertyAnimation(upperLeftLegItem, "rotation");
upperLeftLegAnimation->setStartValue(150);
upperLeftLegAnimation->setEndValue(80);
animation->addAnimation(upperLeftLegAnimation);

QPropertyAnimation *lowerLeftLegAnimation = new QPropertyAnimation(lowerLeftLegItem, "rotation");
lowerLeftLegAnimation->setStartValue(70);
lowerLeftLegAnimation->setEndValue(10);
animation->addAnimation(lowerLeftLegAnimation);

QPropertyAnimation *upperRightLegAnimation = new QPropertyAnimation(upperRightLegItem, "rotation");
upperRightLegAnimation->setStartValue(40);
upperRightLegAnimation->setEndValue(120);
animation->addAnimation(upperRightLegAnimation);

QPropertyAnimation *lowerRightLegAnimation = new QPropertyAnimation(lowerRightLegItem, "rotation");
lowerRightLegAnimation->setStartValue(10);
lowerRightLegAnimation->setEndValue(50);
animation->addAnimation(lowerRightLegAnimation);

QPropertyAnimation *torsoAnimation = new QPropertyAnimation(torsoItem, "rotation");
torsoAnimation->setStartValue(5);
torsoAnimation->setEndValue(-20);
animation->addAnimation(torsoAnimation);


for (int i = 0; i < animation->animationCount(); ++i) {
QPropertyAnimation *anim = qobject_cast<QPropertyAnimation *>(animation->animationAt(i));
anim->setEasingCurve(QEasingCurve::SineCurve);
anim->setDuration(2000);
}

animation->setLoopCount(-1);
animation->start();

}


QRectF Robot::boundingRect() const
{
return QRectF();
}

void Robot::paint(QPainter *painter,
const QStyleOptionGraphicsItem *option, QWidget *widget)
{
Q_UNUSED(painter);
Q_UNUSED(option);
Q_UNUSED(widget);
}

QT画机器人_机器人



标签:20,QT,QPropertyAnimation,机器人,animation,new,event,painter
From: https://blog.51cto.com/u_15515702/5975717

相关文章

  • QT 结构体字节对界问题
    QT默认是4字节对界,所以程序中使用sizeof()时会出现问题,看采用如下方法:/字节对齐,否则共用体那里指针处理时有问题#pragmapack(push)//保存对齐#pragmapack(1)//设定......
  • QT编译报错,LNK2001:无法解析的外部符号
    今天按照以前写过的文件依葫芦画瓢新建了一个类,结果报了3行LNK2001的错误,如下图:QT比较恶心的一个地方就是这种错误不能直接看出来是哪出的问题,而且双击也跳......
  • Linux 下搭建 Qt 环境
    很多时候由于跨平台的需要,我们要在各个平台上安装Qt并进行应用程序的开发,而环境的搭建尤为重要,因为这是程序开发的前提,属于非常关键的一环。由于Linux 各个发行版的安......
  • 嘿,Qt 市场来啦!
    2019/12/02,对于广大的Qt爱好者来说,这是一个特殊的日子,因为QtMarketplace终于来啦,欢呼吧!在过去的一年里,QtMarketplace一直在努力建设之中,并在其伟大社区的帮助下,推出......
  • C++/Qt 系列推荐 - ORM(对象关系映射)
    教程目录以下是教程目录,从理论到实战,一步步深入ORM:​​快速认识ORM​​​​QxOrm介绍、下载、编译​​​​QxOrm快速上手​​​​在Ubuntu上安装MySQL​​​​QMYSQL......
  • 基于点云的机器人抓取识别综述
    机器人作为面向未来的智能制造重点技术,其具有可控性强、灵活性高以及配置柔性等优势,被广泛的应用于零件加工、协同搬运、物体抓取与部件装配等领域,如图1-1所示。然而,传统机......
  • python 接入钉钉群机器人
    一、获取机器人信息。1)添加自定义机器人 2)保存机器人webhook信息  二:调用机器人接口1)curl命令转化程代码可以使用在线工具进行转化程其他语言的代码。cur......
  • pyqt5-python交互
    安装pyqt5,算是框架,直接在pycharm库里面搜pyqt5就行了安装可视化设计器QtDesigner,这个去官网下载就可以了,不过最好找个中文版的下载。在pycharm上配置插件,好让pych......
  • QT实现延时
    今天写程序需要QT代码用到延时__ms的功能//延时10msQEventLooploop;QTimer::singleShot(10,&loop,SLOT(quit()));loop.exec();参考:QT不阻塞线程,达到延时的作用_......
  • 用云函数开发掘金钉钉机器人
    前言前段时间看了B站UP主@​​人工智能小黄鸭​​的出的视频,可以利用飞书机器人在线刷题,非常牛逼,行云流水。自从我在稀土掘金社区技术更文以来,每天非常关注文章点赞评论......