QEventManager使用多线程的方式对任务进行处理,qasync的另一个替代
Project description
QEventManager
介绍
QEventManager使用多线程的方式对任务进行处理,qasync的另一个替代
软件架构
依赖于Qt的QThread类,使用多线程的方式对任务进行处理。
安装教程
pip install qeventmanager
使用说明
加载Url图片
# coding: utf-8
import sys
from PySide6.QtCore import Qt
from PySide6.QtGui import QImage, QPixmap
from qeventmanager import qevent_manager
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton
class Demo(QWidget):
def __init__(self):
super().__init__()
self.verticalLayout = QVBoxLayout(self)
self.pushButton = QPushButton(self)
self.pushButton.setText("Click me")
self.label = QLabel(self)
self.label.setText("Hello, world!")
self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.label.setScaledContents(True)
self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
self.verticalLayout.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.label, 1)
self.pushButton.clicked.connect(self.handle_click)
def handle_click(self):
url = 'https://i.loli.net/2018/12/06/5c0867986a2a0.jpg'
qevent_manager.addLoadImageFromUrl(url, slot=self.handle_image, use_pil=False)
def handle_image(self, image: QImage):
image = image.scaledToWidth(self.verticalLayout.geometry().width())
self.label.setPixmap(QPixmap.fromImage(image))
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = Demo()
demo.resize(1000, 600)
demo.show()
sys.exit(app.exec())
加载本地图片
# coding: utf-8
import sys
from PySide6.QtCore import Qt
from PySide6.QtGui import QImage, QPixmap
from qeventmanager import qevent_manager
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton
class Demo(QWidget):
def __init__(self):
super().__init__()
self.verticalLayout = QVBoxLayout(self)
self.pushButton = QPushButton(self)
self.pushButton.setText("Click me")
self.label = QLabel(self)
self.label.setText("Hello, world!")
self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.label.setScaledContents(True)
self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
self.verticalLayout.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.label, 1)
self.pushButton.clicked.connect(self.handle_click)
def handle_click(self):
url = './img.jpg'
qevent_manager.addLoadImageFromFile(url, slot=self.handle_image, use_pil=False)
def handle_image(self, image: QImage):
image = image.scaledToWidth(self.verticalLayout.geometry().width())
self.label.setPixmap(QPixmap.fromImage(image))
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = Demo()
demo.resize(1000, 600)
demo.show()
sys.exit(app.exec())
加载自定义函数
# coding: utf-8
import sys
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QPushButton
from requests import Response, post
from qeventmanager import qevent_manager
def baidu():
url = 'https://wallpaper.soutushenqi.com/timeStamp?product_id=52&version_code=29106&sign=EB9C805D055305DB83FCAFEA541B9714'
response = post(url)
response.raise_for_status()
response.encoding = 'utf-8'
return response
class Demo(QWidget):
def __init__(self):
super().__init__()
self.verticalLayout = QVBoxLayout(self)
self.pushButton = QPushButton(self)
self.pushButton.setText("Click me")
self.label = QLabel(self)
self.label.setText("Hello, world!")
self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.label.setScaledContents(True)
self.verticalLayout.setAlignment(Qt.AlignmentFlag.AlignTop)
self.verticalLayout.addWidget(self.pushButton)
self.verticalLayout.addWidget(self.label, 1)
self.pushButton.clicked.connect(self.handle_click)
def handle_click(self):
qevent_manager.addTask(baidu, slot=self.handle)
# qevent_manager.addTaskToPool(baidu, slot=self.handle)
def handle(self, response: Response):
self.label.setText(response.text)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = Demo()
demo.resize(1000, 600)
demo.show()
sys.exit(app.exec())
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
qeventmanager-1.0.1.tar.gz
(18.9 kB
view details)
File details
Details for the file qeventmanager-1.0.1.tar.gz.
File metadata
- Download URL: qeventmanager-1.0.1.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d86d2a562e08c6c7ac9fb1cf74679d8d967fc7d75c762b508ee1a01aa0dd408
|
|
| MD5 |
e21af01908dcbe4bbdad3c92551eeef5
|
|
| BLAKE2b-256 |
2d955ed78e6c327ff81143d3ac0f95dd95d3b93a20efe1dcb4a3a5751ef9a87a
|