QLabel signals, linkActivated and linkHovered
-
@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.
-
@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.
-
@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.@mrjj , this is my implementation:
void clsQtLabel::enterEvent(QEvent* pEvent) { QString strLink; if ( pEvent->type() == QEvent::Enter ) { strLink = text(); emit linkHovered(strLink); } else if ( pEvent->type() == QEvent::Leave ) { emit linkHovered(strLink); } }
Now I still have my connect code in place for:
cnSignal = QObject::connect(this, &clsQtLabel::linkHovered ,this, &clsQtLabel::rptrLinkHovered);
It still doesn't get into my slot.
-
@mrjj , this is my implementation:
void clsQtLabel::enterEvent(QEvent* pEvent) { QString strLink; if ( pEvent->type() == QEvent::Enter ) { strLink = text(); emit linkHovered(strLink); } else if ( pEvent->type() == QEvent::Leave ) { emit linkHovered(strLink); } }
Now I still have my connect code in place for:
cnSignal = QObject::connect(this, &clsQtLabel::linkHovered ,this, &clsQtLabel::rptrLinkHovered);
It still doesn't get into my slot.
@SPlatten said in QLabel signals, linkActivated and linkHovered:
emit linkHovered(strLink);
does it execute this line?
-
@SPlatten said in QLabel signals, linkActivated and linkHovered:
emit linkHovered(strLink);
does it execute this line?
-
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.
-
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. -
@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.