Question

I'm a new java / qt programmer and i'm trying to build a gui using java and qt jambi.

I put a pixmap inside a label and it works good, but, now i need to get the image coordinates when i click on the pixmap inside the label. I don't need the QWidget coordinates. I just need the pixmap coordinates to work over the image on the clicked point.

i think i should override the mousePressEvent event, but i don't know how to attach it to "pixmap onclick".

Can you please make me an easy example?

I hope i have explained my problem in the right way, my english is not awesome, sorry!

Was it helpful?

Solution

i think i should override the mousePressEvent event, but i don't know how to attach it to "pixmap onclick".

You seem to be confusing events and signals together. The former is a QEvent, the latter is a signal. What you need is the former, and you do not need the latter.

You would need to subclass the QLabel class and write something like this when writing C++ (not Qt Jambi in Java, but you can take it as pseudo-code either way).

#include <QLabel>
#include <QPoint>
#include <QMouseEvent>

class MyLabel : public QLabel
{
    Q_OBJECT
    public:
        MyLabel(QObject *parent) : QLabel(parent)

     ...

     protected:
         void QWidget::mousePressEvent(QMouseEvent * event)
         {
             ...
             QPoint localPosition = event.pos();
             // Work on the desired point
             ...
         }
};

So, depending your use case, you would need to look into the documentation of these methods:

void QWidget::mousePressEvent(QMouseEvent * event) [virtual protected]

This event handler, for event event, can be reimplemented in a subclass to receive mouse press events for the widget.

If you create new widgets in the mousePressEvent() the mouseReleaseEvent() may not end up where you expect, depending on the underlying window system (or X11 window manager), the widgets' location and maybe more.

The default implementation implements the closing of popup widgets when you click outside the window. For other widget types it does nothing.

and then this is the method to get the local position that also works with Qt 4. You may wish to consider localPos() or other methods introduced in Qt 5 if you need more precision with float, etc.

QPoint QMouseEvent::pos() const

Returns the position of the mouse cursor, relative to the widget that received the event.

If you move the widget as a result of the mouse event, use the global position returned by globalPos() to avoid a shaking motion.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top