[英]Trying to send image from QThread to main or other thread in PyQt
我試圖顯示來自主線程中另一個線程甚至可能是新線程的圖像。 問題是我無法將圖像顯示在其他任何地方。 我正在使用線程_thread
下載圖像,因此它不會凍結gui,但是我似乎無法使用QImage或QPixmap正確地將其取出。
我知道如何在信號中使用字符串等,如下面的示例所示,但是我不確定如何在信號中正確發送圖像。 我嘗試使用請求來獲取圖像URL並將其發送到可以正常工作的主機,但是我無法在主機中獲取字符串,然后只能以最快的速度將其下載到主機中。 由於暫時凍結GUI,這也不是一個好選擇。
這段代碼是一個示例,顯示了我可以做的所有工作,如果願意的話,可以從主目錄下載圖像。
import urllib2
from PyQt4 import QtGui
from PyQt4.QtCore import QThread, pyqtSignal
from PyQt4.QtGui import QApplication, QPixmap
import sys
class MainWindow(QtGui.QWidget):
def __init__(self):
super(self.__class__, self).__init__()
layout = QtGui.QVBoxLayout(self)
self._Button = QtGui.QPushButton()
self._Button.setObjectName("_Button")
self.line_edit = QtGui.QLineEdit()
self.line_edit.setObjectName("line_edit")
self.txt_box = QtGui.QTextEdit()
self.txt_box.setObjectName("txt_Box")
self.img_label = QtGui.QLabel()
self.img_label.setObjectName("img_label")
layout.addWidget(self._Button)
layout.addWidget(self.line_edit)
layout.addWidget(self.img_label)
layout.addWidget(self.txt_box)
self.pixmap = QPixmap()
self._thread = test_thread()
self._Button.clicked.connect(self.start_thread)
self.get_Text = self._thread.test_Signal.connect(self.line_edit.setText)
self.urls = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png'
data = urllib2.urlopen(self.urls).read()
self.pixmap.loadFromData(data)
self.img_label.setPixmap(self.pixmap)
def start_thread(self):
self._thread.start()
self._thread.test_Signal.connect(self.txt_box.setText)
class test_thread(QThread):
test_Signal = pyqtSignal(str)
def __init__(self):
super(test_thread, self).__init__()
def run(self):
string = 'This is a test string.'
self.test_Signal.emit(string)
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
這段代碼顯示了我想要做的更多事情。
import urllib2
from PyQt4 import QtGui
from PyQt4.QtCore import QThread, pyqtSignal
from PyQt4.QtGui import QApplication, QPixmap
import sys
class MainWindow(QtGui.QWidget):
def __init__(self):
super(self.__class__, self).__init__()
layout = QtGui.QVBoxLayout(self)
self._Button = QtGui.QPushButton()
self._Button.setObjectName("_Button")
self.line_edit = QtGui.QLineEdit()
self.line_edit.setObjectName("line_edit")
self.txt_box = QtGui.QTextEdit()
self.txt_box.setObjectName("txt_Box")
self.img_label = QtGui.QLabel()
self.img_label.setObjectName("img_label")
layout.addWidget(self._Button)
layout.addWidget(self.line_edit)
layout.addWidget(self.img_label)
layout.addWidget(self.txt_box)
self.pixmap = QPixmap()
self._thread = test_thread()
self._Button.clicked.connect(self.start_thread)
self.get_Text = self._thread.test_Signal.connect(self.line_edit.setText)
"...Send signal here and show image. EX:"
"...self._thread.image_Signal.connect(self.img_label.setPixmap)...."
"...or...."
def show_image_here(self):
"....Send image here after downloaded in thread...."
def start_thread(self):
self._thread.start()
self._thread.test_Signal.connect(self.txt_box.setText)
class test_thread(QThread):
test_Signal = pyqtSignal(str)
"....Example image signal...."
"....image_Signal = pyqtSignal()...."
def __init__(self):
super(test_thread, self).__init__()
self.pixmap = QPixmap()
def run(self):
string = 'This is a test string.'
self.test_Signal.emit(string)
self.urls = 'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png'
data = urllib2.urlopen(self.urls).read()
self.pixmap.loadFromData(data)
"....Not sure what to do here...."
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
有人可以幫我嗎? 謝謝。
環顧四周之后,我遇到了該線程PySide線程和http下載
我希望早點找到它,因為我在發帖之前做了很多搜索。 回答線程reclosedev
也在評論中發布了此內容:
https://pastebin.com/b4MD5jKh
這極大地幫助我了解了一些東西。 我使用了他的代碼,並對其進行了些微改動,使其適合我的情況,它將非常適合我想做的事情。
這是我使用完整示例的完整工作代碼,適用於可能會發現有用的任何人。
import urllib2
from PyQt4 import QtGui, QtCore
from PyQt4.QtCore import QThread, pyqtSignal
from PyQt4.QtGui import QApplication
import sys
class MainWindow(QtGui.QWidget):
def __init__(self):
super(self.__class__, self).__init__()
layout = QtGui.QVBoxLayout(self)
self._Button = QtGui.QPushButton()
self._Button.setObjectName("_Button")
self.line_edit = QtGui.QLineEdit()
self.line_edit.setObjectName("line_edit")
self.txt_box = QtGui.QTextEdit()
self.txt_box.setObjectName("txt_Box")
self.img_label = QtGui.QLabel()
self.img_label.setObjectName("img_label")
self.imagepreview = ImagePreview()
layout.addWidget(self._Button)
layout.addWidget(self.line_edit)
layout.addWidget(self.img_label)
layout.addWidget(self.txt_box)
layout.addWidget(self.imagepreview)
self._thread = test_thread()
self._Button.clicked.connect(self.start_thread)
self._Button.clicked.connect(self.imagepreview.startDownload)
self.get_Text = self._thread.test_Signal.connect(self.line_edit.setText)
def start_thread(self):
self._thread.start()
self._thread.test_Signal.connect(self.txt_box.setText)
class test_thread(QThread):
test_Signal = pyqtSignal(str)
def __init__(self):
super(test_thread, self).__init__()
def run(self):
string = 'This is a test string.'
self.test_Signal.emit(string)
class DownloadThread(QtCore.QThread):
data_downloaded = pyqtSignal()
def __init__(self, url):
QtCore.QThread.__init__(self)
self.url = url
self._data = None
def run(self):
self._data = urllib2.urlopen(self.url).read()
self.data_downloaded.emit()
def get_data(self):
return self._data
class ImagePreview(QtGui.QWidget):
def __init__(self, parent=None):
super(ImagePreview, self).__init__(parent)
self.setMinimumSize(50, 50)
self.pixmap = None
def paintEvent(self, paintEvent):
painter = QtGui.QPainter(self)
if (self.pixmap):
painter.drawPixmap(0, 0, self.pixmap)
def startDownload(self):
self.download_thread = DownloadThread('https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png')
self.download_thread.start()
self.download_thread.data_downloaded.connect(self.ondownloadFinished)
def ondownloadFinished(self):
self.paintImage()
print("download finished")
def paintImage(self):
print("Painting")
pixmap = QtGui.QPixmap()
pixmap.loadFromData(self.download_thread.get_data())
self.setPixmap(pixmap)
def setPixmap(self, pixmap):
self.pixmap = pixmap
self.setMinimumSize(pixmap.width(), pixmap.height())
self.update()
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.