How to process data after reading from QIODevice.read()? - python

This is my first time to ask question, if there are something I get wrong, please tell me, I will be appreciate.
I am using QWebEngineUrlSchemeHandler for a custom url, and I want to use QFile to open a javascript file for testing.
First, if I just use
QFile("ken.js")
The window could open the javascript, but if I use my custom QFile,
I have no idea how to process the data after I read from QIODevice.read().
I want to know what I need to do to make the window could open the javascript after I read the data from QIODevice.read().
Please give me some suggests, thank.
Here is my full code.
class TestQFile(QtCore.QFile):
def __init__(self, fileName):
super().__init__()
self.setFileName(fileName)
self.open(self.ReadOnly)
self.data = b''
while True:
receivedData = self.read(10)
self.data += receivedData
if receivedData == b'':
break
class TestHandler(QWebEngineUrlSchemeHandler):
def requestStarted(self, request):
self._dev = TestQFile("ken.js")
request.reply(b'text/javascript', self._dev)
class TestWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._view = QWebEngineView(self)
self._handler = TestHandler() # Must keep ref
self._view.page().profile().installUrlSchemeHandler(b'myuri', self._handler)
self._view.setHtml('<html><head><title>Test</title></head><body><div id="d1"></div><script src="myuri://test/ken.js"></script></body></html>')
self.setCentralWidget(self._view)
self.show()
self.raise_()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
win = TestWindow()
app.exec_()

Actually if the class inherits from QFile you do not have to do anything since it already has implemented the methods that QWebEngineUrlRequestJob requires since it will use the methods that every class that inherits from QIODevice must implement as readData(), writeData(), atEnd(), etc.
from PyQt5 import QtCore, QtWidgets, QtWebEngineCore,QtWebEngineWidgets
class TestQFile(QtCore.QFile):
def __init__(self, fileName):
super().__init__()
self.setFileName(fileName)
class TestHandler(QtWebEngineCore.QWebEngineUrlSchemeHandler):
def requestStarted(self, request):
self.file = TestQFile("ken.js")
request.reply(b'text/javascript', self.file)
class TestWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self._view = QtWebEngineWidgets.QWebEngineView(self)
self._handler = TestHandler() # Must keep ref
self._view.page().profile().installUrlSchemeHandler(b'myuri', self._handler)
self._view.setHtml('<html><head><title>Test</title></head><body><div id="d1"></div><script src="myuri://test/ken.js"></script></body></html>')
self.setCentralWidget(self._view)
self.show()
self.raise_()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
win = TestWindow()
sys.exit(app.exec_())

Related

Python, reference class instance/method in decorated function

