QLabel signals, linkActivated and linkHovered
-
There are several bugs around QLabel signals, I created a bug report some time ago:
https://bugreports.qt.io/browse/QTBUG-85246
There doesn't seem to be any updates, can anyone else verify this behaviour? It's now been present for several updates and is still present in the current release.
I have a QLabel which I have created a link, for example, I set the label text using the setText function to:
"<a href="http://google.com" style="color:#000000;text-decoration:none;">Google</a>"
I then call:
setOpenExternalLinks(true);
I also connect signals:
const char clsQtLabel::mscszQtSignalLinkActivated[] = "linkActivated"; const char clsQtLabel::mscszQtSignalLinkHovered[] = "linkHovered"; 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); }
When I move the mouse onto the label I get the linkHovered signal, once and once only, if I move the mouse out and then hover over it again I do not get the signal again. I believe that an additional signal should be added to the QLabel as well as fixing the behaviour as it currently is.
Add linkMouseOut or something like that to indicate that, and then linkHovered should be reset to trigger when the cursor moves onto it again.
-
Now thats fixed, more testing and with the same label which has been set-up as link to the Google website by setting the text to:
%3ca href%3d%22http://google.com%22%3eGoogle%3c/a%3e
and calling the function:
setOpenExternalLinks
I'm connecting the signals:
linkActivated and linkHovered
I've verified that the connection to each is successful by examining the return from the connect calls. The linkHovered now works perfectly, but I'm not getting the linkActivated when I click on the link, I have a breakpoint in the slot and it never gets there, the browser is launched correctly with the Google web-site.
I found this:
https://stackoverflow.com/questions/16581425/insert-clickable-link-in-qlabel-and-detect-click-on-this-link-to-provoke-an-actiAnd calling setOpenExternalLinks with false does allow the linkActivated signal to function correctly, but then it doesn't open the browser. This looks like a bug as I cannot see why you would have a linkActivated signal that only works when the label is not configured to function as a link.
@SPlatten said in QLabel signals, linkActivated and linkHovered:
setOpenExternalLinks
Hi
Docs says
"Specifies whether QLabel should automatically open links using QDesktopServices::openUrl() instead of emitting the linkActivated() signal."
So that seems as expected. -
There are several bugs around QLabel signals, I created a bug report some time ago:
https://bugreports.qt.io/browse/QTBUG-85246
There doesn't seem to be any updates, can anyone else verify this behaviour? It's now been present for several updates and is still present in the current release.
I have a QLabel which I have created a link, for example, I set the label text using the setText function to:
"<a href="http://google.com" style="color:#000000;text-decoration:none;">Google</a>"
I then call:
setOpenExternalLinks(true);
I also connect signals:
const char clsQtLabel::mscszQtSignalLinkActivated[] = "linkActivated"; const char clsQtLabel::mscszQtSignalLinkHovered[] = "linkHovered"; 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); }
When I move the mouse onto the label I get the linkHovered signal, once and once only, if I move the mouse out and then hover over it again I do not get the signal again. I believe that an additional signal should be added to the QLabel as well as fixing the behaviour as it currently is.
Add linkMouseOut or something like that to indicate that, and then linkHovered should be reset to trigger when the cursor moves onto it again.
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?
-
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?
-
@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?
@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 -
@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 -
@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 firstlinkHovered("http://google.com")
.
Then I move the mouse out of "Google", there is alinkHovered("")
.
So when I move the mouse back to "Google" again, there is anotherlinkHovered("http://google.com")
.
Everything seems fine to me. -
@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 firstlinkHovered("http://google.com")
.
Then I move the mouse out of "Google", there is alinkHovered("")
.
So when I move the mouse back to "Google" again, there is anotherlinkHovered("http://google.com")
.
Everything seems fine to me. -
@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
@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 ""
-
@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 ""
-
@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);
@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 triedsetOpenExternalLinks(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 ""
-
@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 triedsetOpenExternalLinks(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 ""
-
@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.
-
@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. -
@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@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-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-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;
} -
@SPlatten
Hi
enter and leave events are virtual functions and not signals.
hehe ninjaed by @J-Hilk :) -
@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 :) -
@mrjj , what I meant was why do it a different way when signals and slots are used for so much of everything else already?
@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.