qurl path and file name

QUrl seems a powerful tool that you can use to get every part of a url. But since a url can have complex syntax, I wonder if QUrl can handle all cases correctly. What are the file name, path, fragment, scheme, authority, query of a url and how to get them? Let’s see the following example:

QUrl url("https://myprogrammingnotes.com/dir1/dir2/index.html?var1=value1&var2=value2#target1");
QMessageBox::information(this,"filename",url.fileName()); //index.html
QMessageBox::information(this,"authority",url.authority()); //myprogrammingnotes.com
QMessageBox::information(this,"fragment",url.fragment()); //target1
QMessageBox::information(this,"path",url.path()); // /dir1/dir2/index.html
QMessageBox::information(this,"query",url.query());//var1=value1&var2=value2
QMessageBox::information(this,"scheme",url.scheme());//https
  •  If the url is https://myprogrammingnotes.com/dir1/dir2/?var1=value1&var2=value2#target1, the file name component would be an empty string, and the path component is /dir1/dir2/.
  • If the url is https://myprogrammingnotes.com/dir1/dir2?var1=value1&var2=value2#target1, the file name is dir2 and the path is /dir1/dir2. So QUrl does not really know if dir2 is a file or directory. It just takes the stuff after the last slash and before the query string as the file name. It takes all characters after the host part and before the query part as the path.
  • If the url is https://myprogrammingnotes.com/?var1=value1&var2=value2#target1, the file name would be a null string and the path would be /.
  • If the url is https://myprogrammingnotes.com?var1=value1&var2=value2#target1, both path and file name would be null strings.
Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Comments are closed, but trackbacks and pingbacks are open.