2014-02-26 6 views
0

장면의 Qlabel에 저장된 이미지가 있습니다. 나는 장면 내부에서 커서가 움직이는 곳을 따라 Qlabel 이미지를 얻고 싶습니다. QGraphicsSceneMouseMove 시도하고 아직 가까이 오지 않았습니다.qlabel에서 장면의 커서를 따르는 방법

void scene::mouseMoveEvent(QGraphicsSceneMouseEvent /*mouseEvent*/) 
    { 

     QPointF P1 = ui->tankarmplay1->mapFromParent(QCursor.pos()); 
     int x = P1.x(); 
     int y = P1.y(); 
     ui->tankarmplay1->setGeometry(x,y, 50, 50); 

    } 

답변

1

업데이트 : 마우스를 가리키는 QGraphicsLineItem이 추가되었습니다. 이것은 QGraphicsItemGroup을 사용하고 동일한 회전 계산을 사용하는 일종의 포탑의 전체 그림으로 대체 될 수 있습니다.

다음 링크는 당신이 잘 알고 있어야하는 일의 많은에 대해 이야기 : 나는 개인적으로 QCursor을 사용하지 않은

http://qt-project.org/doc/qt-5/graphicsview.html

http://qt-project.org/doc/qt-5/application-windows.html

void scene::mouseMoveEvent(QGraphicsSceneMouseEvent * e /*mouseEvent*/) 
{ 

    // QPointF P1 = (e->pos()); 
    // int x = P1.x(); 
    // int y = P1.y(); 
    // ui->tankarmplay1->setGeometry(x, y, 50, 50); 

    ui->tankarmplay1->move((int) e->pos().x(), (int) e->pos().y()); 
} 

http://qt-project.org/doc/qt-5/qgraphicsscenemouseevent.html#pos

. 나는 마우스 이벤트의 pos 정보를 편리하게 이용할 수있을 때 마우스에 대해 매우 손쉽게 찾을 수 있다고 생각합니다. QCursor를 사용했다면 mapFromGlobal이 아닌 mapFromParent을 사용해야 할 것입니다. 여기

http://qt-project.org/doc/qt-5/qcursor.html#pos

http://qt-project.org/doc/qt-5/qcursor.html#details

내가 특정 QGraphicsSceneMouseEvent 방법을 사용하기 전에 쓴 무언가이다.

내가 작동하도록하려면 좌표가 일치하는 데 mapToScene()을 사용해야했습니다.

는 QWidget의 pos의 속성에서

How to draw a point (on mouseclick) on a QGraphicsScene?

, 당신은 일반적으로 move()로 수정합니다. setGeometry도 사용할 수 있지만 결국 widthheight을 많이 참조하게됩니다.

http://qt-project.org/doc/qt-4.8/qwidget.html#pos-prop

http://qt-project.org/doc/qt-4.8/qwidget.html#mouseTracking-prop

UPDATE : 굉장 예 아마 텍스트를 이동하려면 QLabel + QGraphicsProxyWidget를 사용하는 것보다 청소기있을 것 QGraphicsTextItem를 사용하여, 장면과 장면 주 밖에서 사용 마우스 추적을 보여줍니다 현장에서.

widget.h

#ifndef WIDGET_H 
#define WIDGET_H 

#include <QWidget> 
#include <QFrame> 
#include <QLabel> 
#include <QPointF> 
#include "mygraphicsscene.h" 
#include <QGraphicsView> 

class Widget : public QWidget 
{ 
    Q_OBJECT 

public: 
    Widget(QWidget *parent = 0); 
    ~Widget(); 
    qreal map(const qreal & x1, qreal x, const qreal & x2, const qreal & y1, const qreal & y2); 
public slots: 
    void on_sceneMouseMove(QPointF); 
private: 
    QLabel * m_label; 
    MyGraphicsScene * m_scene; 
    QGraphicsView * m_view; 
    QFrame * m_labelContainer; 
}; 

#endif // WIDGET_H 

widget.cpp

#include "widget.h" 
#include <QGraphicsView> 
#include "mygraphicsscene.h" 
#include <QVBoxLayout> 
#include <QLabel> 
#include <QDebug> 

Widget::Widget(QWidget *parent) 
    : QWidget(parent) 
{ 
    QVBoxLayout * vbox = new QVBoxLayout; 

    m_view = new QGraphicsView; 

    m_scene = new MyGraphicsScene; 

    m_view->setScene(m_scene); 

    m_view->setMouseTracking(true); 
    m_scene->setSceneRect(-300,-300, 600, 600); 
    m_view->fitInView(m_scene->sceneRect()); 

    vbox->addWidget(m_view, 1); 

    m_labelContainer = new QFrame; 
    m_labelContainer->setFrameShape(QFrame::Box); 
    m_label = new QLabel("Tracking Label"); 
    m_labelContainer->setFixedSize(300, 300); 
    m_label->setParent(m_labelContainer); 

    vbox->addWidget(m_labelContainer, 1); 

    QObject::connect(m_scene, SIGNAL(mouseMoved(QPointF)), 
        this, SLOT(on_sceneMouseMove(QPointF))); 

    this->setLayout(vbox); 
} 

