QMessageBox - url encoding/decoding

MPeli

I created a QMessageBox with an html link:

QTMessageBox msgBox(Utility::UI::topLevelWidget());

msgBox.setText("<a href=\"http://www.example.cz/?url=www%25www\">Link</a>");

msgBox.exec();

If I left click the link a new web browser tab opens. The problem is that the url http://www.example.cz/?url=www**%2525**www is opened instead of http://www.example.cz/?url=www**%25**www

How do I prevent such behavior?

UPDATE: If I right click the link, choose "Copy link" and paste it into the browser, the link is ok.

Gombat

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related