问题描述
当线程中发出信号并捕获到主gui中时,以下代码在单击按钮或单击几下后崩溃。
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtGui import QPixmap,QImage
from PyQt5.QtCore import pyqtSignal,pyqtSlot,Qt,QThread
import numpy as np
import time
from PyQt5.QtWidgets import QApplication,QDialog,QPushButton,QVBoxLayout
def convert_np_qimage(cv_img,width,height):
h,w,ch = cv_img.shape
bytes_per_line = ch * w
qim = QtGui.QImage(cv_img.data,h,bytes_per_line,QtGui.QImage.Format_RGB888)
print(qim.size())
return qim
class VideoThread(QThread):
change_qimage_signal = pyqtSignal(QImage)
def __init__(self):
super().__init__()
def run(self):
print("run")
width = 1280
height = 1280
cv_img = np.zeros([height,3],dtype=np.uint8)
cv_img.fill(255)
print("image shape: ",cv_img.shape)
qimg = convert_np_qimage(cv_img,height)
self.change_qimage_signal.emit(qimg)
print("emitted")
def stop(self):
self.wait()
import sys
class Dialog(QDialog):
def __init__(self):
super(Dialog,self).__init__()
Dialog.resize(self,640,480)
button=QPushButton("Click")
button.clicked.connect(self.startThread)
mainLayout = QVBoxLayout()
mainLayout.addWidget(button)
self.setLayout(mainLayout)
self.setWindowTitle("QImage Example")
def startThread(self):
self.thread = VideoThread()
self.thread.change_qimage_signal.connect(self.getPixmap)
self.thread.start()
def getPixmap(self,qimg):
print("got qimage")
qpixmap = QPixmap.fromImage(qimg)
if __name__ == '__main__':
app = QApplication(sys.argv)
dialog = Dialog()
sys.exit(dialog.exec_())
如果高度和宽度设置为小数值(例如3),则程序不会崩溃。 如果我们在发射并更改信号之前将qimage转换为qpixmap,该程序也不会崩溃 输入到QPixmap。 最初编写该程序是为了使用opencv从网络摄像头获取图像。创建的numpy数组 对于大图像,opencv也会崩溃。
使用的操作系统是Windows10,pyqt版本是5.12.3
您知道崩溃的原因是什么吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)