Need help in using graphicsView



  • Hi,
    Sorry I have already made one question concerning the same topic, but I didn't get a solution for my problem. I am using Qt designer and visual studio add-in version , so I will include only the .ccp, .h files of my project. I am using a button to open a png image in graphicsView. I have displayed the image and now I am trying to take the xy coordinates when I press with my mouse on the image. I have tried to make subclassing. I really have read many things, but I do not manage to find a solution. I created a project, which doesn't give any errors but still can't take the coordinates (displayed in two labels).I know is a simple application and I hope someone could help me, as I am beginner and really confused.Here is my code:

    ///////////// temp1.h ////////////
    @#ifndef TEMP1_H
    #define TEMP1_H

    #include <QtGui/QMainWindow>
    #include <QScrollArea>
    #include <QMouseEvent>
    #include <QPoint>
    #include <QTextEdit>
    #include <QLabel>

    #include <QGraphicsScene>
    #include <QGraphicsSceneMouseEvent>

    #include "ui_temp1.h"

    class temp1 : public QMainWindow, private Ui::temp1Class
    {
    Q_OBJECT

    public:
    temp1(QWidget *parent = 0, Qt::WFlags flags = 0);
    ~temp1();

    private:
    Ui::temp1Class ui;

    private slots:
    void open_graphicsView(void);

    protected:
    void mousePressEvent(QMouseEvent *event);

    };

    class graphicsView : public QGraphicsScene
    {
    public:
    graphicsView( QObject *parent = 0);

    protected:
    void mousePressEvent ( QGraphicsSceneMouseEvent * mouseEvent );
    };

    #endif // TEMP1_H@

    ////////////////// temp1.cpp //////////////////
    @#include "temp1.h"

    temp1::temp1(QWidget *parent, Qt::WFlags flags)
    : QMainWindow(parent, flags)
    {
    setupUi(this);
    QObject::connect(button, SIGNAL(clicked()), this, SLOT(open_graphicsView()));

    }

    temp1::~temp1()
    {

    }

    void temp1::open_graphicsView(void)
    {

    QGraphicsScene * scene = new QGraphicsScene();
    scene->addPixmap(QPixmap("C:/Users/.../test.png", 0, Qt::AutoColor));
    graphicsView->setScene(scene);

    }

    graphicsView::graphicsView( QObject *parent ) :
    QGraphicsScene( parent )
    {
    }

    void graphicsView::mousePressEvent( QGraphicsSceneMouseEvent * mouseEvent )
    {
    QPointF pos = mouseEvent->pos();
    double x = pos.x();
    double y = pos.y();

    QLabel *label_value_x;
    QLabel *label_value_y;

    label_value_x->setNum(x);
    label_value_y->setNum(y);
    }

    @

    ///////// main.cpp //////////
    @ #include "temp1.h"
    #include <QtGui/QApplication>

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    temp1 w;
    w.show();
    return a.exec();
    }
    @



  • it's hard to see the problem from the code segments. you can debug it to see if you can capture the mouse click event.

    but there's something i don't quite understand.

    your graphicsView class is a subclass of QGraphicsScene

    @
    class graphicsView : public QGraphicsScene
    @

    should it be a subclass of QGraphicsView??

    also if you look at http://doc.qt.nokia.com/latest/qgraphicsscenemouseevent.html

    there are three positions,

    QPointF pos () const
    QPointF scenePos () const
    QPoint screenPos () const

    maybe scenePos() is the one you should read.

    after you get the positions,

    @
    QLabel *label_value_x;
    QLabel *label_value_y;

    label_value_x->setNum(x);
    label_value_y->setNum(y);
    @

    you didn't add the two labels to your widget,



  • I have made the following changes and I do not get any errors.Again I display my image and cannot take the coordinates.I add the two labels to my widget.But when I tried to debug my code it seems that I don't even have access into my code.Do you know what is wrong?

    ////.h////////
    @class graphicsView : public QGraphicsView
    {
    public:
    graphicsView( QObject *parent = 0);

    QLabel *label_value_x;
    QLabel *label_value_y;

    protected:
    void mousePressEvent ( QMouseEvent * event );
    };@

    ////////.cpp /////////
    @graphicsView::graphicsView( QObject *parent )
    {
    }

    void graphicsView::mousePressEvent( QMouseEvent * event )
    {
    QPointF pos = event->pos();
    double x = pos.x();
    double y = pos.y();

    int k=3;
    label_value_x->setNum(x);
    label_value_y->setNum(y);
    @



  • where do you initialize label_value_x??
    where do you insert it into your window?



  • i just made a simple example for you. it has a qgraphicsview, and can capture the mouse position when you press the mouse and output it to labels.

    download here:

    http://dl.dropbox.com/u/13790288/graphicsexample.zip

    !http://dl.dropbox.com/u/13790288/Screenshot-MainWindow.png(example)!



  • Thank you so much for this examle.It is very helpful. But there is something that I cannot understand. To origin the constructor of canvas you use this:
    @Canvas::Canvas(): QGraphicsScene(0,0,1000,1000)
    {
    }@

    and you get a canvas with scrollbars and size (1000,1000), where the coordinates (0,0) are
    placed in the up-left corner of your canvas and the coordinates (1000,1000) are placed in the down-right corner of your canvas.

    If you want to create a canvas of size (214,256) without scrollbars and with the (0,0) value in the down-left corner fo your canvas, how could you declare this?

    Ihave tried to change the size from Qt designer, but I didn't have access in the size of graphicsView.



  • if your QGraphicsScene is very small, it will be centered inside the QGraphicsView.

    for example, if your QGraphicsView has a size (3000,3000), while your QGraphicsScene is just (100,100), the QGraphicsScene will appear in the center of the QGraphicsView.

    in this case, your up-left corner is not (0,0) but some negative value. that's why I have used (1000,1000), because i want to force the up-left corner be the origin.

    you can dig the QGraphicsView document, to see if you can put your QGraphicsScene to the position you want.

    or you can do the computation by yourself.

    for example position.y=QgraphicsScene.size.height-mouse.y (if you want to use the down-left point as your origin)

    you need to dig the document by yourself.

    http://doc.qt.nokia.com/latest/qgraphicsview.html



  • Ok, thanks a lot for your help.I think your example helps to undersand the solution of my problem



  • I apply this method to the code, but still I have a problem. When I press over an empty graphicsView a get the coordinates correctly. But when I load a png image in the graphics view, when I press on the image, the coordinates don't change and remain the same.Do you have something to suggest about this problem?

    Thanks


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.