I'm new to Qt, and this issue about auto resizing has driven me crazy.
我是Qt的新手,这个关于自动调整大小的问题让我抓狂。
I create a class called RenderArea
that inherits QWidget
. In its paintEvent()
, I use a QPainter
to draw an image. In order for the whole window to scale with the image, I resize before painting. The relevant code is
我创建了一个名为RenderArea的类,它继承了QWidget。在它的paintEvent()中,我使用QPainter绘制图像。为了使整个窗口与图像缩放,我在绘制之前调整大小。相关的代码
if (image && !image->isNull())
{
resize(image->size());
painter.drawImage(image->rect(), *image, image->rect());
}
However, RenderArea
will stretch too much through other widgets (like buttons and menus). It is contained in a centralWidget
with a vertical layout. But when I call centralWidget->adjustSize()
it does not scale everything together, but instead shrinks RenderArea
t and hides the image.
然而,RenderArea会在其他窗口小部件(如按钮和菜单)中扩展太多。它包含在一个具有垂直布局的centralWidget中。但是,当我调用centralWidget->调整大小()时,它不会将所有东西都缩放到一起,而是缩小RenderArea t并隐藏图像。
How do I instruct the central widget as well as the window to scale with the new size of my customized widget? I know I could use a QLabel
and set its scaledContents
to be true, but I need a lot of other sophisticated rendering so a simple QLabel
is not enough.
如何指示中心小部件以及窗口随自定义小部件的新大小进行伸缩?我知道我可以使用QLabel并将它的scaledContents设置为true,但是我需要很多其他复杂的渲染,因此仅使用一个简单的QLabel是不够的。
1 个解决方案
#1
3
The sizeHint
function should return recommended widget's size. So RenderArea
should return image size as its sizeHint
. When the image is changed the updateGeometry
function should be called to update a cached sizeHint
value:
sizeHint函数应该会返回推荐的小部件的大小。因此RenderArea应该返回图像大小作为它的sizeHint。当映像被更改时,应该调用updateGeometry函数来更新缓存的sizeHint值:
class RenderArea : public QWidget
{
public:
explicit RenderArea(const QImage &image, QWidget *parent = 0)
: QWidget(parent), m_image(image)
{
}
QSize sizeHint() const
{
return m_image.isNull() ? QWidget::sizeHint() : m_image.size();
}
void paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
if (!m_image.isNull())
{
QPainter painter(this);
painter.drawImage(rect(), m_image, m_image.rect());
}
}
void setImage(const QImage &image)
{
m_image = image;
updateGeometry();
}
private:
QImage m_image;
};
When the child widget is resized, the parent widget isn't doing it automatically. Fortunately there is QWidget::adjustSize
function which lets us to resize the parent widget to fit its content:
当子小部件被调整大小时,父小部件不会自动调整大小。幸运的是有QWidget::调整大小函数,它允许我们调整父小部件的大小以适应其内容:
class Window : public QWidget
{
Q_OBJECT
private slots:
void onImageChanged(const QString &fileName)
{
m_area->setImage(QImage(fileName));
adjustSize();
}
private:
RenderArea *m_area;
};
#1
3
The sizeHint
function should return recommended widget's size. So RenderArea
should return image size as its sizeHint
. When the image is changed the updateGeometry
function should be called to update a cached sizeHint
value:
sizeHint函数应该会返回推荐的小部件的大小。因此RenderArea应该返回图像大小作为它的sizeHint。当映像被更改时,应该调用updateGeometry函数来更新缓存的sizeHint值:
class RenderArea : public QWidget
{
public:
explicit RenderArea(const QImage &image, QWidget *parent = 0)
: QWidget(parent), m_image(image)
{
}
QSize sizeHint() const
{
return m_image.isNull() ? QWidget::sizeHint() : m_image.size();
}
void paintEvent(QPaintEvent *event)
{
QWidget::paintEvent(event);
if (!m_image.isNull())
{
QPainter painter(this);
painter.drawImage(rect(), m_image, m_image.rect());
}
}
void setImage(const QImage &image)
{
m_image = image;
updateGeometry();
}
private:
QImage m_image;
};
When the child widget is resized, the parent widget isn't doing it automatically. Fortunately there is QWidget::adjustSize
function which lets us to resize the parent widget to fit its content:
当子小部件被调整大小时,父小部件不会自动调整大小。幸运的是有QWidget::调整大小函数,它允许我们调整父小部件的大小以适应其内容:
class Window : public QWidget
{
Q_OBJECT
private slots:
void onImageChanged(const QString &fileName)
{
m_area->setImage(QImage(fileName));
adjustSize();
}
private:
RenderArea *m_area;
};