Hyperlink to web page in QLabel using PySide - python

I am having troubles displaying a functional hyperlink to a web page in a QLabel using PySide (Python version 3.2.5). Reading the most upvoted answer from this post c++ - Making QLabel behave like a hyperlink - Stack Overflow, I was under the impression that this would work:
from PySide.QtCore import Qt
from PySide.QtGui import QApplication, QLabel
app = QApplication([])
window = QLabel()
window.setText("<a href=\"http://stackoverflow.com/\" />Stack Overflow</a>")
window.setTextFormat(Qt.RichText)
window.setTextInteractionFlags(Qt.TextBrowserInteraction)
window.setOpenExternalLinks(True)
window.show()
app.exec_()
Unfortunately, the text does not show up as a link. Can anyone please point me in the right direction?

Your link are broken, Fix it should be fine;
from PyQt4.QtGui import QApplication, QLabel
myQApplication = QApplication([])
myQLabel = QLabel()
myQLabel.setText('''<a href='http://stackoverflow.com'>stackoverflow</a>''')
myQLabel.setOpenExternalLinks(True)
myQLabel.show()
myQApplication.exec_()

Related

Changing a label's text is not working properly on transparent widgets

This seems like a bug, but I wanted to confirm before reporting it.
I am trying to update a label's text on a transparent widget, but for some reason the previous text is partially visible (see screenshot below)
Did any one face this issue before? Is there any known workarounds?
System Specs: MacOS Monterey 12.0.1 and Python 3.10
screenshot
from PyQt6 import QtWidgets, QtCore
from PyQt6.QtCore import QTimer
from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QLabel
def update_label():
l1.setText("Bye!")
window.repaint()
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setAttribute(QtCore.Qt.WidgetAttribute.WA_TranslucentBackground)
window.setWindowFlag(QtCore.Qt.WindowType.FramelessWindowHint)
window.setFixedSize(800, 600)
font = QFont()
font.setPointSize(72)
l1 = QLabel(window)
l1.setText("Hello World")
l1.setFont(font)
l1.setStyleSheet("color:red")
window.show()
timer = QTimer()
timer.setInterval(10000)
timer.timeout.connect(update_label)
timer.start()
app.exec()
Not sure it could be considered an answer, but I found a workaround.
Simply close then show the window again:
def update_label():
window.close()
window.show()
l1.setText('Bye!')

QPrinter PDF output is tiny

I have a page set up in PYQT5 Designer, when opened normally on the screen it looks god so I tried to save to PDF.
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication, QFileDialog, QWidget, QMainWindow, QComboBox,
from PyQt5.QtPrintSupport import QPrinter
from export_pdf_ui import *
import sys
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args,**kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.export()
def export(self):
printer = QPrinter(QPrinter.HighResolution)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("test.pdf")
printer.setPaperSize(QPrinter.A4)
printer.setPageSize(QPrinter.A4)
printer.setFullPage(True)
self.ui.centralwidget.render(printer)
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
It works fine and outputs a PDF file, but the content is tiny and in the corner and you can't really read it even when fully zoomed in. How do I make it fill the whole page? or is it a design issue? it looks good on the screen
Try creating the printer with QPrinter::ScreenResolution rather than QPrinter.HighResolution.
I had a similar problem printing a QTextDocument to a physical printer which I created with QPrinter::HighResolution. If I called QTextDocument::setPageSize to eliminate page numbers, the output on paper would be about 50mm wide. I don't know if you are running into the same issue, but for me, the final piece of my puzzle was the resolution I created the QPrinter object with.

PyQt5 - Missing close button icon in QTabBar

