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 windowExample)
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. -
-
@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.
-
If I click [View Detail], anchorClicked event may occur.
-
The event send signal to Mainwindow.show_monitor_temp
self.dis_message.anchorClicked['QUrl'].connect(MainWindow.show_monitor_temp)
-
At the Mainwindow.show_monitor_temp(), I want to know how to retrieve URL information, sent by anchorClicked signal. <-- That is my question.
-
-
@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 showsvoid QTextBrowser::anchorClicked(const QUrl &link)
signal passes theQUrl
link to your slot? -
@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_MainWindowclass 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, QtWidgetsclass 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 theanchorClicked['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. - I don't think there are any other overloads of
-