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. Qlabel clicked event
QtWS25 Last Chance

Qlabel clicked event

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 22.8k Views
  • 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.
  • L Offline
    L Offline
    leafjungle
    wrote on last edited by
    #1

    As we know, we can add a clicked event to a QLabel by:

    connect(ui.label1, signal( clicked() ), this, slot(hello()));

    Thus the responding function is hello():

    void hello(){
    //How Can I get the clicked position of mouse
    }

    I do not know how to get the position of the mouse. I mean the co-ordinate values (x,y)

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      There is no click signal for QLabel.
      If you need one then you need to reimplement its mousePressEvent or mouseReleaseEvent functions and then from it emit your custom signal.
      You can get the click position by QMouseEvent::pos ().
      Check "mouseReleaseEvent":http://qt-project.org/doc/qt-5.0/qtwidgets/qlabel.html#mouseReleaseEvent and "qmouseevent":http://qt-project.org/doc/qt-5.0/qtgui/qmouseevent.html#pos for moe details

      157

      1 Reply Last reply
      0
      • jazzycamelJ Offline
        jazzycamelJ Offline
        jazzycamel
        wrote on last edited by
        #3

        As p3c0 says, you'll have to reimplemented one of the mouse events and emit a custom signal. Something like the following:

        label.h
        @
        ...
        class Label : public QLabel {
        Q_OBJECT

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

        protected:
        void mousePressEvent(QMouseEvent *ev);

        signals:
        void clicked(QPoint pos);
        };
        ...
        @

        label.cpp
        @
        ...
        Label::Label(QWidget *parent, Qt::WindowFlags f):QLabel(parent,f){}
        Label::Label(const QString &text, QWidget *parent, Qt::WindowFlags f):QLabel(text,parent,f){}
        Label::~Label(){}

        void Label::mousePressEvent(QMouseEvent *ev){
        emit clicked(ev->pos());
        }
        ...
        @

        Hope this helps ;o)

        For the avoidance of doubt:

        1. All my code samples (C++ or Python) are tested before posting
        2. As of 23/03/20, my Python code is formatted to PEP-8 standards using black from the PSF (https://github.com/psf/black)
        1 Reply Last reply
        0
        • L Offline
          L Offline
          leafjungle
          wrote on last edited by
          #4

          Thank you very much for you kindly reply.
          I am using visual studio 2010 and QT addin. Each time I rebuild my application, the "ui_xxx.h" is modified automatically and my modification is lost (including the declaration of my class "MyLabel label").

          [quote author="jazzycamel" date="1380876254"]As p3c0 says, you'll have to reimplemented one of the mouse events and emit a custom signal. Something like the following:

          label.h
          @
          ...
          class Label : public QLabel {
          Q_OBJECT

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

          protected:
          void mousePressEvent(QMouseEvent *ev);

          signals:
          void clicked(QPoint pos);
          };
          ...
          @

          label.cpp
          @
          ...
          Label::Label(QWidget *parent, Qt::WindowFlags f):QLabel(parent,f){}
          Label::Label(const QString &text, QWidget *parent, Qt::WindowFlags f):QLabel(text,parent,f){}
          Label::~Label(){}

          void Label::mousePressEvent(QMouseEvent *ev){
          emit clicked(ev->pos());
          }
          ...
          @

          Hope this helps ;o)[/quote]

          1 Reply Last reply
          0
          • raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            you shouldn't do your implementation in "ui_xxx.h". This is generated - as i already noticed ;) - dynamically by Qt.
            Create a new H- and CPP-file instead, or at least add it to an existing implementation.

            --- 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
            0
            • L Offline
              L Offline
              leafjungle
              wrote on last edited by
              #6

              I know you point. I create a MyLabel.h and MyLabel.cpp.
              So I have to use it:
              MyLabel label; //this sentence should be put in ui_xxx.h, right?

              1 Reply Last reply
              0
              • raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on last edited by
                #7

                again...you never edit the "ui_xxx.h" at all. You most probably create a instance there where you call setupUI()

                --- 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
                0
                • L Offline
                  L Offline
                  leafjungle
                  wrote on last edited by
                  #8

                  Still I am puzzled. in which file should I create an instance of MyLabel? And how can I make the GUI use MyLabel instead of the default QLabel?

                  [quote author="raven-worx" date="1380892316"]again...you never edit the "ui_xxx.h" at all. You most probably create a instance there where you call setupUI()[/quote]

                  1 Reply Last reply
                  0
                  • p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

                    AFAIK, there are 2 ways,

                    1. Add a QLabel in your form by dragging a QLabel widget from the widgetbox and right click on that QLabel > promote to you subclassed QLabel (you need to set *.h and *.cpp )

                    2. From you QDialog or QMainWindow include the subclassed QLabel's header file and create an instance there.

                    157

                    1 Reply Last reply
                    1

                    • Login

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