简体   繁体   中英

How to dynamically resize a QImage used in a QLabel/QVBoxLayout/QWidget

I have a derived class (from QWidget) that uses a QVBoxLayout with 2 items, both of which are a QLabel. The top QLabel is used to display a video stream and the bottom QLabel is use for a status line. This is from one of the examples in the Qt documentation.

CaptureWin::CaptureWin()
    {
        QVBoxLayout *vbox = new QVBoxLayout(this);
        vbox->setContentsMargins(QMargins(8, 8, 8, 5));

        m_pLabel = new QLabel();
        m_pMessage = new QLabel("No frame");

        vbox->addWidget(m_pLabel);
        vbox->addWidget(m_pMessage);
    }

    void CaptureWin::setImage(const QImage &image, const QString &status)
    {
        m_pLabel->setPixmap(QPixmap::fromImage(image));
        m_pMessage->setText(status);
    }

This is working just fine, my program captures the video from a shared memory segment (generated from a different process) and the video is displayed in this window.

However, the video image size can change, so I am trying to extend this to change to different size videos dynamically. My shared memory header gives the information about the image like sizes. So I can emit signals when the size changes.

Currently in the slot I delete the QImage obj, then create a new QImage obj with the new size.Like this:

void
    Dialog::updatePictureSize()
    {
        delete m_pCaptureImage;
        m_pCaptureImage = new QImage(m_nPictureWidth, m_nPictureHeight, QImage::Format_RGB32);

        m_pCaptureWin->repaint();
        m_pCaptureWin->show();
    }

As I said this works fine, however the CaptureWin does not resize, only the QImage. So when I go from a large video size to a small video size the basic window does not change I am left with a large white window with a small image inside.

The more I think about this, I think this is very poor design because the QVBoxLayout, which has a reference to the QImage, does not know it has changed.

So, what is the proper way to have the CaptureWin obj resize to accommodate the new QImage size?

Thanks,

-Andres

如果窗口应始终与图像一样大,并且用户不能调整其大小,则可以使用vbox->setSizeConstraint(QLayout::SetFixedSize)来完成此工作。

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