Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Python3.7 How to get URL as string



  • Hello I'm trying to get URL displayed on QTextBrowser as string.

    My Code is :

    GUI designed by QT Designer 5.x

    === monitor.py=======
    class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    self.dis_message = QtWidgets.QTextBrowser(self.splitter)
    self.dis_message.anchorClicked['QUrl'].connect(MainWindow.show_monitor_temp)

    ==== main.py =======
    class Form(QMainWindow, Ui_MainWindow):
    def init(self, parent=None):
    super().init()
    def show_monitor_temp(self):

    My wish :

    When click hyperlink messages displayed on QTextBrowser
    Anchorclick event send url address to another window

    Example)

    NAME : AAAA
    UNIT : BBBB
    BD : CCCC
    TEMP : 56.4
    View Detail1 ( It is hyper link - contains http://AAAA.BBBB.CCCC)

    NAME : DDDD
    UNIT : EEEE
    BD : FFFF
    TEMP : 55.8
    View Detail2( It is hyper link - contains http://DDDD.EEEE.FFFF)

    When user click 'View Detail1', show_monitor_temp(self) method in main.py, get AAAA.BBBB.CCCC as text.
    Also click 'View Detail2', show_monitor_temp(self) method in main.py, get DDDD.EEEE.FFFF as text.


  • Lifetime Qt Champion

    @Junhi What exactly is your question? Where do you need help?



  • Question

    Here is snapshot of my code:

    QtextBrowser.png

    View Detail is configured as
    '<a href =' + '"' + 'http://' + str(var[0]) + '.' + str(var[1]) + '.' + str(var[2]) + '"' + '>' + 'View Detail' + '</a>'

    I want to get URL address as text when I click View Detail link.


  • Lifetime Qt Champion

    @Junhi So, for the first one URL would be: http://LGE0010924.RRUS12-12-C7-S5B.RRUS12B8 ?
    This is actually quite simple in your case as everything is separated by ':'. So, you can split the string like str.split(":") and use the second part as value (str.split(":")[1]).



  • @jsulm Thank you jsulm, but that is not my want.

    1. If I click [View Detail], anchorClicked event may occur.

    2. The event send signal to Mainwindow.show_monitor_temp

      self.dis_message.anchorClicked['QUrl'].connect(MainWindow.show_monitor_temp)

    3. At the Mainwindow.show_monitor_temp(), I want to know how to retrieve URL information, sent by anchorClicked signal. <-- That is my question.


  • Lifetime Qt Champion

    @Junhi So, you want to download data from URL?
    See https://doc.qt.io/qt-5/qnetworkaccessmanager.html



  • @Junhi
    Not sure if you're asking about what @jsulm has pointed you to, or if you are asking how you know what the anchor clicked was, in which case https://doc.qt.io/qt-5/qtextbrowser.html#anchorClicked shows void QTextBrowser::anchorClicked(const QUrl &link) signal passes the QUrl link to your slot?



  • @JonB Yes, I think QUrl information passes to my slot(MainWindow.show_monitor_temp).
    self.dis_message.anchorClicked['QUrl'].connect(MainWindow.show_monitor_temp)
    But it seems not work properly.



  • @Junhi said in Python3.7 How to get URL as string:

    But it seems not work properly.

    What does that mean, how would you like us to help on that?? Do you think it would help us if you showed us your slot code?

    Meanwhile, you don't tell us whether you are using PySide2 or PyQt5. Which one is that self.dis_message.anchorClicked['QUrl'].connect() syntax for, with the ['QUrl']? I have not (so far) needed to use that syntax in either one.



  • @JonB Sorry, It is my first time of python programming. There may be many mistake ..
    I use PyQt5.

    This is part of my code.

    ===== main.py ======
    import sys
    import os
    import sqlite3
    import time
    from PyQt5.QtCore import pyqtSlot, QDate,QUrl
    from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QCalendarWidget, QDialog, QCheckBox
    from PyQt5 import QtCore, QtGui, QtWidgets
    from monitor import Ui_MainWindow

    class Form(QMainWindow, Ui_MainWindow):
    def init(self, parent=None):
    super().init()
    def show_monitor_temp(self):

        print("show_monitor_temp")
        print(QUrl().toString())
    

    ==== monitor.py (designed by QT Designer 5.13) ====
    from PyQt5 import QtCore, QtGui, QtWidgets

    class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
    self.dis_message = QtWidgets.QTextBrowser(self.splitter)
    self.dis_message.anchorClicked['QUrl'].connect(MainWindow.show_monitor_temp)



  • @Junhi
    [I think you have written which code is in which file the wrong way round.]

    Your connection should be:

    self.dis_message.anchorClicked.connect(MainWindow.show_monitor_temp)
    
    • I don't think there are any other overloads of anchorClicked(), or at least we are using the default one, so we don't need the anchorClicked['QUrl']. You can put it back in if you prefer/I am mistaken.

    Your slot should be:

    @pyqtslot(QUrl)   # you can probably omit this, for now
                      # or, you might need @pyqtslot('QUrl')
    def show_monitor_temp(self, url):
        print("show_monitor_temp")
        print(url.toString())  # maybe just: print(str(url))
    
    • See how the slot gets the QUrl as a parameter, because the signal passes it.

    Reference for PyQt5 signals/slots is https://www.riverbankcomputing.com/static/Docs/PyQt5/signals_slots.html.

    Above is quite untested. Give it a go and see how you fare. The important thing is to grasp how the slot receives the QUrl clicked on from the signal as a parameter, which is what you were missing. This is the only bit you have to change, other than that your code is fine.


  • Banned

    @Junhi okay this is only a snippet response and may not fully solve your issue next time you should supply an MRE (Minimal Reproducible Example) that can simply be copy/pasted and ran as is -- so that you can help us do you the favor you are requesting by making it easier for us to help you with your issue. Hope this helps but cannot say it will

    from PyQt5.QtCore    import pyqtSlot, QDate, QUrl
    from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QCalendarWidget, QDialog, QCheckBox
    from PyQt5.QtWidgets import QTextBrowser
    
    # All non Qt imports should take place after the Qt imports as this prevents
    # issues with improper assocation which can occur when using Qt
    from monitor import Ui_MainWindow
    
    import sys
    import os
    import sqlite3
    import time
    
    class Form(QMainWindow):
        def init(self):
          # You should not use super( ) in Python as it introduces 4 known issues that
          # must be handled properly. Further there were still actual bugs within the
          # usage of super( ) when used in Python. Yes while super( ) works fine within
          # C++ it does not work as seamlessly within Python due to the major 
          # differences between these 2 languages. Next the reason it was created was 
          # to handle a rather rare issue and unless you are doing some complicated 
          # inheritance you will most likely never run into this extremely rare issue
          # However the 4 major issues that get included by using super( ) you are much 
          # more likely to occur than that rare issue its meant for to solve. Of course
          # using the basic explicit method, as follows, does not cause these issues and 
          # is as just as simple as using `super( )` further you do not actually gain 
          # anything useful by using `super( )` in Python that could not be done in a 
          # much safer manner.
            QMainWindow.init(self)
    
          # I did not see where you were passing the handle of MainWindow to your
          # UI_MainWindow class -- if that is an actual UI created by the Designer then
          # this might not work because the Designer does not make viable python-qt code
          # as it was only designed to make XML code that was easily portable to different
          # languages -- which means if you are not using numerous languages you lose the
          # only benefit of using a UI and are left with all its negative aspects which
          # are quite a few and its just as easy, quick and cleaner to just use python-qt
          # as it was meant to be used to create a GUI
            self.UI_Win = UI_MainWindow(self)
    
        @pyqtSlot(QUrl)
        def show_monitor_temp(self, URL):
            print("show_monitor_temp")
            print(URL.toString())
    
    
    #==== monitor.py (designed by QT Designer 5.13) ====
    # You should only import the Objects you are using as this helps outline what is
    # being used within that code module
    from PyQt5.QtCore    import * 
    from PyQt5.QtGui     import *
    from PyQt5.QtWidgets import *
    
    # This should be a QObject if you want to pass it a handle to a QtWidget
    class UI_MainWindow(QObject):
        def init(self):
            QObject.init(self)
            pass
    
        def setupUi(self, MainWindow):
            self.dis_message = QTextBrowser(self.splitter)
            self.dis_message.anchorClicked.connect(MainWindow.show_monitor_temp)
    


  • @Denni-0 @JonB

    I solved the problem by changing

    self.dis_message.anchorClicked['QUrl'].connect(MainWindow.show_monitor_temp) ->
    self.dis_message.anchorClicked.connect(MainWindow.show_monitor_temp)

    @pyqtslot() -> @pyqtslot('QUrl')

    Thank you for your kind help.



  • @Junhi
    And by adding url as formal parameter to your def show_monitor_temp(self, url):! Now you know how slots receive any arguments from signals.



  • @JonB Yes, right

    adding url as formal parameter to def show_monitor_temp(self, url):

    :-)


Log in to reply