Qt5 - How to make qurl detect wether the given url is local or not and add "http://" if not?

1.2k views Asked by At

QUrl class can be used to open both local or online file. I used QLineEdit to take URL as QString and give it to QUrl. The program can access both local and online file. My point question is: is there any official way to automatically detect if the given url is local or online and add http:// automatically if the url is online?

For example, if user type www.google.com, it should be online and should be added http:// before it being processed. If user type /home/username/somepath it should be offline.

Of course a little if and else thing with string pattern check can be used for this purpose. My question is, if there's officially supported way to do something like this from Qt5.

1

There are 1 answers

1
Dávid Kaya On BEST ANSWER

You can use QUrl:fromUserInput(...) for that purpose.

QString first("qt-project.org");
QString second("ftp.qt-project.org");
QString third("hostname");
QString fourth("/home/user/test.html");

qDebug() << QUrl::fromUserInput(first);   // QUrl( "http://qt-project.org" )       
qDebug() << QUrl::fromUserInput(second);  // QUrl( "ftp://ftp.qt-project.org" )    
qDebug() << QUrl::fromUserInput(third);   // QUrl( "http://hostname" )             
qDebug() << QUrl::fromUserInput(fourth);  // QUrl( "file:///home/user/test.html" )