Insert clickable link in QLabel and detect click on this link to provoke an action(在 QLabel 中插入可点击链接并检测点击此链接以引发操作)
问题描述
我想处理对我的这个应用程序中的链接的点击:
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.
截至今天,链接在富文本QLabel中是这样描述的:
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>
(由 Qt Designer 生成)
(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();
}
这在带有 QLabel(或接近的东西)的 Qt 中是可能的吗?怎么样?
Is this possible in Qt with a QLabel (or something approaching) ? How?
推荐答案
好的,有兴趣的朋友,我得到了答案:
Ok, for those interested, I got the answer:
- 禁用
QLabel的 - 将
QLabel的信号 linkActivated 连接到您的处理程序.
openExternalLinks"属性- Disable the "
openExternalLinks" property of theQLabel - Connect the signal linkActivated of the
QLabelto your handler.
就是这样:linkActivated 为您提供了链接在参数中引用的 URL,因此我的伪代码可以完美运行.
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 中插入可点击链接并检测点击此链接以引发操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 QLabel 中插入可点击链接并检测点击此链接以引发操作
基础教程推荐
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 这个宏可以转换成函数吗? 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
