微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何基于鼠标单击 qt 小部件应用程序 C++ 更新人脸的眼睛?

如何解决如何基于鼠标单击 qt 小部件应用程序 C++ 更新人脸的眼睛?

我使用 qt creator 创建了一个简单的 GUI,如下所示

enter image description here

mainwindow.cpp 看起来像这样

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QGraphicsEllipseItem>
#include <QGraphicsScene>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QGraphicsScene * scene= new QGraphicsScene(0,200,200);//creating scene to add graphics items
    ui->graphicsView->setScene(scene);//adding scene to graphics view
    QGraphicsEllipseItem * face= new QGraphicsEllipseItem(0,200);//creating face
    scene->addItem(face);//adding face to scene
    QGraphicsEllipseItem * lefteye= new QGraphicsEllipseItem(50,60,30,30);//creating left eye
    scene->addItem(lefteye);//adding left eye to scene
    QGraphicsEllipseItem * righteye= new QGraphicsEllipseItem(125,30);//creating right eye
    scene->addItem(righteye);//adding right eye to scene
    QGraphicsEllipseItem * lefteyeball= new QGraphicsEllipseItem(58,67,15,15);//creating left eyeball
    lefteyeball->setBrush(Qt::black);//setting color of left eyeball
    scene->addItem(lefteyeball);//adding left eyeball to scene
    QGraphicsEllipseItem * righteyeball= new QGraphicsEllipseItem(133,15);//creating right eyeball
    righteyeball->setBrush(Qt::black);//setting color of right eyeball
    scene->addItem(righteyeball);//adding right eyeball to scene
}

MainWindow::~MainWindow()
{
    delete ui;
}

我希望能够根据点击鼠标的位置更新眼睛的方向,有人可以帮助实现这一点。

解决方法

您需要使用 atan2 计算眼睛中心和光标位置之间的角度,并将瞳孔从中心偏移 XA*cos(angle)Y 为 {{ 1}} 其中 A*sin(angle) 是中心的位移(常数值)。

A

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。