简体   繁体   English

如何动态调整QLabel / QVBoxLayout / QWidget中使用的QImage的大小

[英]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. 我有一个派生类(从QWidget),它使用带有2个项目的QVBoxLayout,这两个都是QLabel。 The top QLabel is used to display a video stream and the bottom QLabel is use for a status line. 顶部QLabel用于显示视频流,底部QLabel用于状态行。 This is from one of the examples in the Qt documentation. 这来自Qt文档中的示例之一。

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: 当前在插槽中,我删除了QImage obj,然后创建一个具有新大小的新QImage obj。

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. 就像我说的那样,这很好,但是CaptureWin不会调整大小,仅调整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. 我考虑得越多,我认为这是一个非常糟糕的设计,因为引用了QImage的QVBoxLayout不知道它已经改变了。

So, what is the proper way to have the CaptureWin obj resize to accommodate the new QImage size? 那么,使CaptureWin obj调整大小以适应新的QImage大小的正确方法是什么?

Thanks, 谢谢,

-Andres -Andres

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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