在 QLabel 中插入可点击链接并检测点击此链接以引发操作

2023-10-18C/C++开发问题
2

本文介绍了在 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&quot;</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&quot;</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:

  1. 禁用QLabel
  2. openExternalLinks"属性
  3. QLabel 的信号 linkActivated 连接到您的处理程序.
  1. Disable the "openExternalLinks" property of the QLabel
  2. Connect the signal linkActivated of the QLabel to 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 中插入可点击链接并检测点击此链接以引发操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6