如何在 QScrollArea 中调整带有像素图的 QLabel 的大小?
How to resize a QLabel with pixmap inside a QScrollArea?
我有一个设计器表单(不带按钮模板的对话框),在垂直布局中包含 QScrollArea
和 2 个 QPushButton
对象。我想在 QScrollArea
.
中用像素图设置 QLabel
这是我的代码:
在Viewer
的构造函数中
m_imageLabel = new QLabel;
m_imageLabel->setPixmap(image);
m_imageLabel->setScaledContents(true);
ui->scrollArea->setBackgroundRole(QPalette::Dark);
ui->scrollArea->setWidget(m_imageLabel);
ui->scrollArea->setWidgetResizable(true);
插槽
void Viewer::on_zoomInButton_clicked()
{
m_imageLabel->resize(m_scaleFactor * m_imageLabel->pixmap()->size());
...
}
问题是单击 zoomInButton
时没有任何反应。
如何实现?
原因
通过使用 ui->scrollArea->setWidgetResizable(true);
,您允许滚动区域自动调整小部件的大小:
If this property is set to true, the scroll area will automatically resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space.
此外,您以错误的方式计算了 QLabel
的新大小,即您使用了其 pixmap
的大小,而后者又保持不变。
解决方案
为了达到想要的效果,建议您:
明确设置 widgetResizable
属性 为 false
:
ui->scrollArea->setWidgetResizable(false);
使 QLabel
的新大小取决于其旧大小,而不是其 pixmap
的大小:
m_imageLabel->resize(m_scaleFactor * m_imageLabel->size());
例子
这是我为您准备的一个最小示例,用于演示如何实施建议的解决方案:
#include <QApplication>
#include <QWidget>
#include <QBoxLayout>
#include <QPushButton>
#include <QScrollArea>
#include <QLabel>
struct Viewer : public QWidget
{
explicit Viewer(QWidget *parent = nullptr) :
QWidget(parent)
{
auto *l = new QVBoxLayout(this);
auto *scrollArea = new QScrollArea(this);
auto *btnZoomIn = new QPushButton(tr("Zoom In"), this);
auto *btnZoomOut = new QPushButton(tr("Zoom Out"), this);
auto *label = new QLabel();
qreal scaleFactor = 1.25;
label->setPixmap(QPixmap("qt-creator-logo.png"));
label->setScaledContents(true);
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(label);
scrollArea->setWidgetResizable(false);
l->addWidget(scrollArea);
l->addWidget(btnZoomIn);
l->addWidget(btnZoomOut);
connect(btnZoomIn, &QPushButton::clicked, [label, scaleFactor](){
label->resize(scaleFactor * label->size());
});
connect(btnZoomOut, &QPushButton::clicked, [label, scaleFactor](){
label->resize((2 - scaleFactor) * label->size());
});
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Viewer w;
w.show();
return a.exec();
}
注意:此代码要求 test image qt-creator-logo.png
在运行时位于构建文件夹中。
结果
如所写,此代码产生以下结果:
- 非缩放
- 放大
- 缩小
我有一个设计器表单(不带按钮模板的对话框),在垂直布局中包含 QScrollArea
和 2 个 QPushButton
对象。我想在 QScrollArea
.
QLabel
这是我的代码:
在Viewer
m_imageLabel = new QLabel;
m_imageLabel->setPixmap(image);
m_imageLabel->setScaledContents(true);
ui->scrollArea->setBackgroundRole(QPalette::Dark);
ui->scrollArea->setWidget(m_imageLabel);
ui->scrollArea->setWidgetResizable(true);
插槽
void Viewer::on_zoomInButton_clicked()
{
m_imageLabel->resize(m_scaleFactor * m_imageLabel->pixmap()->size());
...
}
问题是单击 zoomInButton
时没有任何反应。
如何实现?
原因
通过使用 ui->scrollArea->setWidgetResizable(true);
,您允许滚动区域自动调整小部件的大小:
If this property is set to true, the scroll area will automatically resize the widget in order to avoid scroll bars where they can be avoided, or to take advantage of extra space.
此外,您以错误的方式计算了 QLabel
的新大小,即您使用了其 pixmap
的大小,而后者又保持不变。
解决方案
为了达到想要的效果,建议您:
明确设置
widgetResizable
属性 为false
:ui->scrollArea->setWidgetResizable(false);
使
QLabel
的新大小取决于其旧大小,而不是其pixmap
的大小:m_imageLabel->resize(m_scaleFactor * m_imageLabel->size());
例子
这是我为您准备的一个最小示例,用于演示如何实施建议的解决方案:
#include <QApplication>
#include <QWidget>
#include <QBoxLayout>
#include <QPushButton>
#include <QScrollArea>
#include <QLabel>
struct Viewer : public QWidget
{
explicit Viewer(QWidget *parent = nullptr) :
QWidget(parent)
{
auto *l = new QVBoxLayout(this);
auto *scrollArea = new QScrollArea(this);
auto *btnZoomIn = new QPushButton(tr("Zoom In"), this);
auto *btnZoomOut = new QPushButton(tr("Zoom Out"), this);
auto *label = new QLabel();
qreal scaleFactor = 1.25;
label->setPixmap(QPixmap("qt-creator-logo.png"));
label->setScaledContents(true);
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setWidget(label);
scrollArea->setWidgetResizable(false);
l->addWidget(scrollArea);
l->addWidget(btnZoomIn);
l->addWidget(btnZoomOut);
connect(btnZoomIn, &QPushButton::clicked, [label, scaleFactor](){
label->resize(scaleFactor * label->size());
});
connect(btnZoomOut, &QPushButton::clicked, [label, scaleFactor](){
label->resize((2 - scaleFactor) * label->size());
});
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Viewer w;
w.show();
return a.exec();
}
注意:此代码要求 test image qt-creator-logo.png
在运行时位于构建文件夹中。
结果
如所写,此代码产生以下结果:
- 非缩放
- 放大
- 缩小