Getting mouse click location of a label in qt

22,304

Solution 1

Yes, you can set up a signal on your CustomLabel class and have your overridden version of mousePressEvent emit it. i.e.

class CustomLabel : public QLabel
{
Q_OBJECT
signals:
    void mousePressed( const QPoint& );

public:
    CustomLabel( QWidget* parent = 0, Qt::WindowFlags f = 0 );
    CustomLabel( const QString& text, QWidget* parent = 0, Qt::WindowFlags f = 0 );

    void mousePressEvent( QMouseEvent* ev );
};

void CustomLabel::mousePressEvent( QMouseEvent* ev )
{
    const QPoint p = ev->pos();
    emit mousePressed( p );
}

CustomLabel::CustomLabel( QWidget * parent, Qt::WindowFlags f )
    : QLabel( parent, f ) {}

CustomLabel::CustomLabel( const QString& text, QWidget* parent, Qt::WindowFlags f )
    : QLabel( text, parent, f ) {}

The constructors just mimic those of the base QLabel and therefore simply pass their arguments straight on to the corresponding base constructors.

Solution 2

just like this :D

void CustomLabel::mousePressEvent(QMouseEvent *ev) 
{
QString x = QString::number(ev->x());
QString y = QString::number(ev->y());
qDebug() << x << "," << y;
}

Solution 3

Is it just me, or doesn't QMouseEvent already give the information you need?

int QMouseEvent::x () const

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

See also y() and pos().

int QMouseEvent::y () const

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

See also x() and pos().

Ref: http://doc.qt.nokia.com/4.7.old/qmouseevent.html#x

Share:
22,304
wrongusername
Author by

wrongusername

Updated on August 02, 2022

Comments

  • wrongusername
    wrongusername almost 2 years

    I googled around and found this forum thread in which the OP seems to have had the exact problem I am having. The question is, how would I inherit from QLabel and reimplement the mousepressed event? I'm guessing it would be something like this:

    class CustomLabel : public QLabel
    {
    public:
        //what about the constructors?
        void mousePressEvent ( QMouseEvent * ev );
    }
    
    void CustomLabel::mousePressEvent ( QMouseEvent * ev )
    {
        QPoint = ev->pos();
        //I want to have another function get the event position.
        //How would I achieve this? It's void!
        //Is there perhaps some way to set up a signal and slot with the position?
    }
    

    And after I have successfully created a CustomLabel class, how would I be able to put it in design view?

  • wrongusername
    wrongusername over 13 years
    Oh okay, sorry! How would I know if there's a mouseclick and then get the position?
  • wrongusername
    wrongusername over 13 years
    How would I add such a custom label to design view?
  • Troubadour
    Troubadour over 13 years
    @wrongusername: I'm afraid I don't know how to do that bit. Sorry.
  • wrongusername
    wrongusername over 13 years
    it's okay, don't worry, I ended up using an event filter instead