Reputation: 281
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:[email protected]?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:[email protected] in the browser that program opens fine.
Upvotes: 2
Views: 1451
Reputation: 63
#include <QUrl>
#include <QDesktopServices>
myLabel = new QLabel(this);
myLabel->setTextFormat(Qt::RichText);
myLabel->setText("Email:href='mailto:[email protected]'>[email protected]</a>");
myLabel->setOpenExternalLinks(true);
Now you just have to click the link and the standard mail client will be launched.
Upvotes: 1
Reputation: 321
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:
openExternalLinks(true)
, so that QLabel will automatically open links using QDesktopServices::openUrl()
instead of emitting the linkActivated()
signal.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:[email protected]?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:[email protected]?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:[email protected]?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();
Upvotes: 5