void Widget::on_sceneMouseMove(QPointF pt) 
{ 
    QPointF pt2; 
    pt2.setX(map(m_scene->sceneRect().left(), pt.x(), m_scene->sceneRect().right(), 
       m_labelContainer->rect().left(), m_labelContainer->rect().right())); 

    pt2.setY(map(m_scene->sceneRect().top(), pt.y(), m_scene->sceneRect().bottom(), 
       m_labelContainer->rect().top(), m_labelContainer->rect().bottom())); 

// qDebug() << pt << pt2 << m_scene->sceneRect() << m_labelContainer->rect(); 
    m_label->move(pt2.x(), pt2.y()); 

// m_label->setGeometry(pt.x(), pt.y(), 
    //      m_label->width(), m_label->height()); 
} 

qreal Widget::map(const qreal & x1, qreal x, const qreal & x2, const qreal & y1, const qreal & y2) 
{ 
    if(x < x1) 
     x = x1; 
    if(x > x2) 
     x = x2; 
    return (x - x1) * (y2 - y1)/(x2 - x1) + y1; 
} 

Widget::~Widget() 
{ 

} 

MAIN.CPP

#include "widget.h" 
#include <QApplication> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    Widget w; 
    w.show(); 

    return a.exec(); 
} 

mygraphicsview.h

#ifndef MYGRAPHICSSCENE_H 
#define MYGRAPHICSSCENE_H 

#include <QGraphicsScene> 
#include <QGraphicsSceneMouseEvent> 
#include <QGraphicsProxyWidget> 
#include <QGraphicsLineItem> // Added this 

class MyGraphicsScene : public QGraphicsScene 
{ 
    Q_OBJECT 
public: 
    explicit MyGraphicsScene(QObject *parent = 0); 

signals: 
    void mouseMoved(QPointF); 
public slots: 
    void mouseMoveEvent(QGraphicsSceneMouseEvent *); 
private: 
    QGraphicsProxyWidget * m_labelProxy; 
    QGraphicsLineItem * m_lineItem; // Added this 
}; 

#endif // MYGRAPHICSSCENE_H 

mygraphicsview.cpp

#include "mygraphicsscene.h" 
#include <QDebug> 
#include <QLabel> 
#include <QVector2D> 
#include <qmath.h> 
#include <QLineF> 

MyGraphicsScene::MyGraphicsScene(QObject *parent) : 
    QGraphicsScene(parent) 
{ 
    QLabel * label = new QLabel("Tracking Widget\n in Scene"); 
    m_labelProxy = this->addWidget(label); 

    // added the lines below to setup an item, pointing in the positive x direction 
    int x1 = 0; 
    int y1 = 0; 
    m_lineItem = new QGraphicsLineItem(x1, y1, x1 + 20, y1); 
// m_lineItem->setTransformOriginPoint(x1, y1); 
    this->addItem(m_lineItem); 

    m_lineItem->setPos(-100, -100); 
} 


void MyGraphicsScene::mouseMoveEvent(QGraphicsSceneMouseEvent * e) 
{ 
// qDebug() << e->pos() << e->screenPos() << e->scenePos(); 
    emit mouseMoved(e->scenePos()); 
    m_labelProxy->setPos(e->scenePos()); 

    // Added these lines below to calculate and set the rotation. 
    // QVector2D v; 
    // v.setX(e->scenePos().x() - m_lineItem->pos().x()); 
    // v.setY(e->scenePos().y() - m_lineItem->pos().y()); 
    // m_lineItem->setRotation(qAtan2(v.y(), v.x())*180./(3.1459)); 


    QLineF line(m_lineItem->pos(), e->scenePos()); 
    m_lineItem->setRotation(360 - line.angle()); 
} 

희망.

+0

phyatt, move() 함수가 작동하지 않는 것 같습니다. 장면을 처음 만들었을 때 setMouseTracking을 true로 설정했지만 움직이지는 않습니다. – user3353931

+0

잘 작동합니다! 하지만 qwidget을 고정시키고 커서 방향을 가리 키도록했습니다. – user3353931

+0

커서를 가리키는 화살표가 있습니까? 게임의 고정 된 전류가 대상에서 대포를 뽑는 것처럼? – phyatt

관련 문제