I want to catch a click on a page and add at the anchor link some text.
For example, If the user click on an anchor with href mysite.com/about, I want modify the link in: mysite.com/about?myvar=value
I used QWebEngineUrlRequestInterceptor but the click on the link is not catched.
What I am doing wrong?
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
from PyQt5.QtPrintSupport import *
import os
import sys
class MyPage(QWebEnginePage):
def __init__(self, parent=None):
super(MyPage, self).__init__(parent)
def triggerAction(self, action, checked=False):
if action == QWebEnginePage.OpenLinkInNewWindow:
self.createWindow(QWebEnginePage.WebBrowserWindow)
return super(MyPage, self).triggerAction(action, checked)
def acceptNavigationRequest(self, url, navtype, mainframe):
if navtype == QWebEnginePage.NavigationTypeLinkClicked:
# go somewhere else
self.setUrl(QUrl('https://wikipedia.org'))
# abort the current navigation
return False
return super(MyPage, self).acceptNavigationRequest(url, navtype, mainframe)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.myPage = MyPage(self) #fab
#self.setPage(self.myPage) #fab
self.browser = QWebEngineView()
self.browser.setUrl(QUrl(WEBSITE_HOME))
self.browser.loadFinished.connect(self.update_title)
self.setCentralWidget(self.browser)
navtb = QToolBar("Navigation")
navtb.setIconSize(QSize(16, 16))
# self.addToolBar(navtb)
back_btn = QAction(QIcon(os.path.join('images', 'arrow-back.png')), "Back", self)
back_btn.setStatusTip("Back to previous page")
back_btn.triggered.connect(self.browser.back)
navtb.addAction(back_btn)
next_btn = QAction(QIcon(os.path.join('images', 'arrow-forward.png')), "Forward", self)
next_btn.setStatusTip("Forward to next page")
next_btn.triggered.connect(self.browser.forward)
navtb.addAction(next_btn)
reload_btn = QAction(QIcon(os.path.join('images', 'refresh.png')), "Reload", self)
reload_btn.setStatusTip("Reload page")
reload_btn.triggered.connect(self.browser.reload)
navtb.addAction(reload_btn)
home_btn = QAction(QIcon(os.path.join('images', 'home.png')), "Home", self)
home_btn.setStatusTip("Go home")
home_btn.triggered.connect(self.navigate_home)
navtb.addAction(home_btn)
stop_btn = QAction(QIcon(os.path.join('images', 'cross-circle.png')), "Stop", self)
stop_btn.setStatusTip("Stop loading current page")
stop_btn.triggered.connect(self.browser.stop)
navtb.addAction(stop_btn)
navtb.setContextMenuPolicy(Qt.PreventContextMenu)
self.show()
self.setWindowIcon(QIcon(os.path.join('images', 'ma-icon-64.png')))
def update_title(self):
title = self.browser.page().title()
self.setWindowTitle("%s - Title" % title)
def navigate_home(self):
self.browser.setUrl(QUrl(WEBSITE_LOGIN))
def createWindow(self, windowType):
if windowType == QWebEnginePage.WebBrowserTab:
self.webView = MyWindow()
self.webView.setAttribute(Qt.WA_DeleteOnClose, True)
self.webView.show()
return self.webView
return super(MyWindow, self).createWindow(windowType)
WEBSITE_HOME = "http://google.com"
WEBSITE_LOGIN = "http://google.com"
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
If you only want to modify/replace a url before a link is navigated to, perhaps the simplest solution is to reimplement acceptNavigationRequest:
class MyPage(QWebEnginePage):
def acceptNavigationRequest(self, url, navtype, mainframe):
if navtype == QWebEnginePage.NavigationTypeLinkClicked:
# go somewhere else
self.setUrl(QUrl('https://wikipedia.org'))
# abort the current navigation
return False
return super(MyPage, self).acceptNavigationRequest(url, navtype, mainframe)
Related
I'm building a simple browser with python, PyQt5 QWebEnjineView.
I want to make the reload button to be hidden when the page is loading and "stop loading" button visible,
When loading is finished, then reload button will be visible again and "stop loading" button will be hidden.
My QWebEnjineView is in a method called add_new_tab and I've defined QTabWidget as self.tabs in the init method.
import os
import sys
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import QWebEngineView
class mainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(mainWindow, self).__init__()
# create tabs
self.tabs = QTabWidget()
self.tabs.tabBarDoubleClicked.connect(self.tab_open_doubleclick)
self.tabs.setTabsClosable(True)
self.tabs.tabCloseRequested.connect(self.close_current_tab)
self.tabs.currentWidget().loadProgress.connect(self.loadProgressHandler)
self.tabs.currentWidget().loadFinished.connect(self.loadFinishedHandler)
self.setCentralWidget(self.tabs)
# self.setCentralWidget(self.browser)
self.showMaximized()
# nav bar
self.navbar = QToolBar()
self.navbar.setMovable(False)
self.addToolBar(self.navbar)
# Refresh button
self.reload_butn = QPushButton(self, text="Reload")
self.reload_butn.clicked.connect(self.reload_tab)
# Set reload button visible
self.reload_butn.setHidden(False)
# Stop button
self.stop_btn = QPushButton(self, text="Stop")
self.stop_btn.clicked.connect(self.stop_loading_tab)
self.stop_btn.setHidden(True)
# Set stop_butn hidden initially
self.stop_btn.setHidden(True)
# Add Refresh and Stop button
self.navbar.addWidget(self.stop_btn)
self.navbar.addWidget(self.reload_butn)
# Add Address bar
self.url_bar = QLineEdit()
self.url_bar.returnPressed.connect(self.navigate_to_url)
self.navbar.addWidget(self.url_bar)
# on startup
self.add_new_tab(QUrl("https://www.google.com/"), "Homepage")
self.show()
#QtCore.pyqtSlot(int)
def loadProgressHandler(self, prog):
self.stop_btn.setHidden(False) # When any page is loading, then stop_butn will visible
self.reload_butn.setHidden(True) # When any page is loading, then reload_butn will hidde
#QtCore.pyqtSlot()
def loadFinishedHandler(self):
self.reload_butn.setHidden(False) # When loading is finished, then reload_butn will be visible again for the user
self.stop_btn.setHidden(True) # When load finished, stop button will be hidden
# reload tab
def reload_tab(self):
self.tabs.currentWidget().reload()
def stop_loading_tab(self):
self.tabs.currentWidget().stop()
def close_current_tab(self, i):
if self.tabs.count() < 2 :
return
self.tabs.removeTab(i)
# stop load current tab
def stop_loading_tab(self):
self.tabs.currentWidget().stop()
# doubleclick on empty space for new tab
def tab_open_doubleclick(self, i):
if i == -1: # No tab under the click
self.add_new_tab(QUrl("http://www.google.com/"), label="New tab")
# function to add new tab
def add_new_tab(self, qurl=None, label="Blank"):
if qurl is None:
qurl = QUrl('https://www.google.com/')
browser = QWebEngineView()
browser.setUrl(qurl)
i = self.tabs.addTab(browser, label)
self.tabs.setCurrentIndex(i)
def navigate_to_url(self):
self.tabs.currentWidget().setUrl(QUrl(self.url_bar.text()))
app = QApplication(sys.argv)
app.setApplicationName("browser")
window = mainWindow()
app.exec_()
I have some button for reload, back, home etc. where I called self.tabs.currentWidget().reload() for example in the reload method,
But when I'm adding self.tabs.currentWidget().loadProgress.connect(self.loadProgressHandler)
for the operation, then It's giving me a error
self.tabs.currentWidget().loadProgress.connect(self.loadProgressHandler)
AttributeError: 'NoneType' object has no attribute 'loadProgress'
Can anyone tell me why is it showing that the self.tabs.currentWidget() is NoneType?
Is there a way to fix it? Ask me if you need more details
Thank you!
The cause of the error is:
self.tabs.currentWidget().loadProgress.connect(self.loadProgressHandler)
self.tabs.currentWidget().loadFinished.connect(self.loadFinishedHandler)
Where the OP is assuming that the connection will occur with all the pages, and that is incorrect since it will only occur with the current widget which in that case is None causing the error.
In this case the solution is to connect each QWebEngineView created and check in the slots if the sender() matches the currentWidget().
remove
self.tabs.currentWidget().loadProgress.connect(self.loadProgressHandler)
self.tabs.currentWidget().loadFinished.connect(self.loadFinishedHandler)
add connection:
def add_new_tab(self, qurl=None, label="Blank"):
if qurl is None:
qurl = QUrl('https://www.google.com/')
browser = QWebEngineView()
browser.loadProgress.connect(self.loadProgressHandler)
browser.loadFinished.connect(self.loadFinishedHandler)
i = self.tabs.addTab(browser, label)
self.tabs.setCurrentIndex(i)
browser.load(qurl)
Validate:
#QtCore.pyqtSlot(int)
def loadProgressHandler(self, prog):
if self.tabs.currentWidget() is not self.sender():
return
self.stop_btn.show()
self.reload_butn.hide()
#QtCore.pyqtSlot()
def loadFinishedHandler(self):
if self.tabs.currentWidget() is not self.sender():
return
self.reload_butn.show()
self.stop_btn.hide()
Update:
There are the following errors:
The visibility of the widgets added to the QToolBar are managed using the associated QActions.
Instead of managing 2 slots associated with the progress and completion of loading, only one of them should be used since, for example, the associated slot is also called when it is loaded at 100% so it could be hidden since it can be invoked together with finished.
It is better to verify that the variables that can be None to avoid exceptions.
Considering the above, the solution is:
import sys
from PyQt5.QtCore import pyqtSlot, QUrl
from PyQt5.QtWidgets import (
QApplication,
QLineEdit,
QMainWindow,
QPushButton,
QTabWidget,
QToolBar,
)
from PyQt5.QtWebEngineWidgets import QWebEngineView
class mainWindow(QMainWindow):
def __init__(self, parent=None):
super(mainWindow, self).__init__(parent)
self.tabs = QTabWidget(tabsClosable=True)
self.tabs.tabBarDoubleClicked.connect(self.tab_open_doubleclick)
self.tabs.tabCloseRequested.connect(self.close_current_tab)
self.navbar = QToolBar(movable=True)
self.addToolBar(self.navbar)
self.reload_butn = QPushButton(self, text="Reload")
self.reload_butn.clicked.connect(self.reload_tab)
self.stop_btn = QPushButton(self, text="Stop")
self.stop_btn.clicked.connect(self.stop_loading_tab)
self.url_bar = QLineEdit()
self.url_bar.returnPressed.connect(self.navigate_to_url)
self.stop_action = self.navbar.addWidget(self.stop_btn)
self.reload_action = self.navbar.addWidget(self.reload_butn)
self.navbar.addWidget(self.url_bar)
self.stop_action.setVisible(False)
self.add_new_tab(QUrl("https://www.google.com/"), "Homepage")
self.setCentralWidget(self.tabs)
self.showMaximized()
#pyqtSlot(int)
def loadProgressHandler(self, prog):
if self.tabs.currentWidget() is not self.sender():
return
loading = prog < 100
self.stop_action.setVisible(loading)
self.reload_action.setVisible(not loading)
def reload_tab(self):
self.tabs.currentWidget().reload()
def stop_loading_tab(self):
self.tabs.currentWidget().stop()
def close_current_tab(self, i):
if self.tabs.count() < 2:
return
self.tabs.removeTab(i)
def stop_loading_tab(self):
if self.tabs.currentWidget() is None:
return
self.tabs.currentWidget().stop()
def tab_open_doubleclick(self, i):
if i == -1:
self.add_new_tab(QUrl("http://www.google.com/"), label="New tab")
def add_new_tab(self, qurl=None, label="Blank"):
if qurl is None:
qurl = QUrl("https://www.google.com/")
browser = QWebEngineView()
browser.loadProgress.connect(self.loadProgressHandler)
i = self.tabs.addTab(browser, label)
self.tabs.setCurrentIndex(i)
browser.load(qurl)
def navigate_to_url(self):
if self.tabs.currentWidget() is None:
return
self.tabs.currentWidget().load(QUrl.fromString(self.url_bar.text()))
def main():
app = QApplication(sys.argv)
app.setApplicationName("browser")
window = mainWindow()
app.exec_()
if __name__ == "__main__":
main()
I'm new to PyQt and I'm trying to create a videoplayer that screenshots the current frame it retrieves. I implemented this method for grabbing the videoframes in order to take screenshots and added it to a videoplayer GUI. Here's how the GUI essentially looks like:
I created a trigger for the screenshot action in the VideoWindow class with the following code:
screenshotAction = QAction(QIcon('screenshot.png'), '&Screenshot', self)
screenshotAction.setShortcut('Ctrl+S')
screenshotAction.setStatusTip('Screenshot scenes')
screenshotAction.triggered.connect(self.screenshotCall)
I then took the implementation of the VideoFrameGrabber class and called it in the screenshot function:
def screenshotCall(self):
#Call video frame grabber
self.grabber = VideoFrameGrabber(self.videoWidget, self)
self.mediaPlayer.setVideoOutput(self.grabber)
self.grabber.frameAvailable.connect(self.process_frame)
self.mediaPlayer.setVideoOutput(self.videoWidget)
Complete code:
# PyQt5 Video player
#!/usr/bin/env python
from PyQt5.QtCore import QDir, Qt, QUrl, pyqtSignal, QPoint, QRect, QObject
from PyQt5.QtMultimedia import QMediaContent, QMediaPlayer, QVideoFrame, QAbstractVideoSurface, QAbstractVideoBuffer, QVideoSurfaceFormat
from PyQt5.QtMultimediaWidgets import QVideoWidget
from PyQt5.QtWidgets import (QApplication, QFileDialog, QHBoxLayout, QLabel,
QPushButton, QSizePolicy, QSlider, QStyle, QVBoxLayout, QWidget)
from PyQt5.QtWidgets import QMainWindow,QWidget, QPushButton, QAction
from PyQt5.QtGui import QIcon, QPainter, QImage
import sys
import os
import os.path as osp
class VideoFrameGrabber(QAbstractVideoSurface):
frameAvailable = pyqtSignal(QImage)
def __init__(self, widget: QWidget, parent: QObject):
super().__init__(parent)
self.widget = widget
def supportedPixelFormats(self, handleType):
return [QVideoFrame.Format_ARGB32, QVideoFrame.Format_ARGB32_Premultiplied,
QVideoFrame.Format_RGB32, QVideoFrame.Format_RGB24, QVideoFrame.Format_RGB565,
QVideoFrame.Format_RGB555, QVideoFrame.Format_ARGB8565_Premultiplied,
QVideoFrame.Format_BGRA32, QVideoFrame.Format_BGRA32_Premultiplied, QVideoFrame.Format_BGR32,
QVideoFrame.Format_BGR24, QVideoFrame.Format_BGR565, QVideoFrame.Format_BGR555,
QVideoFrame.Format_BGRA5658_Premultiplied, QVideoFrame.Format_AYUV444,
QVideoFrame.Format_AYUV444_Premultiplied, QVideoFrame.Format_YUV444,
QVideoFrame.Format_YUV420P, QVideoFrame.Format_YV12, QVideoFrame.Format_UYVY,
QVideoFrame.Format_YUYV, QVideoFrame.Format_NV12, QVideoFrame.Format_NV21,
QVideoFrame.Format_IMC1, QVideoFrame.Format_IMC2, QVideoFrame.Format_IMC3,
QVideoFrame.Format_IMC4, QVideoFrame.Format_Y8, QVideoFrame.Format_Y16,
QVideoFrame.Format_Jpeg, QVideoFrame.Format_CameraRaw, QVideoFrame.Format_AdobeDng]
def isFormatSupported(self, format):
imageFormat = QVideoFrame.imageFormatFromPixelFormat(format.pixelFormat())
size = format.frameSize()
return imageFormat != QImage.Format_Invalid and not size.isEmpty() and \
format.handleType() == QAbstractVideoBuffer.NoHandle
def start(self, format: QVideoSurfaceFormat):
imageFormat = QVideoFrame.imageFormatFromPixelFormat(format.pixelFormat())
size = format.frameSize()
if imageFormat != QImage.Format_Invalid and not size.isEmpty():
self.imageFormat = imageFormat
self.imageSize = size
self.sourceRect = format.viewport()
super().start(format)
self.widget.updateGeometry()
self.updateVideoRect()
return True
else:
return False
def stop(self):
self.currentFrame = QVideoFrame()
self.targetRect = QRect()
super().stop()
self.widget.update()
def present(self, frame):
if frame.isValid():
cloneFrame = QVideoFrame(frame)
cloneFrame.map(QAbstractVideoBuffer.ReadOnly)
image = QImage(cloneFrame.bits(), cloneFrame.width(), cloneFrame.height(),
QVideoFrame.imageFormatFromPixelFormat(cloneFrame.pixelFormat()))
self.frameAvailable.emit(image) # this is very important
cloneFrame.unmap()
if self.surfaceFormat().pixelFormat() != frame.pixelFormat() or \
self.surfaceFormat().frameSize() != frame.size():
self.setError(QAbstractVideoSurface.IncorrectFormatError)
self.stop()
return False
else:
self.currentFrame = frame
self.widget.repaint(self.targetRect)
return True
def updateVideoRect(self):
size = self.surfaceFormat().sizeHint()
size.scale(self.widget.size().boundedTo(size), Qt.KeepAspectRatio)
self.targetRect = QRect(QPoint(0, 0), size)
self.targetRect.moveCenter(self.widget.rect().center())
def paint(self, painter):
if self.currentFrame.map(QAbstractVideoBuffer.ReadOnly):
oldTransform = self.painter.transform()
if self.surfaceFormat().scanLineDirection() == QVideoSurfaceFormat.BottomToTop:
self.painter.scale(1, -1)
self.painter.translate(0, -self.widget.height())
image = QImage(self.currentFrame.bits(), self.currentFrame.width(), self.currentFrame.height(),
self.currentFrame.bytesPerLine(), self.imageFormat)
self.painter.drawImage(self.targetRect, image, self.sourceRect)
self.painter.setTransform(oldTransform)
self.currentFrame.unmap()
class VideoWindow(QMainWindow):
def __init__(self, parent=None):
super(VideoWindow, self).__init__(parent)
self.setWindowTitle("PyQt Video Player Widget")
self.counter = 0
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
self.videoWidget = QVideoWidget()
self.videoFrame = QVideoFrame()
self.playButton = QPushButton()
self.playButton.setEnabled(False)
self.playButton.setIcon(self.style().standardIcon(QStyle.SP_MediaPlay))
self.playButton.clicked.connect(self.play)
self.positionSlider = QSlider(Qt.Horizontal)
self.positionSlider.setRange(0, 0)
self.positionSlider.sliderMoved.connect(self.setPosition)
self.errorLabel = QLabel()
self.errorLabel.setSizePolicy(QSizePolicy.Preferred,
QSizePolicy.Maximum)
# Create new action
openAction = QAction(QIcon('open.png'), '&Open', self)
openAction.setShortcut('Ctrl+O')
openAction.setStatusTip('Open video')
openAction.triggered.connect(self.openFile)
# Create exit action
exitAction = QAction(QIcon('quit.png'), '&Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(self.exitCall)
screenshotAction = QAction(QIcon('screenshot.png'), '&Screenshot', self)
screenshotAction.setShortcut('Ctrl+S')
screenshotAction.setStatusTip('Screenshot scenes')
screenshotAction.triggered.connect(self.screenshotCall)
# Create menu bar and add action
menuBar = self.menuBar()
fileMenu = menuBar.addMenu('&File')
#fileMenu.addAction(newAction)
fileMenu.addAction(openAction)
fileMenu.addAction(screenshotAction)
fileMenu.addAction(exitAction)
# Create a widget for window contents
wid = QWidget(self)
self.setCentralWidget(wid)
# Create layouts to place inside widget
controlLayout = QHBoxLayout()
controlLayout.setContentsMargins(0, 0, 0, 0)
controlLayout.addWidget(self.playButton)
controlLayout.addWidget(self.positionSlider)
layout = QVBoxLayout()
layout.addWidget(self.videoWidget)
layout.addLayout(controlLayout)
layout.addWidget(self.errorLabel)
# Set widget to contain window contents
wid.setLayout(layout)
self.mediaPlayer.setVideoOutput(self.videoWidget)
self.mediaPlayer.stateChanged.connect(self.mediaStateChanged)
self.mediaPlayer.positionChanged.connect(self.positionChanged)
self.mediaPlayer.durationChanged.connect(self.durationChanged)
self.mediaPlayer.error.connect(self.handleError)
def openFile(self):
fileName, _ = QFileDialog.getOpenFileName(self, "Open Movie",
QDir.homePath())
self.path = osp.dirname(str(fileName))
if fileName != '':
self.mediaPlayer.setMedia(
QMediaContent(QUrl.fromLocalFile(fileName)))
self.playButton.setEnabled(True)
def exitCall(self):
sys.exit(app.exec_())
def screenshotCall(self):
#Call video frame grabber
self.grabber = VideoFrameGrabber(self.videoWidget, self)
self.mediaPlayer.setVideoOutput(self.grabber)
self.mediaPlayer.pause()
self.grabber.frameAvailable.connect(self.process_frame)
self.errorLabel.setText("Taking a screenshot of image "+str(self.counter)+" ....")
self.mediaPlayer.play()
self.mediaPlayer.setVideoOutput(self.videoWidget)
def play(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
else:
self.mediaPlayer.play()
def mediaStateChanged(self, state):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.playButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPause))
else:
self.playButton.setIcon(
self.style().standardIcon(QStyle.SP_MediaPlay))
def positionChanged(self, position):
self.positionSlider.setValue(position)
def durationChanged(self, duration):
self.positionSlider.setRange(0, duration)
def setPosition(self, position):
self.mediaPlayer.setPosition(position)
def handleError(self):
self.playButton.setEnabled(False)
self.errorLabel.setText("Error: " + self.mediaPlayer.errorString())
def process_frame(self, image):
# Save image here
filename = "screenshot" + str(self.counter).zfill(6)
self.path = 'C:/Desktop/temp'
image.save(self.path+'/{}.png'.format(str(filename)))
self.counter = self.counter+1
if __name__ == '__main__':
app = QApplication(sys.argv)
player = VideoWindow()
player.resize(720, 480)
player.show()
sys.exit(app.exec_())
However, whenever I try taking a screenshot, the video display turns black. Based from the audio, the video is still playing, but the current display remains black all throughout the duration of the video from the moment the screenshot is taken. This happens for both when the player is paused and while it is still playing. I think it's because of the line self.mediaPlayer.setVideoOutput(self.grabber), since it overwrites the current display on the screen with the grabber.
I've tried calling the videoWidget back and set it as the video output after the frame was grabbed, but it seems to overwrite the grabber and does not produce any screenshots at all.
Is there a way for me to simultaneously take screenshots and still display the video on the GUI?
solved this by calling the pause function every time the video plays, and then playing the part of the video where it was paused when the play button is pressed.
here's the code:
def screenshotCall(self):
#Call video frame grabber
self.grabber = VideoFrameGrabber(self.videoWidget, self)
self.mediaPlayer.setVideoOutput(self.grabber)
self.grabber.frameAvailable.connect(self.process_frame)
self.errorLabel.setText("Taking a screenshot of image "+str(self.counter)+" ....")
self.mediaPlayer.pause()
def play(self):
if self.mediaPlayer.state() == QMediaPlayer.PlayingState:
self.mediaPlayer.pause()
else:
self.mediaPlayer.play()
self.mediaPlayer.setVideoOutput(self.videoWidget)
I'm developing a webview browser in Python and PyQt5 and I want to know How to highlight particular text in Python and PyQt5 web browser. I want to highlight particular text like web find text in other browsers.
self.browser = QWebEngineView()
self.browser.HighlightAllOccurrences('hello world')
You have to use the findText method of QWebEngineView (or QWebEnginePage), in the following example there is a search bar when you press Ctrl + F that allows to make the search easier for the user:
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
class SearchPanel(QtWidgets.QWidget):
searched = QtCore.pyqtSignal(str, QtWebEngineWidgets.QWebEnginePage.FindFlag)
closed = QtCore.pyqtSignal()
def __init__(self, parent=None):
super(SearchPanel, self).__init__(parent)
lay = QtWidgets.QHBoxLayout(self)
done_button = QtWidgets.QPushButton('&Done')
self.case_button = QtWidgets.QPushButton('Match &Case', checkable=True)
next_button = QtWidgets.QPushButton('&Next')
prev_button = QtWidgets.QPushButton('&Previous')
self.search_le = QtWidgets.QLineEdit()
self.setFocusProxy(self.search_le)
done_button.clicked.connect(self.closed)
next_button.clicked.connect(self.update_searching)
prev_button.clicked.connect(self.on_preview_find)
self.case_button.clicked.connect(self.update_searching)
for btn in (self.case_button, self.search_le, next_button, prev_button, done_button, done_button):
lay.addWidget(btn)
if isinstance(btn, QtWidgets.QPushButton): btn.clicked.connect(self.setFocus)
self.search_le.textChanged.connect(self.update_searching)
self.search_le.returnPressed.connect(self.update_searching)
self.closed.connect(self.search_le.clear)
QtWidgets.QShortcut(QtGui.QKeySequence.FindNext, self, activated=next_button.animateClick)
QtWidgets.QShortcut(QtGui.QKeySequence.FindPrevious, self, activated=prev_button.animateClick)
QtWidgets.QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Escape), self.search_le, activated=self.closed)
#QtCore.pyqtSlot()
def on_preview_find(self):
self.update_searching(QtWebEngineWidgets.QWebEnginePage.FindBackward)
#QtCore.pyqtSlot()
def update_searching(self, direction=QtWebEngineWidgets.QWebEnginePage.FindFlag()):
flag = direction
if self.case_button.isChecked():
flag |= QtWebEngineWidgets.QWebEnginePage.FindCaseSensitively
self.searched.emit(self.search_le.text(), flag)
def showEvent(self, event):
super(SearchPanel, self).showEvent(event)
self.setFocus(True)
class Browser(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(Browser, self).__init__(parent)
self._view = QtWebEngineWidgets.QWebEngineView()
self.setCentralWidget(self._view)
self._view.load(QtCore.QUrl('https://doc.qt.io/qt-5/qwebengineview.html'))
self._search_panel = SearchPanel()
self.search_toolbar = QtWidgets.QToolBar()
self.search_toolbar.addWidget(self._search_panel)
self.addToolBar(QtCore.Qt.BottomToolBarArea, self.search_toolbar)
self.search_toolbar.hide()
self._search_panel.searched.connect(self.on_searched)
self._search_panel.closed.connect(self.search_toolbar.hide)
self.create_menus()
#QtCore.pyqtSlot(str, QtWebEngineWidgets.QWebEnginePage.FindFlag)
def on_searched(self, text, flag):
def callback(found):
if text and not found:
self.statusBar().show()
self.statusBar().showMessage('Not found')
else:
self.statusBar().hide()
self._view.findText(text, flag, callback)
def create_menus(self):
menubar = self.menuBar()
file_menu = menubar.addMenu('&File')
file_menu.addAction('&Find...', self.search_toolbar.show, shortcut=QtGui.QKeySequence.Find)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication.instance()
if app is None:
app = QtWidgets.QApplication(sys.argv)
w = Browser()
w.show()
sys.exit(app.exec_())
I've created a wizard with Pyside.
On one page, I create a new thread, which starts an installer.
When the installer is ready, the Wizard should go forward automatically, without clicking the next button.
I've read the pyside documentation, and now my understanding is, that QWizard has a next function. But how can I use this function?
My test is working fine:
from PySide.QtGui import *
from PySide.QtCore import *
...
...
class Install(QWizardPage):
def __init__(self, parent=None):
super(Install, self).__init__(parent)
def initializePage(self):
self.setTitle("Install")
label = QLabel("Install")
label.setWordWrap(True)
layout = QVBoxLayout()
self.progressBar = QProgressBar(self)
self.progressBar.setRange(0,1)
self.progressBar.setRange(0,0)
layout.addWidget(self.progressBar)
layout.addWidget(label)
self.setLayout(layout)
self.myTask = TaskThread()
self.myTask.start()
self.myTask.taskFinished.connect(self.Finished)
def Finished(self):
print("finish")
def isComplete(self):
return False
class TaskThread(QThread):
taskFinished = Signal()
def run(self):
a = 0
while a != 10000:
print("test")
a += 1
self.taskFinished.emit()
And when I try to use the next function I try:
self.CallNext = QWizard().next
self.myTask.taskFinished.connect(self.CallNext)
And also:
self.myTask.taskFinished.connect(QWizard().next)
But this is not working
This connection should be done in the context where the QWizard and QWizardPage exist, but before that we must move the creation of the QThread to the constructor, for example in the following example I do in the main:
class Install(QWizardPage):
def __init__(self, parent=None):
super(Install, self).__init__(parent)
self.myTask = TaskThread()
def initializePage(self):
[...]
self.setLayout(layout)
self.myTask.start()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
wizard = QWizard()
install = Install()
install.setTitle("installer")
install.myTask.taskFinished.connect(wizard.next)
wizard.addPage(install)
page = QWizardPage()
page.setTitle("next Page")
wizard.addPage(page)
wizard.show()
sys.exit(wizard.exec_())
I have a browser application written in python with PySide Qt. But now I want to add a button in the toolbar to print the website. How do I do this? Because CTRL + P is not working in the application.
Here is the code:
import sys
from PySide import QtCore, QtGui, QtWebKit, QtHelp, QtNetwork
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
Action1 = QtGui.QAction('Google', self)
Action1.setShortcut('Ctrl+M')
Action1.triggered.connect(self.load_message)
self.toolbar1 = self.addToolBar('Google')
self.toolbar1.addAction(Action1)
Action2 = QtGui.QAction('Yahoo', self)
Action2.setShortcut('Ctrl+H')
Action2.triggered.connect(self.load_list)
self.toolbar2 = self.addToolBar('Yahoo')
self.toolbar2.addAction(Action2)
exitAction = QtGui.QAction('Exit', self)
exitAction.setShortcut('Ctrl+Q')
exitAction.triggered.connect(self.on_exit)
self.toolbar3 = self.addToolBar('Exit')
self.toolbar3.addAction(exitAction)
self.resize(750, 750)
self.setWindowTitle('Browser')
self.web = QtWebKit.QWebView(self)
self.web.load(QtCore.QUrl('http://www.google.com'))
self.setCentralWidget(self.web)
def on_exit(self):
QtGui.qApp.quit
def load_message(self):
self.web.load(QtCore.QUrl('http://www.google.com'))
def load_list(self):
self.web.load(QtCore.QUrl('http://www.yahoo.com'))
app = QtGui.QApplication(sys.argv)
app.setWindowIcon(QtGui.QIcon('myicon.ico'))
main_window = MainWindow()
main_window.show()
sys.exit(app.exec_())
In your __init__ method add a Print action:
printAction = QtGui.QAction('Print', self)
printAction.setShortcut('Ctrl+P')
printAction.triggered.connect(self.do_print)
self.toolbar4 = self.addToolBar('Print')
self.toolbar4.addAction(printAction)
And create a do_print method:
def do_print(self):
p = QtGui.QPrinter()
p.setPaperSize(QtGui.QPrinter.A4)
p.setFullPage(True)
p.setResolution(300)
p.setOrientation(QtGui.QPrinter.Portrait)
p.setOutputFileName('D:\\test.pdf')
self.web.print_(p)
This will print to a file D:\test.pdf.
To configure your printer differently see the QPrinter documentation. Also if you want a print preview dialog see the QPrintPreviewDialog docs.
If you want a standard print dialog the use:
def do_print(self):
p = QtGui.QPrinter()
dialog = QtGui.QPrintDialog(p)
dialog.exec_()
self.web.print_(p)