简体   繁体   中英

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

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.

First, there are two ways to handle link activation in 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.
  • Connect to the linkActivated() signal and then manually open link in the connected slot (by calling QDesktopServices::openUrl() for example).

Also you use the exec() function wrong. 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.

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(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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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