gpt4 book ai didi

c++ - Qt4中获取QVBoxLayout的一个布局单元格的大小

转载 作者:太空宇宙 更新时间:2023-11-04 12:20:02 25 4
gpt4 key购买 nike

我试图在 QVBoxLayout 中以适当的大小渲染图像,但我无法检索到正确的大小。该布局包含一个 QLabel,它在设计器 View 中以合适的尺寸显示(参见图片)。目标是以最大可用尺寸显示图像。

Layout

这是我获取尺寸的尝试(全部失败):

VideoResourceWidget::VideoResourceWidget(VideoResource* resource, QWidget *parent) :
QWidget(parent),
ui(new Ui::VideoResourceForm),
m_videoResource(resource)
{
ui->setupUi(this);

// INFO: -> size = (670,463) // this seems to be too small
m_videoSize = this->geometry().size();

// first attempt -> size = (0,0)
m_videoSize = this->geometry().size();
m_videoSize.setHeight(m_videoSize.height() - ui->controllerLayout->geometry().height());

// second attempt -> size = (100,30) way too small
m_videoSize = ui->videoLayout->itemAt(ui->videoLayout->indexOf(ui->frameLabel))->geometry().size();

ui->videoLayout->activate(); // hint from another question

// forth attempt -> size = (145,428) better but not still too small
m_videoSize = ui->videoLayout->itemAt(ui->videoLayout->indexOf(ui->frameLabel))->geometry().size();

// third attempt -> size = (670,434) there is still a lot more room
m_videoSize = this->geometry().size();
m_videoSize.setHeight(m_videoSize.height() - ui->controllerLayout->geometry().height());

ui->videoLayout->setSpacing(1);
ui->frameLabel->setMargin(0);

ui->videoLayout->activate(); // hint from another question

// fifth attempt -> size = (145,428) same as before
m_videoSize = ui->videoLayout->itemAt(ui->videoLayout->indexOf(ui->frameLabel))->geometry().size();

// sixth attempt -> size = (670,434) same as before
m_videoSize = this->geometry().size();
m_videoSize.setHeight(m_videoSize.height() - ui->controllerLayout->geometry().height());

QImage frame = m_videoResource->firstFrame();
ui->frameLabel->setPixmap(QPixmap::fromImage(frame).scaled(m_videoSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));

connect(ui->nextFrameButton, SIGNAL(clicked()), this, SLOT(nextFrame()));
}

这是第一次显示 VideoResourceWidget 后的 GUI。

Displaying the first frame

虽然最终结果不一定很漂亮,但我很乐意有效地利用可用空间。

更新:我更新了屏幕截图以反射(reflect)我最近的尝试。

更新:最小示例:

主窗口.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>771</width>
<height>580</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralWidget">
<layout class="QVBoxLayout" name="verticalLayout" stretch="1,0">
<item>
<layout class="QHBoxLayout" name="displayLayout" stretch="0,0,0">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="frameLabel">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="controllerLayout" stretch="1,0">
<item>
<widget class="QScrollBar" name="horizontalScrollBar">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="nextButton">
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>771</width>
<height>25</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

public slots:
void next();

private:
Ui::MainWindow *ui;

QSize m_imageSize;
QImage m_image;
};

#endif // MAINWINDOW_H

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->layout()->activate();

connect(ui->nextButton, SIGNAL(clicked()), this, SLOT(next()));

// Goal: display the image centered using the maximally available space
m_image = QImage("/tmp/lena.jpg");
m_imageSize = ui->frameLabel->size();
ui->frameLabel->setPixmap(QPixmap::fromImage(m_image).scaled(m_imageSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::next()
{
// just redraw
m_imageSize = ui->frameLabel->size();
ui->frameLabel->setPixmap(QPixmap::fromImage(m_image).scaled(m_imageSize, Qt::KeepAspectRatio, Qt::SmoothTransformation));
}

最佳答案

减小布局的边距和间距(尝试一下。我不确定哪个是什么)。这样,标签和您的图片都会获得更多空间。

我不确定您希望它看起来像什么,但第二个屏幕截图对我来说非常好。 (保持纵横比使其占据标签的高度)。它只是没有居中。

关于c++ - Qt4中获取QVBoxLayout的一个布局单元格的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5595532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com