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

问题描述 投票:0回答:1

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

my application

当我单击“输出文件”链接时,我希望能够在我的应用程序中生成一个操作。

截至今天,该链接在富文本 QLabel 中是这样描述的:

<a href="http://google.fr"><span style=" text-decoration: underline; color:#0000ff;">Output File&quot;</span></a>

(由 Qt Designer 生成)

单击后,它将打开默认的网络浏览器以访问 Google。那不是我想要的;我想要这样的东西:

<a href="#browse_output"><span style=" text-decoration: underline; color:#0000ff;">Output File&quot;</span></a>

并能够检测被单击的链接并做出相应反应:

(pseudo code)

if( link_clicked.toString() == "#browse_output" ){
    on_browse_output_clicked();
}

这在 Qt 中用 QLabel (或类似的东西)可能吗?怎么办?

c++ qt hyperlink qlabel
1个回答
40
投票
  1. 禁用
    openExternalLinks
     的“
    QLabel
  2. ”属性
  3. QLabel 的信号
    linkActivated
    连接到您的处理程序。

就这样:

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();
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.