简体   繁体   English

Qt 4.8.6中的qlabel mailto链接

[英]qlabel mailto link in Qt 4.8.6

I've followed instructions given on previous questions like this 我跟着像前面的问题作出批示,

so now if I put a link to a regular page it opens fine with the default browser. 所以现在,如果我将链接放到常规页面,它可以使用默认浏览器打开。 But if I want to open a mailto link from QT QLabel 4.8.6 the link does nothing. 但是,如果我想从QT QLabel 4.8.6打开mailto链接,该链接什么都不做。 What am I doing wrong? 我究竟做错了什么?

here is the code: 这是代码:

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&)));

(this is defined as a public slot in the appropriate h file) (这被定义为适当的h文件中的公共插槽)

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

Just to clarify: I have a default mail program set up in my computer, and when I type mailto:a@bc in the browser that program opens fine. 只是为了澄清:我在我的计算机中设置了默认邮件程序,当我在浏览器中键入mailto:a @ bc时程序打开正常。

First, there are two ways to handle link activation in QLabel. 首先,有两种方法可以处理QLabel中的链接激活。 You should use one of them, but I see you are trying to use both. 你应该使用其中一个,但我发现你正试图同时使用它们。

This two ways are: 这两种方式是:

  • Call openExternalLinks(true) , so that QLabel will automatically open links using QDesktopServices::openUrl() instead of emitting the linkActivated() signal. 调用openExternalLinks(true) ,以便QLabel将使用QDesktopServices::openUrl()自动打开链接, 而不是发出linkActivated()信号。
  • Connect to the linkActivated() signal and then manually open link in the connected slot (by calling QDesktopServices::openUrl() for example). 连接到linkActivated()信号,然后手动打开连接插槽中的链接(例如,通过调用QDesktopServices::openUrl() )。

Also you use the exec() function wrong. 你也使用exec()函数错误。 You should put the exec() call after the connect() call, because exec() is blocking so the signal connection will actually happened after the dialog is closed. 您应该在connect() exec()调用之后调用exec() ,因为exec()是阻塞的,因此在关闭对话框后实际发生信号连接。

So your code should be like this: 所以你的代码应该是这样的:

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

or like this: 或者像这样:

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>");

And a little advise: put the label initialization code into the UpgradeMessageDialog constructor. 还有一点建议:将标签初始化代码放入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>");
}

And then you can use your dialog this way: 然后你可以这样使用你的对话框:

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

Now you just have to click the link and the standard mail client will be launched. 现在您只需单击该链接即可启动标准邮件客户端。

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

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