Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Python3.7 How to get URL as string
Forum Updated to NodeBB v4.3 + New Features

Python3.7 How to get URL as string

Scheduled Pinned Locked Moved Solved Qt for Python
14 Posts 4 Posters 1.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Junhi
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • J Junhi

      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.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        Junhi
        wrote on last edited by
        #3

        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.

        jsulmJ 1 Reply Last reply
        0
        • J Junhi

          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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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]).

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          0
          • jsulmJ jsulm

            @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]).

            J Offline
            J Offline
            Junhi
            wrote on last edited by
            #5

            @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.

            jsulmJ JonBJ 2 Replies Last reply
            0
            • J Junhi

              @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.

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • J Junhi

                @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.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #7

                @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?

                J 1 Reply Last reply
                0
                • JonBJ JonB

                  @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?

                  J Offline
                  J Offline
                  Junhi
                  wrote on last edited by
                  #8

                  @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.

                  JonBJ 1 Reply Last reply
                  0
                  • J Junhi

                    @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.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @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.

                    J 1 Reply Last reply
                    2
                    • JonBJ JonB

                      @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.

                      J Offline
                      J Offline
                      Junhi
                      wrote on last edited by
                      #10

                      @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)

                      JonBJ 1 Reply Last reply
                      0
                      • J Junhi

                        @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)

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @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.

                        1 Reply Last reply
                        1
                        • J Offline
                          J Offline
                          Junhi
                          wrote on last edited by
                          #12

                          @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.

                          JonBJ 1 Reply Last reply
                          0
                          • J Junhi

                            @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.

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            @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.

                            J 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @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.

                              J Offline
                              J Offline
                              Junhi
                              wrote on last edited by
                              #14

                              @JonB Yes, right

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

                              :-)

                              1 Reply Last reply
                              1

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved