That's because %
has the html encoding %25
. So %25
-> %2525
.
Why does Qt encode the links automatically?
In the QMessageBox, there is a QLabel. The label uses the Qt::TextFormat Qt::AutoText by default. Therefore, it detects in your text, that it is html encoded and generates the link.
The QLabel sends the signal linkActivated(const QString& link) or uses QDesktopServices::openUrl(), depending its the boolean openExternalLinks.
It seems, that the QMessageBox sets openExternalLinks
to true.
Since the link will be used as input for a QUrl, it will be parsed. That's the reason for the double encoding.
It is possible, to modify the behavior of QDesktopServices::openUrl()
by using its static method void QDesktopServices::setUrlHandler
. I implemented and tested it for the desired behavior:
MyUrlHandler urlHandler;
QDesktopServices::setUrlHandler( "http", &urlHandler, "handleUrl" );
QMessageBox msgBox;
msgBox.setText( "<a href=\"http://www.example.cz/?url=www%25www\">Link</a>" );
msgBox.show();
Using the class MyUrlHandler
:
class MyUrlHandler : public QObject
{
Q_OBJECT
public:
MyUrlHandler(QObject* parent=0):QObject(parent){}
public slots:
void handleUrl(const QUrl &url)
{
QDesktopServices::openUrl( QUrl::fromEncoded( url.toString().toAscii() ) );
}
};
The trick is simple, I set the link address directly to the QUrl
instance as already valid url.
But unfortunately, it modifies the behavior globally.