更改图像的大小以适应 GUI 窗口的新大小

changing image's size to fit the new size of the GUI window

本文关键字:GUI 窗口 新大小 图像      更新时间:2023-10-16

我已经阅读了Qt调整大小策略的文档,但我找不到如何实现改变图像大小以适应Qlabel新大小的场景。

这是我的GUI结构

mainLayout:
    toplayout:
        QLabel
        QPushButton

现在我有了cv::Mat图像。我已转换为QPixmap

Window::Window(QWidget *parent): QDialog(parent)
{
    // Widgets
    browserButton = new QPushButton(tr("Open"));
       imageLabel = new QLabel;
    imageLabel->setAlignment(Qt::AlignHCenter);
    imageLabel->setScaledContents(true);

    // Connect Actions
    connect(browserButton, SIGNAL(clicked()), this, SLOT(browserClicked()));
    // Layouts
    QVBoxLayout *topLayout = new QVBoxLayout;
    topLayout->addWidget(imageLabel);
    topLayout->addWidget(browserButton);
    // Main Layout
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(topLayout);
    setLayout(mainLayout);
    setWindowTitle("Image Browser");
}

这是按钮

void Window::browserClicked()
{
    // Get file name as QString 
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("Open Image"),
                                                    NULL,
                                                    tr("Image Files (*.png *.jpeg *.jpg *.bmp)"));
    // Read file name as String not QString and store the image           
    m_original_CVimg = cv::imread(fileName.toStdString());
    imageLabel->setPixmap(this->cvMatToQPixmap(m_original_CVimg));
}

我想在每次用户改变窗口大小时重新调整图像的大小。如果我扩大窗口,图像也会扩大,但问题是如果我缩小窗口,窗口的大小就会受到图像大小的限制。任何建议

看一下QResizeEvent