【发布时间】: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