Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Detecting QWidget/QLabel Name on mousePressEvent
Qt 6.11 is out! See what's new in the release blog

Detecting QWidget/QLabel Name on mousePressEvent

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 5 Posters 2.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pistorinoj
    wrote on last edited by
    #1

    I am using QT 5.10.11 with VS2015 C++ on a Win10 platform.

    I have a series of qlabels that I want to detect mouse clicks on. I followed some examples like: https://www.youtube.com/watch?v=d0CDMtfefB4 and was actually successful detecting mouse clicks.

    Now, I need to determine which of several qlabels the mouse was clicked on.

    What is the best way of doing that?

    Thanks for any help.

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Use Event filter mechanism for this. Set an object name for each and every label or u can use the text which you set on the label itself. Create one event filter object and set this as filter to every QLabel. Inside the eventFilter(..) function you can get an object name or text of the label.

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      2
      • D Offline
        D Offline
        Devopia53
        wrote on last edited by
        #3

        Or subclassing like a YouTube video. like this:

        // in MyLabel.h
        #include <QLabel>
        
        class MyLabel : public QLabel
        {
            Q_OBJECT
        [...]
        ​signals:
        ​    void pressed(const QString &name, const QPoint &pos);
        ​protected:
            virtual void mousePressEvent(QMouseEvent *event) override
        ​    {
                if (event->button() == Qt::LeftButton)
                    emit pressed(text(), event->pos());​
        ​    }
        [...]
        };
        
        // in Dialog.cpp
        #include "mylabel.h"
        
        [...]
        for (int i = 0; i < 5; ++i) {
            auto label = new MyLabel(QString("TEST-%1").arg(i));
            connect(label, &MyLabel::pressed, [label](const QString &name, const QPoint &pos){
                qDebug() << label << name << pos;
            });
            [...]
        }
        [...]
        
        1 Reply Last reply
        1
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          There is also
          http://doc.qt.io/qt-5/qsignalmapper.html
          for such use case.

          raven-worxR 1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            There is also
            http://doc.qt.io/qt-5/qsignalmapper.html
            for such use case.

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @mrjj
            just a note: QSignalMapper is deprecated since there is the possibility to connect to lambda slots

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            2
            • mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi
              Good catched, just noticed
              "This class is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code." :)

              1 Reply Last reply
              0
              • P Offline
                P Offline
                pistorinoj
                wrote on last edited by
                #7

                Thanks all.

                One thing I notice is that using these approaches causes a significant performance hit.
                In my app, I am processing images.
                Before adding the events feature to the QLabel class, I was running at ~15fps.
                After adding this to QLabel, I dropped to ~8fps.
                In most cases, this probably does not matter but it does for me.

                mrjjM 1 Reply Last reply
                0
                • P pistorinoj

                  Thanks all.

                  One thing I notice is that using these approaches causes a significant performance hit.
                  In my app, I am processing images.
                  Before adding the events feature to the QLabel class, I was running at ~15fps.
                  After adding this to QLabel, I dropped to ~8fps.
                  In most cases, this probably does not matter but it does for me.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @pistorinoj
                  Hi
                  Adding a mousePressEvent override should not have any influence
                  on how fast it can draw its pixmap.
                  I would test again.
                  Or you could post teh actual code used and we could have a look.
                  Its not normal that it would reduce the "FPS" by 50% since code is only active if u click it.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved