简体   繁体   中英

QPixmap resizing with Realsense image

I try thanks to a Qlabel to make it automatically resizable, it includes a QPixmap which displays a Realsense camera that works on a thread vid_thread . I have a timer that refreshes the image every 20 milliseconds, to avoid too heavy load. Except that the resizing is not fluid, it does not instantly follow the movement of the mouse as do the other elements of the interface.

I tried thanks to resize event not to make it dependent on this timer, and since it's more fluid, but still not instantaneous

Any smart hints how to accomplish this?

class UI_main(QMainWindow):
    def __init__(self):
        [...]
        self.timer = QTimer()
        self.timer.setInterval(20)
        self.timer.timeout.connect(self.setImage)
        self.timer.start()
        [...]
    def setImage(self):
        self.camLabel.setP(self.server_thread.vid_thread.p)
        [...]
    def setupUi(self):
        [...]
        self.camLabel = camLabel(self.detectionContainer)
        self.camLabel.setStyleSheet("border-color: rgb(112, 112, 112); border-width : 1px; border-radius:5px; border-style:inset;")
        self.hCamContainer.addWidget(self.camLabel)
        [...]

class camLabel(QLabel):
    mouseSignal = pyqtSignal(tuple)
    def __init__(self, parent=None):
        super(camLabel, self).__init__(parent)
        self.p = QPixmap()
        self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)
        self.setMinimumSize(640, 480)
        self.setScaledContents(True)

    def setP(self, p):
        self.p = QPixmap.fromImage(p)
        self.setPixmap(self.p.scaled(self.width(), self.height(),
                                     Qt.KeepAspectRatio, Qt.FastTransformation))

    def mousePressEvent(self, event):
        self.x=int(event.x()*(640/self.width()))
        self.y=int(event.y()*(480/self.height()))

        print("point x: ", self.x, ", point y: ", self.y)
        print("point x (ancien): ",event.x(), ", point y(ancien): ", event.y())
        print("Width : ", self.width(), ", Height: ", self.height())

        self.mouseSignal.emit((self.x,self.y))

    def resizeEvent(self, event):
        self.setPixmap(self.p.scaled(self.width(), self.height(),
                                     Qt.KeepAspectRatio,Qt.FastTransformation))

在此处输入图像描述

Probably, this solution would help. The key difference is that the image is updated on paintEvent, not on resizeEvent. The code is C++ not Python, but I am sure you get the idea.

#ifndef IMAGEWIDGET_H
#define IMAGEWIDGET_H

#include <QWidget>
#include <QPainter>
#include <QPaintEvent>

class ImageWidget : public QWidget
{
    Q_OBJECT
public:
    explicit ImageWidget(QWidget *parent = nullptr) : QWidget(parent) {}
    void setImage(const QString &path) {
        m_image.load(path);
    }

protected:
    void paintEvent(QPaintEvent *) override {
        QPainter painter(this);
        auto scaled = m_image.scaled(width(), height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
        painter.drawImage(0,0, scaled);
        painter.end();
    }

private:
    QImage m_image;
};

#endif // IMAGEWIDGET_H

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM