pythontextpyqt5qurl

PyQt5: How to print a QUrl without the module name


I am trying to grab a url from the web, but no matter what I do, it also prints the entire module for the QUrl:

import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEngineView

app = QApplication(sys.argv)
url = 'http://stackoverflow.com'
wv = QWebEngineView()

wv.load(QUrl(url))
print str(QUrl(url))
wv.show()
app.exec_()

which gives me :

PyQt5.QtCore.QUrl(u'http://stackoverflow.com')

I am only interested grabbing the unicode string, without the module name:

u'http://stackoverflow.com'

I know I can just print the url, but this is just for reproducing the problem in a bigger app.


Solution

  • The QUrl class has a dedicated toString method for this:

    >>> u = QtCore.QUrl(u'http://stackoverflow.com')
    >>> u.toString()
    u'http://stackoverflow.com'
    

    You can also pass in an argument with Formatting Options, which will modify how the url is displayed (e.g. by removing the query or stripping trailing slashes).