I use the QWebView (python 3.3 + pyside 1.1.2 + Qt 4.8) as FileViewer. Picture, Text, HTML, ... all fine, but PDF has a display problem. I tested two possible ways.
internal pdf viewer: after use webview.load(file) it loads, but
the screen is blank, after loading another file, all works fine, it
shows the file
pdf.js: after use setContent() with filebase, it
loads the webviewer.html/.js with a white page and the loading circle. The
screen only refresh if I resize the form or use the scrollbars, but
then all is fine
I don't find an event for "plugin/javascript finished loading", so I could force a repaint or so.
Here an example code for case 1:
import sys
from PySide import QtCore, QtGui, QtWebKit ##UnusedWildImport
class DialogTest(QtGui.QDialog):
def __init__(self, parent = None):
super(DialogTest, self).__init__(parent)
self.resize(620, 600)
self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
self.PreviewBox = QtWebKit.QWebView()
self.PreviewBox.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True)
self.PreviewBox.settings().setAttribute(QtWebKit.QWebSettings.WebAttribute.DeveloperExtrasEnabled, True)
self.PreviewBox.settings().setAttribute(QtWebKit.QWebSettings.PrivateBrowsingEnabled, True)
self.PreviewBox.settings().setAttribute(QtWebKit.QWebSettings.LocalContentCanAccessRemoteUrls, True)
self.PreviewBox.loadFinished.connect(self._loadfinished)
self.button_test1 = QtGui.QPushButton("File 1")
self.button_test1.clicked.connect(self._onselect1)
self.button_test2 = QtGui.QPushButton("File 2")
self.button_test2.clicked.connect(self._onselect2)
layout_Buttons = QtGui.QHBoxLayout()
layout_Buttons.addWidget(self.button_test1)
#layout_Buttons.addStretch()
layout_Buttons.addWidget(self.button_test2)
layout_Main = QtGui.QVBoxLayout()
layout_Main.addLayout(layout_Buttons)
layout_Main.addWidget(self.PreviewBox)
self.setLayout(layout_Main)
def Execute(self):
self.show()
self.exec_()
def _onselect1(self):
self.PreviewBox.load(QtCore.QUrl().fromLocalFile("c:\\tmp\\test1.pdf"))
def _onselect2(self):
self.PreviewBox.load(QtCore.QUrl().fromLocalFile("c:\\tmp\\test2.pdf"))
def _loadfinished(self, ok):
#self.PreviewBox.repaint()
pass
app = QtGui.QApplication(sys.argv)
DialogTest().Execute()
Edit: Workaround
Case 1 (webkit plugin) has an otherbug, it takes the focus to itself, so this solution isn't acceptable to me. I played with the pdf.js again and found a workaroud:
self.PreviewBox.setHtml(content, baseUrl = QtCore.QUrl().fromLocalFile(path))
self.PreviewBox.hide()
QtCore.QTimer.singleShot(700, self.PreviewBox.show)
The hide() must be after the content filling and the timer haven't to be too low.
//jay
I just solved a similar problem cleaning the QWebView before every pdf load.
Be careful with the loadFinished() signal.
In your example:
self.PreviewBox.load(QUrl('about:blank'))
or, in case we don't like 'about:blank' this may be a more portable solution:
self.PreviewBox.setHtml('<html><head></head><title></title><body></body></html>')
Related
I'm trying to create an application that contains a web browser within it, but when I add the web browser my menu bar visually disappears but functionally remains in place. The following are two images, one showing the "self.centralWidget(self.web_widget)" commented out, and the other allows that line to run. If you run the example code, you will also see that while visually the entire web page appears as if the menu bar wasn't present, you have to click slightly below each entry field and button in order to activate it, behaving as if the menu bar was in fact present.
Web Widget Commented Out
Web Widget Active
Example Code
import os
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
class WebPage(QWebEngineView):
def __init__(self, parent=None):
QWebEngineView.__init__(self)
self.current_url = ''
self.load(QUrl("https://facebook.com"))
self.loadFinished.connect(self._on_load_finished)
def _on_load_finished(self):
print("Url Loaded")
class MainWindow(QMainWindow):
def __init__(self, parent=None):
# Initialize the Main Window
super(MainWindow, self).__init__(parent)
self.create_menu()
self.add_web_widet()
self.show()
def create_menu(self):
''' Creates the Main Menu '''
self.main_menu = self.menuBar()
self.main_menu_actions = {}
self.file_menu = self.main_menu.addMenu("Example File Menu")
self.file_menu.addAction(QAction("Testing Testing", self))
def add_web_widet(self):
self.web_widget = WebPage(self)
self.setCentralWidget(self.web_widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.showMaximized()
sys.exit(app.exec_()) # only need one app, one running event loop
Development Environment
Windows 10, PyQt5, pyqt5-5.9
EDIT
The problem doesn't seem to be directly related to the menu bar. Even removing the menu bar the issue still occurs. That said, changing from showMaximized() to showFullScreen() does seem to solve the problem.
I no longer believe this is an issue with PyQt5 specifically but rather a problem with the graphics driver. Specifically, if you look at Atlassian's HipChat application it has a similar problem which is documented here:
https://jira.atlassian.com/browse/HCPUB-3177
Some individuals were able to solve the problem by running the application from the command prompt with the addendum "--disable-gpu" but that didn't work for my python application. On the other hand, rolling back the Intel(R) HD Graphics Driver did solve my problem. Version 21.20.16.4627 is the one that seems to be causing problems.
I am having trouble figuring out how to use Qt to create translation files for a python apllication.
I'm using python 2.7, Qt version 5.9.1 and PyQt4 4.12.1 to create my GUI on OSX 10.11.6.
For now I just wanted to translate a few words on my code.
For what I understand, I have to use QtLinguist to open a .ts file, translate the words and create a .qm file, which will then be used by python.
From Qt Linguist page I get that I need to use a .pro project file, that will be read by pylupdate4, etc...
Now, I do I create a .pro file?
I tried running:
$ qmake -project myfile.py
$ pylupdate4 myfile.pro -ts file.ts
but the resulting .pro file can't be read by pylupdate4 (XML error: Parse error at line 1, column 1 [...])
From this Tutorial, I tried:
$ pylupdate4 myfile.py -ts file.ts
Which creates an empty .ts file, that Qt Linguist can't open.
Can someone give my any tip on what might be wrong, the 15 tabs I have open in my browser are not helping.
Here's my python code if you need it:
import sys
import os.path as osp
import os
from PyQt4 import QtGui, QtCore
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow,self).__init__()
# Set MainWindow geometry, use settings of last session. If it's first session,
# use defaulted settings
self.settings = QtCore.QSettings('Paul',QtCore.QSettings.NativeFormat)
self.resize(self.settings.value("size", QtCore.QSize(500, 300)).toSize())
self.move(self.settings.value("pos", QtCore.QPoint(5, 5)).toPoint());
self.initUI()
def closeEvent(self, e):
#Save MainWindow geometry session when closing the window
self.settings.setValue("size",self.size())
self.settings.setValue("pos",self.pos())
e.accept()
def initUI(self):
self.hbox = QtGui.QVBoxLayout(self) # Create Vertival box layout to put the buttons
self.myButtons = QtGui.QPushButton('button',self) #create push button
self.myButtons.setStyleSheet("""QPushButton { background-color: red; font:bold 20px}""")
self.myButtons.setToolTip('Push this button')
self.myButtons.setText(self.tr(QtCore.QString('yes')))
comboBox=QtGui.QComboBox(self) #create drop down menu
comboBox.addItem('Portugues')
comboBox.addItem('English')
self.hbox.addWidget(comboBox,1,QtCore.Qt.AlignRight) #add drop down menu to box layout
self.hbox.addStretch(3) # set separation between buttons
self.myButtons.clicked.connect(self.buttonClicked) # what should the button do
self.hbox.addWidget(self.myButtons,1,QtCore.Qt.AlignRight) #add button to box layout
self.setWindowTitle('Test2')
self.show()
def buttonClicked(self):
msbox= QtGui.QMessageBox()
choice=msbox.warning(self,'ok',"This button doesn't do anything!!!")
if choice == QtGui.QMessageBox.No:
print('nanan')
else:
print('Bye')
self.settings.setValue("size",self.size());
self.settings.setValue("pos",self.pos());
sys.exit()
def main():
app = QtGui.QApplication(sys.argv)
translator = QtCore.QTranslator()
translator.load("~/basefiles/translations/qt_pt.qm")
app.installTranslator(translator)
ex = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
When you use self.tr you must pass the string, not the QString variable, in your case it changes:
self.myButtons.setText(self.tr(QtCore.QString('yes')))
to
self.myButtons.setText(self.tr("yes"))
And run everything again.
I am using PyQt5 5.5.1 (64-bit) with Python 3.4.0 (64-bit) on Windows 8.1
64-bit.
I am having trouble restoring the position and size (geometry) of my
very simple PyQt app.
Here is minimal working application:
import sys
from PyQt5.QtWidgets import QApplication, QWidget
class myApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
view = myApp()
sys.exit(app.exec())
What I read online is that this is the default behavior and we need to
use QSettings to save and retrieve settings from Windows registry,
which is stored in
\\HKEY_CURRENT_USER\Software\{CompanyName}\{AppName}\
Here are some of the links I read.
I could have followed those tutorials but those tutorials/docs were
written for C++ users.
C++ is not my glass of beer, and converting those codes are impossible to me.
Related:
QSettings(): How to save to current working directory
This should do.
import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtCore import QSettings, QPoint, QSize
class myApp(QWidget):
def __init__(self):
super(myApp, self).__init__()
self.settings = QSettings( 'My company', 'myApp')
# Initial window size/pos last saved. Use default values for first time
self.resize(self.settings.value("size", QSize(270, 225)))
self.move(self.settings.value("pos", QPoint(50, 50)))
def closeEvent(self, e):
# Write window size and position to config file
self.settings.setValue("size", self.size())
self.settings.setValue("pos", self.pos())
e.accept()
if __name__ == '__main__':
app = QApplication(sys.argv)
frame = myApp()
frame.show()
app.exec_()
I simplified this example: QSettings(): How to save to current working directory
Similar to #Valentin's response, because I feel settings are being written to registry, which will be issue for cross compatiblity. Here is the relevant startEvent() and closeEvent() for the job.
def startEvent()
self.settings = QSettings(QSettings.IniFormat,QSettings.SystemScope, '__MyBiz', '__settings')
self.settings.setFallbacksEnabled(False) # File only, not registry or or.
# setPath() to try to save to current working directory
self.settings.setPath(QSettings.IniFormat,QSettings.SystemScope, './__settings.ini')
# Initial window size/pos last saved
self.resize(self.settings.value("size", QSize(270, 225)))
self.move(self.settings.value("pos", QPoint(50, 50)))
self.tab = QWidget()
def closeEvent(self, e):
# Write window size and position to config file
self.settings.setValue("size", self.size())
self.settings.setValue("pos", self.pos())
startEvent() should be initiated at startup and closeEvent() should be taken care before quitting the main window.
You should indeed use QSetting for this.
All the Qt examples have been converted to Python. They are included in the source packages of PyQt (or PySide), which you can download here
You can also look online in the github repo, particularly in application.py of mainwindows example.
def readSettings(self):
settings = QSettings("Trolltech", "Application Example")
pos = settings.value("pos", QPoint(200, 200))
size = settings.value("size", QSize(400, 400))
self.resize(size)
self.move(pos)
def writeSettings(self):
settings = QSettings("Trolltech", "Application Example")
settings.setValue("pos", self.pos())
settings.setValue("size", self.size())
Fire writeSettings() before quitting and initiate readSettings() on startup.
In my case I use .ini files to store settings (language, default user, ...). the same code works on both Debian and Windows.
An example:
from PySide.QtCore import QSettings
self.settings = QSettings('settings.ini', QSettings.IniFormat)
...
self.settings.setValue('size', self.size())
I am trying to get a PrinterDialog for users to choose all the options for printing.
When I try this with PySide, the returned printer is always the same (default I guess), no matter what changes the user makes in the dialog. The same happens when I pass it a printer initially.
I have come up with this small self-contained example to see what I am talking about.
USE_PYQT4 = True
if USE_PYQT4:
from PyQt4 import QtGui, QtCore
else:
from PySide import QtGui, QtCore
import sys
class Window(QtGui.QWidget):
def __init__(self):
super(Window, self).__init__()
self.printBtn = QtGui.QPushButton("Print Preview")
self.printBtn.clicked.connect(self.onPrint)
# Will store the QPrinter instance
self.printer = None
# Will store the QPrintDialog, because it
# might get garbage-collected, and the QPrinter with it
self.pdialog = None
layout = QtGui.QVBoxLayout()
layout.addWidget(self.printBtn)
self.setLayout(layout)
def printState(self):
print '*'*20
if self.printer is None:
print '-- no printer -- '
else:
print 'printer name', self.printer.printerName()
print 'page size', self.printer.pageSize()
print 'paper size', self.printer.paperSize()
print
print
def askPrinter(self):
# Either like this:
#
# self.printer = QtGui.QPrinter()
# self.pdialog = dialog = QtGui.QPrintDialog(self.printer)
# Or like that:
#
self.pdialog = dialog = QtGui.QPrintDialog()
dialog.setOption(QtGui.QAbstractPrintDialog.PrintCollateCopies, True)
dialog.setOption(QtGui.QAbstractPrintDialog.PrintCurrentPage, True)
dialog.setOption(QtGui.QAbstractPrintDialog.PrintPageRange, True)
dialog.setOption(QtGui.QAbstractPrintDialog.PrintSelection, True)
dialog.setOption(QtGui.QAbstractPrintDialog.PrintShowPageSize, True)
dialog.setOption(QtGui.QAbstractPrintDialog.PrintToFile, True)
print 'before PrintDialog'
self.printState()
if dialog.exec_() != QtGui.QDialog.Accepted:
return False
# And that, if not set before
self.printer = dialog.printer()
print 'after PrintDialog'
self.printState()
return True
def onPrint(self):
if not self.askPrinter():
print 'oops'
return
self.preview()
def preview(self):
dialog = QtGui.QPrintPreviewDialog(self.printer)
dialog.paintRequested.connect(self.handlePaintRequest)
dialog.exec_()
def handlePaintRequest(self, printer):
doc = QtGui.QTextDocument()
cursor = QtGui.QTextCursor(doc)
cursor.insertText("""
SOME TEXT
""")
cursor.insertText("-"*20)
data = [('Line {}'.format(i), '{} times'.format(i*2)) for i in xrange(20)]
table = cursor.insertTable(len(data), max(len(d) for d in data))
for row in range(table.rows()):
for column in range(table.columns()):
cursor.insertText(unicode(data[row][column]))
cursor.movePosition(QtGui.QTextCursor.NextCell)
cursor.movePosition(QtGui.QTextCursor.NextBlock)
cursor.insertText("-"*20)
cursor.insertText("""
And that's it
""")
doc.print_(printer)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
app.window = Window()
app.window.show()
sys.exit(app.exec_())
Note that when using PyQt4, the issue is different:
In PySide, if the page size is not set manually, it takes A4 (maybe it's the default for the first printer). Also, the options set from setOption seem to be ignored...
In PyQt4, the page size is set to 0, whatever that is, unless the page size dialog is opened. When the page size is 0, it's definitely different than when the page size dialog is opened. Also, the page margins are set to 0 here. However, the options are respected and it takes the correct default values (set through the CUPS admin page).
What is weird is that I checked the Kate print dialog, and it's completely correct. Kate is a Qt application in C++. I couldn't test that myself on C++ though.
Is this the correct way to create a QPrintDialog or am I doing something wrong?
Why is there that much difference? Shouldn't it all be delegated to Qt in the end?
How can I overcome this difference? (i.e. by manually taking the differences and filling the values or something)
I'm on Python 2.7.3 with PySide 1.1.0 and PyQt4 4.10.1 on Linux.
Thanks in advance for any input.
Update:
I tried the example provided with PySide, and it has the same issues; so most probably I'm not doing anything wrong.
Also, I checked bug reports. There are lots of them, none of which lead to anywhere. Some are fixed for Qt5, some ignored.
I tried your piece of code. It works perfectly and returns the correct printer and page setup. on win xp, PySide 1.1.2 and python 3.3.2. it may be a problem with your qt libraries or some os mis-configurations.
I'm currently trying to implement some kind of file browser / "explorer" into a programme... I'm using Python and PySide in connection with the Qt-window-toolkit. More or less this youtube-video shows the behaviour I want to have at the end. However, this tutorial used C++ as programming language and I haven't been able yet to reason the right python code from the C++ example.
Basically, my problem is to get the right column (file view) showing the content of the folder clicked in the left column (tree-style folder view).
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PySide import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.resize(600, 600)
self.fileBrowserWidget = QtGui.QWidget(self)
self.setCentralWidget(self.fileBrowserWidget)
self.dirmodel = QtGui.QFileSystemModel()
# Don't show files, just folders
self.dirmodel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.AllDirs)
self.folder_view = QtGui.QTreeView(parent=self);
self.folder_view.setModel(self.dirmodel)
self.folder_view.clicked[QtCore.QModelIndex].connect(self.clicked)
# Don't show columns for size, file type, and last modified
self.folder_view.setHeaderHidden(True)
self.folder_view.hideColumn(1)
self.folder_view.hideColumn(2)
self.folder_view.hideColumn(3)
self.selectionModel = self.folder_view.selectionModel()
self.filemodel = QtGui.QFileSystemModel()
# Don't show folders, just files
self.filemodel.setFilter(QtCore.QDir.NoDotAndDotDot | QtCore.QDir.Files)
self.file_view = QtGui.QListView(parent=self);
self.file_view.setModel(self.filemodel)
splitter_filebrowser = QtGui.QSplitter()
splitter_filebrowser.addWidget(self.folder_view)
splitter_filebrowser.addWidget(self.file_view)
splitter_filebrowser.setStretchFactor(0,2)
splitter_filebrowser.setStretchFactor(1,4)
hbox = QtGui.QHBoxLayout(self.fileBrowserWidget)
hbox.addWidget(splitter_filebrowser)
def set_path(self):
self.dirmodel.setRootPath("")
def clicked(self, index):
# get selected path of folder_view
index = self.selectionModel.currentIndex()
dir_path = self.dirmodel.filePath(index)
###############################################
# Here's my problem: How do I set the dir_path
# for the file_view widget / the filemodel?
###############################################
self.filemodel.setRootPath(dir_path)
app = QtGui.QApplication(sys.argv)
main = MainWindow()
main.show()
main.set_path()
sys.exit(app.exec_())
As you can see in my code, I've already tried to use the setRootPath-function... however, that doesn't seem to be the correct one. So I wonder, what I've got to do for getting this to work?
You need to set the root index to the appropriate one in the file model. You can do this by adding the following line to the end of the clicked() function:
self.file_view.setRootIndex(self.filemodel.index(dir_path))
I was able to figure it out from my experience using Qt in C++. The documentation for Qt in C++ is really quite good if you can figure out how it translates to Python. I was able to figure this out by looking at the QFileSystemModel documentation.
You need to set the root index of the files list view:
def clicked(self, index):
# the signal passes the index of the clicked item
dir_path = self.filemodel.filePath(index)
root_index = self.filemodel.setRootPath(dir_path)
self.file_view.setRootIndex(root_index)