I'm trying to set my tabs "Closable", however I can't see the close button icon on individual tabs when using PyQt5 and "Fusion" style:
When I set "Windows" style I can see:
I already tried to use styleSheet qtabbar button but it didn't work:
QTabBar::close-button {
image: url(close.png);
subcontrol-position: left;
}
This is my code:
#!/bin/python3
import sys
from PyQt5.QtWidgets import (QApplication, QVBoxLayout,
QTabBar, QFrame)
class App(QFrame):
def __init__(self):
super().__init__()
self.setWindowTitle("Web Browser")
self.setBaseSize(683, 384)
self.CreateApp()
def CreateApp(self):
self.layout = QVBoxLayout()
self.tab_Bar = QTabBar(movable=True, tabsClosable=True)
self.tab_Bar.tabCloseRequested.connect(self.CloseTab)
self.tab_Bar.addTab("Tab 1")
self.tab_Bar.addTab("Tab 2")
self.tab_Bar.setCurrentIndex(0)
self.layout.addWidget(self.tab_Bar)
self.setLayout(self.layout)
self.show()
def CloseTab(self, i):
self.tab_Bar.removeTab(i)
if __name__ == "__main__":
QApplication.setStyle('Fusion')
app = QApplication(sys.argv)
window = App()
sys.exit(app.exec_())
I'm using Python version 3.6.2 and PyQt5 version 5.10.
What could be the root cause of this problem? Maybe my system is lacking some icons in Fusion style?
Similar question can be found here. Though it does not have an official answer, there are some suggestions in comments. Do check them. Since it is working for "Windows" and not for "Fusion", can be a bug in QT5.

PyQt5 don't update TextEdit and Label Text [duplicate]

I'm newbie.
I want to click a pushButton to open a new window and take text from main window lineEdit and copy to new pop-up window lineEdit.
So far I an create new window but can't access lineEdit. No errors, app is not responding.
This is what I have:
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic
app = QApplication([]) #Main Window
ui = uic.loadUi(r"D:\UI_test\gui\main_gui_TT.ui")
appedit = QApplication([]) #Pop-up
uiedit = uic.loadUi(r"D:\UI_test\gui\input_TT.ui")
def edit1():
uiedit.show()
appedit.exec_()
uiedit.lineEdit_CC.setText('text') <-this line is a problem
ui.pushButton_1edit.pressed.connect(edit1)
ui.show()
app.exec_()
Please help what is wrong here?
You should only have a single QApplication even if you have many windows, considering the above the solution is:
from PyQt5.QtWidgets import QApplication
from PyQt5 import uic
app = QApplication([]) # Main Window
ui = uic.loadUi(r"D:\UI_test\gui\main_gui_TT.ui")
uiedit = uic.loadUi(r"D:\UI_test\gui\input_TT.ui")
def edit1():
uiedit.show()
uiedit.lineEdit_CC.setText("text")
ui.pushButton_1edit.pressed.connect(edit1)
ui.show()
app.exec_()

How to save an HTML page to PDF in PyQt5

I'm trying to get some simple html to be saved onto a pdf document using PyQt5.
The webpage renders properly, using the show() command I can get a window showing me the web content in question, however trying to print it to pdf only results in blank pdfs.
This is on python 3.6 and as of posting this question I have the current PyQt5 version. (Windows 10, 64 Bit)
There's a lot of older questions to this already on stackoverflow, but I found most of them use functions that no longer exist.
The problematic code follows:
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineSettings
from PyQt5 import QtWidgets
from PyQt5.QtPrintSupport import QPrinter
from PyQt5 import QtCore
[...]
app = QtWidgets.QApplication(sys.argv)
QWebEngineSettings.globalSettings().setAttribute(QWebEngineSettings.PluginsEnabled, True)
QWebEngineSettings.globalSettings().setAttribute(QWebEngineSettings.ScreenCaptureEnabled, True)
loader = QWebEngineView()
#loader.setAttribute(QtCore.Qt.WA_DontShowOnScreen, True)
loader.setAttribute(QtCore.Qt.WA_DeleteOnClose, True)
loader.setZoomFactor(1)
loader.setHtml(webpage)
printer = QPrinter()
printer.setPageSize(QPrinter.A4)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("test.pdf")
printer.setOrientation(QPrinter.Portrait)
printer.setFullPage(True)
def emit_pdf(finished):
loader.show()
loader.render(printer)
if self.open_on_complete:
import webbrowser
webbrowser.open("test.pdf")
#app.exit()
loader.loadFinished.connect(emit_pdf)
app.exec()
There is no need to use use QPrinter in Qt5, because QWebEnginePage has a dedicated printToPdf method:
import sys
from PyQt5 import QtCore, QtWidgets, QtWebEngineWidgets
app = QtWidgets.QApplication(sys.argv)
loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)
loader.page().pdfPrintingFinished.connect(
lambda *args: print('finished:', args))
loader.load(QtCore.QUrl('https://en.wikipedia.org/wiki/Main_Page'))
def emit_pdf(finished):
loader.show()
loader.page().printToPdf("test.pdf")
loader.loadFinished.connect(emit_pdf)
app.exec()
There's also a print method that will render to a printer, if you really want that.

Categories