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.
Posted in

Comments are closed, but trackbacks and pingbacks are open.