C++ 获取Qt4中QVBoxLayout的布局单元的大小
我试图在C++ 获取Qt4中QVBoxLayout的布局单元的大小,c++,qt4,C++,Qt4,我试图在QVBoxLayout中以适当的大小渲染图像,但无法检索到正确的大小。布局包含一个QLabel,它在designer视图中以合适的大小显示(见图)。目标是以最大可用大小显示图像 以下是我获取大小的尝试(全部失败): 这是第一次显示VideoResourceWidget后的GUI 虽然最终的结果并不一定是漂亮的,但我希望有效地利用可用空间 更新:我更新了屏幕截图以反映我最近的尝试 更新:最小示例: mainwindow.ui <?xml version="1.0" encodin
QVBoxLayout
中以适当的大小渲染图像,但无法检索到正确的大小。布局包含一个QLabel
,它在designer视图中以合适的大小显示(见图)。目标是以最大可用大小显示图像
以下是我获取大小的尝试(全部失败):
这是第一次显示VideoResourceWidget
后的GUI
虽然最终的结果并不一定是漂亮的,但我希望有效地利用可用空间
更新:我更新了屏幕截图以反映我最近的尝试
更新:最小示例:
mainwindow.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>
减少版面的边距和间距(发挥作用。我不确定哪一个是什么)。这样标签就有了更多的空间,你的照片也一样 我不确定你期望它是什么样子,但是第二个屏幕截图对我来说非常好。(保持纵横比使其占据标签的高度)。它只是没有居中
- 减小版面的边距和间距(使用它。我不确定哪个是什么)。这样标签就有了更多的空间,你的照片也一样
我不确定你期望它是什么样子,但是第二个屏幕截图对我来说非常好。(保持纵横比使其占据标签的高度)。它只是没有居中
实际上,我不会使用QLabel来渲染视频,但我想这只是一个测试,我知道QLabel很容易用于显示像素贴图。我的选择是使用性能和重新实现QGLWidget,并通过OpenGL进行视频渲染。首先,我会尝试删除间隔。然后设置标签的sizeHint(正如其他人已经指出的) 如果确实需要放置间隔符,请将其stretchFactor设置为“0”,并将标签的stretchFactor设置为“1”
实际上,我不会使用QLabel来渲染视频,但我想这只是一个测试,我知道QLabel很容易用于显示像素贴图。我的选择是使用性能和重新实现QGLWidget,并通过OpenGL进行视频渲染。1)如果标签太小,请尝试设置sizeHint以进行扩展,并尝试在所有涉及的布局中设置所有边距和间距。一定在那里。2) 构造一个我可以自己运行的最小示例,然后我提供正确的答案(希望如此)。首先,感谢您的努力。我目前正忙于准备一些无关的东西,还没有时间创建一个最小的示例。如果标签太小,请尝试设置sizeHint以进行扩展,并尝试在所有涉及的布局中设置所有边距和间距。一定在那里。2) 构造一个我可以自己运行的最小示例,然后我提供正确的答案(希望如此)。首先,感谢您的努力。我目前正忙于准备一些无关的东西,还没有时间创建一个最小的示例。一旦我能再次集中精力,我会给你回电话的。
<?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>
#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
#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));
}