I'm having a hard time finding a way to reference class instances in a decorator function.
import json
import time
import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from main_UI import Ui_ApplicationWindow
from slack import RTMClient
class WorkerThread(QThread):
finished = pyqtSignal(str)
def __init__(self):
QThread.__init__(self)
self.rtm_client = RTMClient(token="xoxp...")
def run(self):
self.rtm_client.start()
#RTMClient.run_on(event="message")
def say_hello(**payload):
data = payload['data']
if (len(data) != 0):
if "text" in data:
text = data['text']
self.finished.emit(str(text))
class ApplicationWindow(QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_ApplicationWindow()
self.ui.setupUi(self)
self.ui.pushButton.clicked.connect(self.start_rtm)
def start_rtm(self):
self.thread = WorkerThread()
self.thread.finished.connect(self.update)
self.thread.start()
#pyqtSlot(str)
def update(self, data):
self.ui.label.setText(data)
if __name__ == "__main__":
app = QApplication(sys.argv)
myWindow = ApplicationWindow()
myWindow.show()
app.exec_()
So in say_hello since it can't take self as an argument, I'm not able to use self.finished.emit(text) at the end of the function.
How can I reference a class instance/function using self in say_hello?
No, You can not. Instead of using the #RTMClient.run_on() decorator, use the RTMClient.on() function to register it.
import threading
import asyncio
from slack import RTMClient
from PyQt5 import QtCore, QtWidgets
class SlackClient(QtCore.QObject):
textChanged = QtCore.pyqtSignal(str)
def start(self):
RTMClient.on(event="message", callback=self.say_hello)
threading.Thread(target=self._start_loop, daemon=True).start()
def _start_loop(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
slack_token = "xoxb-...."
rtm_client = RTMClient(token=slack_token)
rtm_client.start()
def say_hello(self, **payload):
data = payload["data"]
if data:
if "text" in data:
text = data["text"]
self.textChanged.emit(text)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
client = SlackClient()
button = QtWidgets.QPushButton("Start")
textedit = QtWidgets.QPlainTextEdit()
button.clicked.connect(client.start)
client.textChanged.connect(textedit.appendPlainText)
w = QtWidgets.QWidget()
lay = QtWidgets.QVBoxLayout(w)
lay.addWidget(button)
lay.addWidget(textedit)
w.show()
sys.exit(app.exec_())
Update:
import sys
import threading
import asyncio
from slack import RTMClient
from PyQt5 import QtCore, QtWidgets
from main_UI import Ui_ApplicationWindow
class SlackClient(QtCore.QObject):
textChanged = QtCore.pyqtSignal(str)
def start(self):
RTMClient.on(event="message", callback=self.say_hello)
threading.Thread(target=self._start_loop, daemon=True).start()
def _start_loop(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
slack_token = "xoxb-...."
rtm_client = RTMClient(token=slack_token)
rtm_client.start()
def say_hello(self, **payload):
data = payload["data"]
if data:
if "text" in data:
text = data["text"]
self.textChanged.emit(text)
class ApplicationWindow(QtWidgets.QMainWindow):
def __init__(self):
super(ApplicationWindow, self).__init__()
self.ui = Ui_ApplicationWindow()
self.ui.setupUi(self)
self.client = SlackClient()
# connections
self.ui.pushButton.clicked.connect(self.client.start)
self.client.textChanged.connect(self.ui.label.setText)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myWindow = ApplicationWindow()
myWindow.show()
sys.exit(app.exec_())
Actually you cannot self since that is a global variable, not a class one.
from slack import RTMClient
class WorkerThread(QThread):
finished = pyqtSignal(dict)
def __init__(self):
QThread.__init__(self)
self.rtm_client = RTMClient(token="xoxp-....")
def run(self):
self.rtm_client.start()
#RTMClient.run_on(event="message")
def say_hello(**payload):
data = payload['data']
if (len(data) != 0):
if "text" in data:
text = data['text']
WorkerThread.finished.emit(text) <--- using self impossible
I suggest that you make such variable private by appending two underscores at the beginning (__my_private_var)

Writing a custom QPushButton class in Python

I've recently started learning PyQt on my own and I've come in some trouble trying to write a custom class that inherits from QPushButton so I can adjust its attributes. I'm trying to pass a text as an argument whenever I initialize an object of this class. I am pretty sure there's something wrong with my init but I haven't found it yet.
Here is the code:
import sys
from PySide import QtGui, QtCore
class mainb(QtGui.QPushButton):
def __init__(Text,self, parent = None):
super().__init__(parent)
self.setupbt(Text)
def setupbt(self):
self.setFlat(True)
self.setText(Text)
self.setGeometry(200,100, 60, 35)
self.move(300,300)
print('chegu aqui')
self.setToolTip('Isso é muito maneiro <b>Artur</b>')
self.show()
class mainwindow(QtGui.QWidget):
def __init__(self , parent = None):
super().__init__()
self.setupgui()
def setupgui(self):
self.setToolTip('Oi <i>QWidget</i> widget')
self.resize(800,600)
self.setWindowTitle('Janela do Artur')
af = mainb("Bom dia",self)
self.show()
"""
btn = QtGui.QPushButton('Botão',self)
btn.clicked.connect(QtCore.QCoreApplication.instance().quit)
btn.resize(btn.sizeHint())
btn.move(300, 50)
"""
def main():
app = QtGui.QApplication(sys.argv)
ex = mainwindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You are using super in wrong way, super must get a instance and another thing your first arg is Text, that's wrong that should be self. I fixed some more and the below code should work for you
import sys
from PySide import QtGui, QtCore
class mainb(QtGui.QPushButton):
def __init__(self, Text, parent = None):
super(mainb, self).__init__()
self.setupbt(Text)
def setupbt(self, Text):
self.setFlat(True)
self.setText(Text)
self.setGeometry(200,100, 60, 35)
self.move(300,300)
print('chegu aqui')
self.setToolTip('Isso muito maneiro <b>Artur</b>')
self.show()
class mainwindow(QtGui.QWidget):
def __init__(self , parent = None):
super(mainwindow, self).__init__()
self.setupgui()
def setupgui(self):
self.setToolTip('Oi <i>QWidget</i> widget')
self.resize(800,600)
self.setWindowTitle('Janela do Artur')
newLayout = QtGui.QHBoxLayout()
af = mainb("Bom dia",self)
newLayout.addWidget(af)
self.setLayout(newLayout)
self.show()
def main():
app = QtGui.QApplication(sys.argv)
ex = mainwindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Your def setupbt(self) does not seem to take the text as argument. Try def setupbt(self, Text): instead.

How to access attribute of the main window

I am writing a program and I want for external function to access attribute of the main window. It must be an external function, because it is provided by import method. I have created a really small MWE to give an idea of the problem. Here function func is supposed to print value of MainWindow's vari.
class importedclass():
def func(self):
print(win.vari)
x=importedclass()
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.vari = 1
x.func()
def main():
app = QtGui.QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
Just to make it clear: importedclass class is my fix-up class, I am not allowed to change anything in MainWindow class!
Because each one needs an instance of the other, one of them needs to pass itself to the other. Check if this example solution fully addresses your problem.
# file 1
class External():
def func(self, win):
print(win.vari)
# file 2
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.vari = 1
import External
x = External()
x.func(self)
def main():
app = QtGui.QApplication(sys.argv)
win = MainWindow()
sys.exit(app.exec_())

Python: PyQt QTreeview example - selection

I'm using Python 2.7 and Qt designer and I'm new to MVC:
I have a View completed within Qt to give me a directory tree list, and the controller in place to run things. My question is:
Given a Qtree view, how may I obtain a directory once a dir is selected?
Code snap shot is below, I suspect it's SIGNAL(..) though I'm unsure:
class Main(QtGui.QMainWindow):
plot = pyqtSignal()
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# create model
model = QtGui.QFileSystemModel()
model.setRootPath( QtCore.QDir.currentPath() )
# set the model
self.ui.treeView.setModel(model)
**QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test)**
def test(self):
print "hello!"
The signal you're looking for is selectionChanged emmited by the selectionModel owned by your tree. This signal is emmited with the selected item as first argument and the deselected as second, both are instances of QItemSelection.
So you might want to change the line:
QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test)
to
QtCore.QObject.connect(self.ui.treeView.selectionModel(), QtCore.SIGNAL('selectionChanged()'), self.test)
Also I recommend you to use the new style for signals and slots. Redefine your test function as:
#QtCore.pyqtSlot("QItemSelection, QItemSelection")
def test(self, selected, deselected):
print("hello!")
print(selected)
print(deselected)
Here you have a working example:
from PyQt4 import QtGui
from PyQt4 import QtCore
class Main(QtGui.QTreeView):
def __init__(self):
QtGui.QTreeView.__init__(self)
model = QtGui.QFileSystemModel()
model.setRootPath( QtCore.QDir.currentPath() )
self.setModel(model)
QtCore.QObject.connect(self.selectionModel(), QtCore.SIGNAL('selectionChanged(QItemSelection, QItemSelection)'), self.test)
#QtCore.pyqtSlot("QItemSelection, QItemSelection")
def test(self, selected, deselected):
print("hello!")
print(selected)
print(deselected)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
PyQt5
In PyQt5 is a little bit different (thanks to Carel and saldenisov for comments and aswer.)
... connect moved from being an object method to a method acting upon the attribute when PyQt went from 4 to 5
So instead the known:
QtCore.QObject.connect(self.ui.treeView, QtCore.SIGNAL('clicked()'), self.test)
now you write:
class Main(QTreeView):
def __init__(self):
# ...
self.setModel(model)
self.doubleClicked.connect(self.test) # Note that the the signal is now a attribute of the widget.
Here is a the example (by saldenisov) using PyQt5.
from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication
class Main(QTreeView):
def __init__(self):
QTreeView.__init__(self)
model = QFileSystemModel()
model.setRootPath('C:\\')
self.setModel(model)
self.doubleClicked.connect(self.test)
def test(self, signal):
file_path=self.model().filePath(signal)
print(file_path)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
In PyQt5 it can be done in this way:
from PyQt5.QtWidgets import QTreeView,QFileSystemModel,QApplication
class Main(QTreeView):
def __init__(self):
QTreeView.__init__(self)
model = QFileSystemModel()
model.setRootPath('C:\\')
self.setModel(model)
self.doubleClicked.connect(self.test)
def test(self, signal):
file_path=self.model().filePath(signal)
print(file_path)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
w = Main()
w.show()
sys.exit(app.exec_())
If I understand the question correctly you would like the directory or file name selected.
This is what I do:
from PyQt4 import QtGui
from PyQt4 import QtCore
# ---------------------------------------------------------------------
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.resize(600,400)
self.setWindowTitle("Treeview Example")
self.treeview = QtGui.QTreeView(self)
self.treeview.model = QtGui.QFileSystemModel()
self.treeview.model.setRootPath( QtCore.QDir.currentPath() )
self.treeview.setModel(self.treeview.model)
self.treeview.setColumnWidth(0, 200)
self.setCentralWidget(self.treeview)
self.treeview.clicked.connect(self.on_treeview_clicked)
# ---------------------------------------------------------------------
#QtCore.pyqtSlot(QtCore.QModelIndex)
def on_treeview_clicked(self, index):
indexItem = self.treeview.model.index(index.row(), 0, index.parent())
# path or filename selected
fileName = self.treeview.model.fileName(indexItem)
# full path/filename selected
filePath = self.treeview.model.filePath(indexItem)
print(fileName)
print(filePath)
# ---------------------------------------------------------------------
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
I tried this alternative to get the file name...
Instead of:
indexItem = self.treeview.model.index(index.row(), 0, index.parent())
# path or filename selected
fileName = self.treeview.model.fileName(indexItem)
I tried:
# path or filename selected
fileName = index.internalPointer().fileName
Which also seems to work...

PySide threading and http downloading

I've had soooo much trouble getting this code to work properly!!!! It runs fine when I debug it step by step, but when running normally it just crashes. Initially I was using a QThread to update the ImagePreview pixmap, but after a whole day of crashes and pain, I changed course. Now it works, in the above scenario when using the debugger, but otherwise I'm stumped. Please help me! What's wrong with this code? Is there another approach I can use? I'm trying to constantly update the image preview with an image downloaded from a url.
import sys
import io
import urllib2
from PySide import QtCore, QtGui, QtNetwork
import time
class QDownloadBuffer(QtCore.QBuffer):
downloadFinished = QtCore.Signal()
def __init__(self):
super(QDownloadBuffer, self).__init__()
self.open(QtCore.QBuffer.ReadWrite)
self.url = QtCore.QUrl("http://www.google.com.au/images/srpr/logo3w.png")
self.manager = QtNetwork.QNetworkAccessManager()
self.request = QtNetwork.QNetworkRequest(self.url)
self.manager.finished.connect(self.onFinished)
def startDownload(self):
print("Starting Download --")
self.reply = self.manager.get(self.request)
self.reply.error[QtNetwork.QNetworkReply.NetworkError].connect(self.onError)
def onFinished(self):
print("Download Finished -- ")
print(self.write(self.reply.readAll()))
self.reply.close()
self.downloadFinished.emit()
def onError(self):
print("oh no there is an error -- ")
print(self.reply.error())
class ImagePreview(QtGui.QWidget):
def __init__(self, parent=None):
super(ImagePreview, self).__init__(parent)
self.setMinimumSize(50, 50)
self.text = None
self.pixmap = None
self.dl_n = 0
def paintEvent(self, paintEvent):
painter = QtGui.QPainter(self)
if(self.pixmap):
painter.drawPixmap(0, 0, self.pixmap)
if(self.text):
painter.setPen(QtCore.Qt.blue)
painter.setFont(QtGui.QFont("Arial", 30))
painter.drawText(self.rect(), QtCore.Qt.AlignCenter, self.text)
def startDownload(self):
self.setText(str(self.dl_n))
self.dl_n += 1
print("Starting Download {0}".format(self.dl_n))
self.db = QDownloadBuffer()
self.connect(self.db, QtCore.SIGNAL("downloadFinished()"), self, QtCore.SLOT("ondownloadFinished()"))
self.db.startDownload()
def ondownloadFinished(self):
self.paintImage()
print("download finished?")
self.db.close()
self.startDownload()
def paintImage(self):
print("Painting")
pixmap = QtGui.QPixmap()
pixmap.loadFromData(self.db.data())
self.setPixmap(pixmap)
def setPixmap(self, pixmap):
self.pixmap = pixmap
self.setMinimumSize(pixmap.width(), pixmap.height())
self.update()
def setText(self, text):
self.text = text
self.update()
class MainWindow(QtGui.QWidget):
def __init__(self):
super(MainWindow, self).__init__()
self.imagepreview = ImagePreview()
self.button = QtGui.QPushButton("Start")
self.button.clicked.connect(self.imagepreview.startDownload)
layout = QtGui.QVBoxLayout()
layout.addWidget(self.button)
layout.addWidget(self.imagepreview)
self.setLayout(layout)
if __name__ == "__main__":
import sys
try:
app = QtGui.QApplication(sys.argv)
except RuntimeError:
pass
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
I think the problem is that you are calling self.startDownload() from slot (signal handler). So you are not returning control to Qt main loop (or something like this). Proper way is to call it as deferred event, e.g. by calling it through QTimer.singleShot:
def ondownloadFinished(self):
self.paintImage()
print("download finished?")
self.db.close()
QtCore.QTimer.singleShot(0, self.startDownload)
Note that singleShot with msec set to 0:
QtCore.QTimer.singleShot(0, self.startDownload)
is the same as:
QtCore.QMetaObject.invokeMethod(self, 'startDownload', QtCore.Qt.QueuedConnection)
(source, related question)

Categories

Resources