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

2022-01-02 00:00:00 qt hyperlink qlabel c++

我想处理对我的这个应用程序中的链接的点击:

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();
  }
}

相关文章