How to make QLabel behave like a link without html?
If you using html then the line break you have to do with <br>
:
text = 'some <br> text'
link = QLabel()
link.setText('''<a href="http://stackoverflow.com/">{0}</a>'''.format(text))
Insert clickable link in QLabel and detect click on this link to provoke an action
Ok, for those interested, I got the answer:
- Disable the "
openExternalLinks
" property of theQLabel
- Connect the signal linkActivated of the
QLabel
to your handler.
That's all: linkActivated
gives you the URL that the link refers to in argument, so my pseudo code works perfectly.
// header
private slots:
void on_description_linkActivated(const QString &link);
// cpp
void KernelBuild::on_description_linkActivated(const QString &link)
{
if( link == "#browse_output" ){
on_outfilebtn_clicked();
}
}
How to add hyperlinks in Qt without QLabel?
You can easily have a widget open a link on click:
class Link : public QWidget {
Q_OBJECT
public:
Link(QUrl url, QWidget p = nullptr) : QWidget(p), _url(url) {}
QUrl _url;
void mouseReleaseEvent(QMouseEvent *) { QDesktopServices::openUrl(_url); }
}
You can avoid any extra signals and connections, and have each link widget store its own link internally, the url can be set on construction and changed at any time. Not using signals and slots makes it easier to change the link too, without having to disconnect previous connections.
IMO going for a signals and slots solution is only justified when you want different arbitrary behavior. In this case you always want the same - to open a particular link, so you might as well hardcode that and go for an easier and more computationally efficient solution.
Hyperlink to web page in QLabel using PySide
Your link are broken, Fix it should be fine;
from PyQt4.QtGui import QApplication, QLabel
myQApplication = QApplication([])
myQLabel = QLabel()
myQLabel.setText('''<a href='http://stackoverflow.com'>stackoverflow</a>''')
myQLabel.setOpenExternalLinks(True)
myQLabel.show()
myQApplication.exec_()
How do I change the color of a link temporarily when hovered in a QLabel?
You already have it. linkHovered
passes the URL when it emits you just need to check if it contains any URL or it's just an empty string. If it is not an empty string then change the color else remove it.
from PyQt5.QtWidgets import QLabel, QApplication
from PyQt5.QtGui import QMouseEvent, QCursor
from PyQt5 import QtCore
import sys
class SpecialLinkLabel(QLabel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.link_text = 'Click <a href="https://google.com" style="color: {color}">here</a> for something special!</a>'
self.setText(self.link_text)
self.linkHovered.connect(self.change_link_color)
def change_link_color(self, link):
print("Hovered: ", repr(link))
self.setText(self.link_text.format(color="red") if link else self.link_text.format(color="blue"))
if link:
self.setCursor(QCursor(QtCore.Qt.PointingHandCursor))
else:
self.unsetCursor()
app = QApplication(sys.argv)
special_label = SpecialLinkLabel()
special_label.show()
sys.exit(app.exec())
Related Topics
Is There a Reason Why Not to Use Link-Time Optimization (Lto)
How to Install Tensorflow on Windows
How to Get Python Exception Text
How to Get a List of Files in a Folder in Which the Files Are Sorted with Modified Date Time
What's a Good and Stable C++ Tree Implementation
How to Pan Images in Qgraphicsview
Is C++ Considered Weakly Typed? Why
Which Header Should I Include for 'Size_T'
Does Std::Array<> Guarantee Allocation on the Stack Only
Opencv Image Loading for Opengl Texture
Qt 5.5 Embed External Application into Qwidget
How to Use Clang++ with -Std=C++11 -Weverything -Werror
Best Way to Do Variant Visitation with Lambdas
Qthread Emits Finished() Signal But Isrunning() Returns True and Isfinished() Returns False