繁体   English   中英

Qt 4.8.6中的qlabel mailto链接

[英]qlabel mailto link in Qt 4.8.6

我跟着像前面的问题作出批示,

所以现在,如果我将链接放到常规页面,它可以使用默认浏览器打开。 但是,如果我想从QT QLabel 4.8.6打开mailto链接,该链接什么都不做。 我究竟做错了什么?

这是代码:

UpgradeMessageDialog* umd = new UpgradeMessageDialog();
umd->ui->label->setOpenExternalLinks(true);
umd->ui->label->setTextInteractionFlags(Qt::TextBrowserInteraction);
umd->ui->label->setText("<a href='mailto:user@foo.com?subject=Test&body=Just a test'>My link</a>");
umd->exec();
umd->ui->label->connect(umd->ui->label,
    SIGNAL(linkActivated(const QString&)), umd,
            SLOT(linkOpen(const QString&)));

(这被定义为适当的h文件中的公共插槽)

void UpgradeMessageDialog::linkOpen(const QString &link)
{
    QDesktopServices::openUrl(QUrl(link));
}

只是为了澄清:我在我的计算机中设置了默认邮件程序,当我在浏览器中键入mailto:a @ bc时程序打开正常。

首先,有两种方法可以处理QLabel中的链接激活。 你应该使用其中一个,但我发现你正试图同时使用它们。

这两种方式是:

  • 调用openExternalLinks(true) ,以便QLabel将使用QDesktopServices::openUrl()自动打开链接, 而不是发出linkActivated()信号。
  • 连接到linkActivated()信号,然后手动打开连接插槽中的链接(例如,通过调用QDesktopServices::openUrl() )。

你也使用exec()函数错误。 您应该在connect() exec()调用之后调用exec() ,因为exec()是阻塞的,因此在关闭对话框后实际发生信号连接。

所以你的代码应该是这样的:

umd->ui->label->setText("<a href='mailto:user@foo.com?subject=Test&body=Just a test'>My link</a>");
connect(umd->ui->label, SIGNAL(linkActivated(QString)), umd, SLOT(linkOpen(QString)));
umd->exec();

或者像这样:

umd->ui->label->setTextFormat(Qt::RichText);
umd->ui->label->setTextInteractionFlags(Qt::TextBrowserInteraction);
umd->ui->label->setOpenExternalLinks(true);
umd->ui->label->setText("<a href='mailto:user@foo.com?subject=Test&body=Just a test'>My link</a>");

还有一点建议:将标签初始化代码放入UpgradeMessageDialog构造函数中。

UpgradeMessageDialog::UpgradeMessageDialog(QDialog* parent) : QDialog(parent)
{
  ui->label->setTextFormat(Qt::RichText);
  ui->label->setTextInteractionFlags(Qt::TextBrowserInteraction);
  ui->label->openExternalLinks(true);
  ui->label->setText("<a href='mailto:user@foo.com?subject=Test&body=Just a test'>My link</a>");
}

然后你可以这样使用你的对话框:

QScopedPointer<UpgradeMessageDialog> umd = new UpgradeMessageDialog;
umd->exec();
#include <QUrl>
#include <QDesktopServices>

myLabel = new QLabel(this);
myLabel->setTextFormat(Qt::RichText);
myLabel->setText("Email:href='mailto:serge@essetee.be'>serge@essetee.be</a>");
myLabel->setOpenExternalLinks(true);

现在您只需单击该链接即可启动标准邮件客户端。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM