在QLabel中插入可点击的链接,并检测点击此链接以引发操作 [英] Insert clickable link in QLabel and detect click on this link to provoke an action
问题描述
我想处理点击该应用程序中的链接:
当我点击输出文件链接,我想能够生成一个动作应用程序。
截至今天,链接在富文本QLabel中如下描述:
< a href =http://google.fr>< span style =text-decoration:underline; color:#0000ff;>输出文件& / span>< / a>
(由Qt Designer生成)
当点击时,它会打开默认的Web浏览器去Google。这不是我想要的;我想要像:
< a href =#browse_output>< span style = decoration:underline; color:#0000ff;>输出文件& quot;< / span>< / a>
并且能够检测到被点击的链接并做出相应的反应:
(伪代码)
if(link_clicked.toString()==#browse_output){
on_browse_output_clicked ;这是可能在Qt与QLabel(或某事接近)吗?如何?解决方案好的,对于那些感兴趣的人,我得到了答案:
- 停用
QLabel
$的 openExternalLinks
b $ b - 连接
的 linkActivated 信号, QLabel
到您的处理程序。
这是全部: linkActivated
提供链接在参数中引用的URL,所以我的伪代码完美地工作。
// 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();
}
}
I would like to handle a click to the link in this application of mine:
When I click on the "Output File" link, I would like to be able to generate an action in my application.
As of today, the link is described like this in the rich text QLabel:
<a href="http://google.fr"><span style=" text-decoration: underline; color:#0000ff;">Output File"</span></a>
(generated by Qt Designer)
When clicked, it will open the default web browser to go to Google. That's not what I want; I'd like something like:
<a href="#browse_output"><span style=" text-decoration: underline; color:#0000ff;">Output File"</span></a>
And be able to detect the link that's clicked and react accordingly:
(pseudo code)
if( link_clicked.toString() == "#browse_output" ){
on_browse_output_clicked();
}
Is this possible in Qt with a QLabel (or something approaching) ? How?
解决方案 Ok, for those interested, I got the answer:
- Disable the "
openExternalLinks
" property of the QLabel
- 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();
}
}
这篇关于在QLabel中插入可点击的链接,并检测点击此链接以引发操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!