【问题标题】:How to update eyes of a face based on mouse click qt widget application C++?如何基于鼠标单击 qt 小部件应用程序 C++ 更新面部的眼睛?
【发布时间】:2021-04-28 07:45:00
【问题描述】:

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

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,0,200,200);//creating scene to add graphics items
    ui->graphicsView->setScene(scene);//adding scene to graphics view
    QGraphicsEllipseItem * face= new QGraphicsEllipseItem(0,0,200,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,60,30,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,67,15,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;
}

我希望能够根据鼠标点击的位置更新眼睛的方向,有人可以帮助解决这个问题。

【问题讨论】:

  • 你应该计算黑色圆圈(眼睛)中心的坐标。它应该位于鼠标点击和眼睛中心之间的线上。
  • 啊,又一个Xeyes克隆诞生了... :-)

标签: qt qt5 qgraphicsview qgraphicsscene qgraphicsitem


【解决方案1】:

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

// eyeX, eyeY - coordinates of center of the eye
// pupilX, pupilY - coordinates of center of the pupil
double dy = eyeY - cursorY;
double dx = eyeX - cursorX;
double angle = atan2(dy, dx);
double shift = 10.0;
double pupilX = eyeX + shift * cos(angle);
double pupilY = eyeY + shift * sin(angle);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多