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 signals, linkActivated and linkHovered
Forum Updated to NodeBB v4.3 + New Features

QLabel signals, linkActivated and linkHovered

Scheduled Pinned Locked Moved Solved General and Desktop
31 Posts 4 Posters 5.5k 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.
  • J.HilkJ J.Hilk

    @SPlatten

    can't you simply overwrite https://doc.qt.io/qt-5/qwidget.html#enterEvent and https://doc.qt.io/qt-5/qwidget.html#leaveEvent to make your own hovered signal?

    SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #3

    @J-Hilk , if I was aware of they're existence I would have done so, but the fact that they exist, shouldn't QLabel be correct?

    Kind Regards,
    Sy

    J.HilkJ 1 Reply Last reply
    0
    • SPlattenS SPlatten

      @J-Hilk , if I was aware of they're existence I would have done so, but the fact that they exist, shouldn't QLabel be correct?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by J.Hilk
      #4

      @SPlatten QWidget is the base class of QLabel and its where they are declared

      IIRC you also have to set https://doc.qt.io/qt-5/qwidget.html#mouseTracking-prop
      to true, to receive the enter and exit events


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      SPlattenS 2 Replies Last reply
      1
      • J.HilkJ J.Hilk

        @SPlatten QWidget is the base class of QLabel and its where they are declared

        IIRC you also have to set https://doc.qt.io/qt-5/qwidget.html#mouseTracking-prop
        to true, to receive the enter and exit events

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #5

        @J-Hilk That I already do.

        Kind Regards,
        Sy

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Bonnie
          wrote on last edited by Bonnie
          #6

          @SPlatten said in QLabel signals, linkActivated and linkHovered:

          <a href="http://google.com" style="color:#000000;text-decoration:none;">Google</a>

          I've tested a QLabel with above content in Windows / Qt5.12.7 (without setting mouseTracking prop).
          When I move the mouse to "Google", I get the first linkHovered("http://google.com").
          Then I move the mouse out of "Google", there is a linkHovered("").
          So when I move the mouse back to "Google" again, there is another linkHovered("http://google.com").
          Everything seems fine to me.

          SPlattenS 1 Reply Last reply
          2
          • B Bonnie

            @SPlatten said in QLabel signals, linkActivated and linkHovered:

            <a href="http://google.com" style="color:#000000;text-decoration:none;">Google</a>

            I've tested a QLabel with above content in Windows / Qt5.12.7 (without setting mouseTracking prop).
            When I move the mouse to "Google", I get the first linkHovered("http://google.com").
            Then I move the mouse out of "Google", there is a linkHovered("").
            So when I move the mouse back to "Google" again, there is another linkHovered("http://google.com").
            Everything seems fine to me.

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #7

            @Bonnie , can you please post a snippet of your code than I can compare, as I really haven't done anything other than create the label and set-up the connection.

            I'm using Qt 5.14.2

            Kind Regards,
            Sy

            B 1 Reply Last reply
            0
            • SPlattenS SPlatten

              @Bonnie , can you please post a snippet of your code than I can compare, as I really haven't done anything other than create the label and set-up the connection.

              I'm using Qt 5.14.2

              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #8

              @SPlatten Sure, here is my main.cpp

              #include <QApplication>
              #include <QMainWindow>
              #include <QLabel>
              #include <QDebug>
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  QMainWindow w;
                  w.resize(320, 240);
              
                  QLabel* label = new QLabel(&w);
              
                  QObject::connect(label, &QLabel::linkHovered, [=](const QString& link){
                      qDebug() << "linkHovered"<<link;
                  });
              
                  label->setFrameShape(QFrame::StyledPanel);
                  label->setText("<a href=\"http://google.com\" style=\"color:#000000;text-decoration:none;\">Google</a>");
                  w.show();
              
                  return a.exec();
              }
              

              While moving my mouse I get:

              linkHovered "http://google.com"
              linkHovered ""
              linkHovered "http://google.com"
              linkHovered ""
              linkHovered "http://google.com"
              linkHovered ""
              
              SPlattenS 1 Reply Last reply
              4
              • B Bonnie

                @SPlatten Sure, here is my main.cpp

                #include <QApplication>
                #include <QMainWindow>
                #include <QLabel>
                #include <QDebug>
                
                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                
                    QMainWindow w;
                    w.resize(320, 240);
                
                    QLabel* label = new QLabel(&w);
                
                    QObject::connect(label, &QLabel::linkHovered, [=](const QString& link){
                        qDebug() << "linkHovered"<<link;
                    });
                
                    label->setFrameShape(QFrame::StyledPanel);
                    label->setText("<a href=\"http://google.com\" style=\"color:#000000;text-decoration:none;\">Google</a>");
                    w.show();
                
                    return a.exec();
                }
                

                While moving my mouse I get:

                linkHovered "http://google.com"
                linkHovered ""
                linkHovered "http://google.com"
                linkHovered ""
                linkHovered "http://google.com"
                linkHovered ""
                
                SPlattenS Offline
                SPlattenS Offline
                SPlatten
                wrote on last edited by SPlatten
                #9

                @Bonnie , Thank you, can you try a conventional connect with a slot in C++ as thats what I've been using. Also add a connect for linkActivated too. You should also call:

                label->setOpenExternalLinks(true);
                

                Kind Regards,
                Sy

                B 1 Reply Last reply
                0
                • SPlattenS SPlatten

                  @Bonnie , Thank you, can you try a conventional connect with a slot in C++ as thats what I've been using. Also add a connect for linkActivated too. You should also call:

                  label->setOpenExternalLinks(true);
                  
                  B Offline
                  B Offline
                  Bonnie
                  wrote on last edited by Bonnie
                  #10

                  @SPlatten
                  I actually do test in a slot of a subclassed QMainWindow.
                  Above code is just to make it looks more simple in a single cpp file.
                  And I also tried setOpenExternalLinks(true), still the same.

                  Here are all the files:
                  main.cpp:

                  #include <QApplication>
                  #include "mainwindow.h"
                  
                  int main(int argc, char *argv[])
                  {
                      QApplication a(argc, argv);
                  
                      MainWindow w;
                      w.show();
                  
                      return a.exec();
                  }
                  

                  mainwindow.h:

                  #ifndef MAINWINDOW_H
                  #define MAINWINDOW_H
                  
                  #include <QMainWindow>
                  
                  class MainWindow : public QMainWindow
                  {
                      Q_OBJECT
                  
                  public:
                      MainWindow(QWidget *parent = nullptr);
                  private slots:
                      void label_linkHovered(const QString &link);
                      void label_linkActivated(const QString &link);
                  };
                  #endif // MAINWINDOW_H
                  

                  mainwindow.cpp:

                  #include "mainwindow.h"
                  
                  #include <QLabel>
                  #include <QDebug>
                  
                  MainWindow::MainWindow(QWidget *parent)
                      : QMainWindow(parent)
                  {
                      resize(320, 240);
                      QLabel *label = new QLabel(this);
                      connect(label, &QLabel::linkHovered, this, &MainWindow::label_linkHovered);
                      connect(label, &QLabel::linkActivated, this, &MainWindow::label_linkActivated);
                      label->setFrameShape(QFrame::StyledPanel);
                      label->setText("<a href=\"http://google.com\" style=\"color:#000000;text-decoration:none;\">Google</a>");
                      label->setOpenExternalLinks(true);
                  }
                  
                  void MainWindow::label_linkHovered(const QString &link)
                  {
                      qDebug() << "linkHovered" << link;
                  }
                  
                  void MainWindow::label_linkActivated(const QString &link)
                  {
                      qDebug() << "linkActivated" << link;
                  }
                  

                  Output still:

                  linkHovered "http://google.com"
                  linkHovered ""
                  linkHovered "http://google.com"
                  linkHovered ""
                  linkHovered "http://google.com"
                  linkHovered ""
                  
                  SPlattenS 1 Reply Last reply
                  4
                  • B Bonnie

                    @SPlatten
                    I actually do test in a slot of a subclassed QMainWindow.
                    Above code is just to make it looks more simple in a single cpp file.
                    And I also tried setOpenExternalLinks(true), still the same.

                    Here are all the files:
                    main.cpp:

                    #include <QApplication>
                    #include "mainwindow.h"
                    
                    int main(int argc, char *argv[])
                    {
                        QApplication a(argc, argv);
                    
                        MainWindow w;
                        w.show();
                    
                        return a.exec();
                    }
                    

                    mainwindow.h:

                    #ifndef MAINWINDOW_H
                    #define MAINWINDOW_H
                    
                    #include <QMainWindow>
                    
                    class MainWindow : public QMainWindow
                    {
                        Q_OBJECT
                    
                    public:
                        MainWindow(QWidget *parent = nullptr);
                    private slots:
                        void label_linkHovered(const QString &link);
                        void label_linkActivated(const QString &link);
                    };
                    #endif // MAINWINDOW_H
                    

                    mainwindow.cpp:

                    #include "mainwindow.h"
                    
                    #include <QLabel>
                    #include <QDebug>
                    
                    MainWindow::MainWindow(QWidget *parent)
                        : QMainWindow(parent)
                    {
                        resize(320, 240);
                        QLabel *label = new QLabel(this);
                        connect(label, &QLabel::linkHovered, this, &MainWindow::label_linkHovered);
                        connect(label, &QLabel::linkActivated, this, &MainWindow::label_linkActivated);
                        label->setFrameShape(QFrame::StyledPanel);
                        label->setText("<a href=\"http://google.com\" style=\"color:#000000;text-decoration:none;\">Google</a>");
                        label->setOpenExternalLinks(true);
                    }
                    
                    void MainWindow::label_linkHovered(const QString &link)
                    {
                        qDebug() << "linkHovered" << link;
                    }
                    
                    void MainWindow::label_linkActivated(const QString &link)
                    {
                        qDebug() << "linkActivated" << link;
                    }
                    

                    Output still:

                    linkHovered "http://google.com"
                    linkHovered ""
                    linkHovered "http://google.com"
                    linkHovered ""
                    linkHovered "http://google.com"
                    linkHovered ""
                    
                    SPlattenS Offline
                    SPlattenS Offline
                    SPlatten
                    wrote on last edited by
                    #11

                    @Bonnie , please add:

                    connect(label, &QLabel::linkActivated, this, &MainWindow::label_linkActivated);
                    

                    Just because I have that too, your version of Qt is also quite a lot older than I'm using...apart from that there is no difference.

                    Kind Regards,
                    Sy

                    B 1 Reply Last reply
                    0
                    • SPlattenS SPlatten

                      @Bonnie , please add:

                      connect(label, &QLabel::linkActivated, this, &MainWindow::label_linkActivated);
                      

                      Just because I have that too, your version of Qt is also quite a lot older than I'm using...apart from that there is no difference.

                      B Offline
                      B Offline
                      Bonnie
                      wrote on last edited by Bonnie
                      #12

                      @SPlatten
                      Fine, I've updated the testing code in the above.
                      But the output is still the same.
                      My current project started from 5.12, so I have only 5.12 installed.

                      SPlattenS 1 Reply Last reply
                      1
                      • B Bonnie

                        @SPlatten
                        Fine, I've updated the testing code in the above.
                        But the output is still the same.
                        My current project started from 5.12, so I have only 5.12 installed.

                        SPlattenS Offline
                        SPlattenS Offline
                        SPlatten
                        wrote on last edited by
                        #13

                        @Bonnie , ok, thanks, so its only the version that is different. Shouldn't be to hard for anyone with the latest version to try.

                        Kind Regards,
                        Sy

                        1 Reply Last reply
                        0
                        • J.HilkJ J.Hilk

                          @SPlatten QWidget is the base class of QLabel and its where they are declared

                          IIRC you also have to set https://doc.qt.io/qt-5/qwidget.html#mouseTracking-prop
                          to true, to receive the enter and exit events

                          SPlattenS Offline
                          SPlattenS Offline
                          SPlatten
                          wrote on last edited by SPlatten
                          #14

                          @J-Hilk , I've tried:

                          cnSignal = QObject::connect(this, &clsQtLabel::enterEvent
                                                     ,this, &clsQtLabel::rptrEnterEvent);
                          

                          clsQtLabel is the name of my derived class. The slot:

                          void clsQtLabel::rptrEnterEvent(QEvent* pEvent) {
                              const QString cstrSignal("enterEvent");
                              QJsonObject objJSON;
                              if ( mpobjNode->blnCheckSubscribers(cstrSignal, &objJSON) == true ) {
                                  QJsonObject objParam;
                                  //objParam["event"] = strLink;
                                  objJSON[clsXMLnode::mscszAttrParameters] = objParam;
                                  emit mpobjNode->commonRptdSignal(objJSON);
                              }
                          }
                          

                          Something is wrong because the connect returns null and does not connect. This is from:

                          ```
                          

                          QMetaObject::Connection clsQtLabel::connect(clsSignal* pobjSignal) {
                          QMetaObject::Connection cnSignal;
                          if ( pobjSignal == nullptr ) {
                          //Cannot proceed without signal or subscriber, abort!
                          return cnSignal;
                          }
                          QString strSignal = pobjSignal->strGetSignal();
                          if ( strSignal.compare(clsQtLabel::mscszQtSignalEnterEvent) == 0 ) {
                          cnSignal = QObject::connect(this, &clsQtLabel::enterEvent
                          ,this, &clsQtLabel::rptrEnterEvent);
                          } else if ( strSignal.compare(clsQtLabel::mscszQtSignalLinkActivated) == 0 ) {
                          cnSignal = QObject::connect(this, &clsQtLabel::linkActivated
                          ,this, &clsQtLabel::rptrLinkActivated);
                          } else if ( strSignal.compare(clsQtLabel::mscszQtSignalLinkHovered) == 0 ) {
                          cnSignal = QObject::connect(this, &clsQtLabel::linkHovered
                          ,this, &clsQtLabel::rptrLinkHovered);
                          }
                          return cnSignal;
                          }

                          Kind Regards,
                          Sy

                          J.HilkJ mrjjM 2 Replies Last reply
                          0
                          • SPlattenS SPlatten

                            @J-Hilk , I've tried:

                            cnSignal = QObject::connect(this, &clsQtLabel::enterEvent
                                                       ,this, &clsQtLabel::rptrEnterEvent);
                            

                            clsQtLabel is the name of my derived class. The slot:

                            void clsQtLabel::rptrEnterEvent(QEvent* pEvent) {
                                const QString cstrSignal("enterEvent");
                                QJsonObject objJSON;
                                if ( mpobjNode->blnCheckSubscribers(cstrSignal, &objJSON) == true ) {
                                    QJsonObject objParam;
                                    //objParam["event"] = strLink;
                                    objJSON[clsXMLnode::mscszAttrParameters] = objParam;
                                    emit mpobjNode->commonRptdSignal(objJSON);
                                }
                            }
                            

                            Something is wrong because the connect returns null and does not connect. This is from:

                            ```
                            

                            QMetaObject::Connection clsQtLabel::connect(clsSignal* pobjSignal) {
                            QMetaObject::Connection cnSignal;
                            if ( pobjSignal == nullptr ) {
                            //Cannot proceed without signal or subscriber, abort!
                            return cnSignal;
                            }
                            QString strSignal = pobjSignal->strGetSignal();
                            if ( strSignal.compare(clsQtLabel::mscszQtSignalEnterEvent) == 0 ) {
                            cnSignal = QObject::connect(this, &clsQtLabel::enterEvent
                            ,this, &clsQtLabel::rptrEnterEvent);
                            } else if ( strSignal.compare(clsQtLabel::mscszQtSignalLinkActivated) == 0 ) {
                            cnSignal = QObject::connect(this, &clsQtLabel::linkActivated
                            ,this, &clsQtLabel::rptrLinkActivated);
                            } else if ( strSignal.compare(clsQtLabel::mscszQtSignalLinkHovered) == 0 ) {
                            cnSignal = QObject::connect(this, &clsQtLabel::linkHovered
                            ,this, &clsQtLabel::rptrLinkHovered);
                            }
                            return cnSignal;
                            }

                            J.HilkJ Offline
                            J.HilkJ Offline
                            J.Hilk
                            Moderators
                            wrote on last edited by
                            #15

                            @SPlatten enterEvent is not a signal but a function. it gets called automatically and you're supposed to override it!


                            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                            Q: What's that?
                            A: It's blue light.
                            Q: What does it do?
                            A: It turns blue.

                            1 Reply Last reply
                            3
                            • SPlattenS SPlatten

                              @J-Hilk , I've tried:

                              cnSignal = QObject::connect(this, &clsQtLabel::enterEvent
                                                         ,this, &clsQtLabel::rptrEnterEvent);
                              

                              clsQtLabel is the name of my derived class. The slot:

                              void clsQtLabel::rptrEnterEvent(QEvent* pEvent) {
                                  const QString cstrSignal("enterEvent");
                                  QJsonObject objJSON;
                                  if ( mpobjNode->blnCheckSubscribers(cstrSignal, &objJSON) == true ) {
                                      QJsonObject objParam;
                                      //objParam["event"] = strLink;
                                      objJSON[clsXMLnode::mscszAttrParameters] = objParam;
                                      emit mpobjNode->commonRptdSignal(objJSON);
                                  }
                              }
                              

                              Something is wrong because the connect returns null and does not connect. This is from:

                              ```
                              

                              QMetaObject::Connection clsQtLabel::connect(clsSignal* pobjSignal) {
                              QMetaObject::Connection cnSignal;
                              if ( pobjSignal == nullptr ) {
                              //Cannot proceed without signal or subscriber, abort!
                              return cnSignal;
                              }
                              QString strSignal = pobjSignal->strGetSignal();
                              if ( strSignal.compare(clsQtLabel::mscszQtSignalEnterEvent) == 0 ) {
                              cnSignal = QObject::connect(this, &clsQtLabel::enterEvent
                              ,this, &clsQtLabel::rptrEnterEvent);
                              } else if ( strSignal.compare(clsQtLabel::mscszQtSignalLinkActivated) == 0 ) {
                              cnSignal = QObject::connect(this, &clsQtLabel::linkActivated
                              ,this, &clsQtLabel::rptrLinkActivated);
                              } else if ( strSignal.compare(clsQtLabel::mscszQtSignalLinkHovered) == 0 ) {
                              cnSignal = QObject::connect(this, &clsQtLabel::linkHovered
                              ,this, &clsQtLabel::rptrLinkHovered);
                              }
                              return cnSignal;
                              }

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

                              @SPlatten
                              Hi
                              enter and leave events are virtual functions and not signals.
                              hehe ninjaed by @J-Hilk :)

                              SPlattenS 1 Reply Last reply
                              2
                              • mrjjM mrjj

                                @SPlatten
                                Hi
                                enter and leave events are virtual functions and not signals.
                                hehe ninjaed by @J-Hilk :)

                                SPlattenS Offline
                                SPlattenS Offline
                                SPlatten
                                wrote on last edited by
                                #17

                                @mrjj , bugger, why why why?

                                Kind Regards,
                                Sy

                                mrjjM 1 Reply Last reply
                                0
                                • SPlattenS SPlatten

                                  @mrjj , bugger, why why why?

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

                                  @SPlatten
                                  That is just normal OOP.
                                  Also, one can subclass and emit a signal in those event handlers to have it as signals also.
                                  But default its not signals :)

                                  SPlattenS 1 Reply Last reply
                                  1
                                  • mrjjM mrjj

                                    @SPlatten
                                    That is just normal OOP.
                                    Also, one can subclass and emit a signal in those event handlers to have it as signals also.
                                    But default its not signals :)

                                    SPlattenS Offline
                                    SPlattenS Offline
                                    SPlatten
                                    wrote on last edited by
                                    #19

                                    @mrjj , what I meant was why do it a different way when signals and slots are used for so much of everything else already?

                                    Kind Regards,
                                    Sy

                                    mrjjM 1 Reply Last reply
                                    0
                                    • SPlattenS SPlatten

                                      @mrjj , what I meant was why do it a different way when signals and slots are used for so much of everything else already?

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

                                      @SPlatten
                                      well one reason is that signal and slot cannot return values as such.
                                      So it wont be useful for normal OOP where you overwrite virtual functions to alter logic.
                                      Its not easy to do with signals alone. Also very, very often you want to call base class version of the function you overrride as you only want to extend it and keep old logic too and in this case a signal system would be very clunky.

                                      btw. in your use case if you want hover events but dont feel like subclassing. you could use eventfilter and cheat that way.

                                      SPlattenS 1 Reply Last reply
                                      1
                                      • mrjjM mrjj

                                        @SPlatten
                                        well one reason is that signal and slot cannot return values as such.
                                        So it wont be useful for normal OOP where you overwrite virtual functions to alter logic.
                                        Its not easy to do with signals alone. Also very, very often you want to call base class version of the function you overrride as you only want to extend it and keep old logic too and in this case a signal system would be very clunky.

                                        btw. in your use case if you want hover events but dont feel like subclassing. you could use eventfilter and cheat that way.

                                        SPlattenS Offline
                                        SPlattenS Offline
                                        SPlatten
                                        wrote on last edited by
                                        #21

                                        @mrjj , thanks, I'm no newb when it comes to coding, but in this case I fail to see why enterEvent couldn't be a signal, its perfect for it.

                                        Kind Regards,
                                        Sy

                                        mrjjM 1 Reply Last reply
                                        0
                                        • SPlattenS SPlatten

                                          @mrjj , thanks, I'm no newb when it comes to coding, but in this case I fail to see why enterEvent couldn't be a signal, its perfect for it.

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

                                          @SPlatten
                                          Well often its used for hover effect and used internally by same Widget so im not sure
                                          its a universal use case.
                                          But good news is that you can make them signals :)
                                          If you want.

                                          SPlattenS 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