K
K
Konstantin Malyarov2019-04-02 17:09:04
Python
Konstantin Malyarov, 2019-04-02 17:09:04

How to update label in Qt?

When the stream starts, the window is blocked and the label is not updated.

def start(self):
        print('Stream Strat')
        self.widget_stream.clear()
        response = requests.post('http://192.168.122.1:8080/sony/camera', json={"method": "startLiveview","params": [],"id": 1,"version": "1.0" })
        print(response.content)
        response = requests.get('http://192.168.122.1:8080/liveview/liveviewstream', stream=True)
        for line in response.iter_content(chunk_size=1024*1024):
            if line.find(b'\xff\xd8') != -1 and line.find(b'\xff\xd9') != -1:
                b_jpeg_s = line.find(b'\xff\xd8')
                b_jpeg_e = line.find(b'\xff\xd9')
                image_bmp = Image.open(io.BytesIO(line[b_jpeg_s:b_jpeg_e + 2]))
                image_bmp.save('image.bmp')
                pixmap = QPixmap('image.bmp')
                self.label_image.setPixmap(pixmap)

How to make the label update regularly. (A byte stream comes from the server, which is converted into an image, and then must be inserted into the label)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2019-04-02
@Konstantin18ko

I can't upload the image in your way

import io
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import requests


def log_uncaught_exceptions(ex_cls, ex, tb):
    text = '{}: {}:\n\n'.format(ex_cls.__name__, ex)
    import traceback
    text += ''.join(traceback.format_tb(tb))

    QMessageBox.critical(None, 'Ошибка!', text)
    quit()

sys.excepthook = log_uncaught_exceptions


class SenderMessage(QObject):
    image_bytes = pyqtSignal(bytes)

    def __init__(self):
        super().__init__()

    @pyqtSlot()
    def run(self):
        image_bytes = requests.get('https://dadaviz.ru/wp-content/uploads/2018/02/1-12.jpg', stream=True).content
        self.image_bytes.emit(image_bytes)

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        self.setWindowTitle("Показать изображение")
        self.setGeometry(400, 200, 626, 626)

        self.label_image = QLabel(self)
        self.label_image.setGeometry(0, 0, 626, 626)

        self.pixmap = QPixmap()

        self.thread = QThread()

        self.sender_message = SenderMessage()

        self.sender_message.moveToThread(self.thread)
        self.sender_message.image_bytes.connect(self.signalHandlerImageBytes)


        self.thread.started.connect(self.sender_message.run)
        self.thread.start()

    def signalHandlerImageBytes(self, image_bytes):
        self.pixmap.loadFromData(image_bytes)
        self.label_image.setPixmap(self.pixmap)

def main():
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

B
bbkmzzzz, 2019-04-02
@bbkmzzzz

You don't have time to process the event queue and redraw widgets. Second thread on QThread or QRunnable, send signals from it with new data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question