我编写了一个 QML 应用程序,其中显示了来自图像提供商的 QImages。我写了一个继承自 QQuickImageProvider 的类。在 QML 中,我有两个图像对象。
ColumnLayout {
RowLayout {
Image {
source: "image://backend/1"
width: parent.width/2
}
Image {
source: "image://backend/1"
width: parent.width/2
}
这是提供者的代码:
QImage qmlProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
QSize req(requestedSize.width()<0?100:requestedSize.width(),
requestedSize.height()<0?100:requestedSize.height());
try {
QImage ret=assign.at(id)->scaled(req,Qt::KeepAspectRatio);
*size=ret.size();
return ret;
}
catch( out_of_range ) {
QImage ret(req,QImage::Format_RGB16);
*size=ret.size();
ret.fill(QColor(qrand()/(RAND_MAX/255),qrand()/(RAND_MAX/255),qrand()/(RAND_MAX/255)).rgba());
return ret;
}
}
它到目前为止有效,这意味着显示了图片。 现在我想要缩放这些图片。我希望每张图片都填满我的应用程序窗口的一半,但它不起作用。我设置的每个宽度都会被忽略,图像提供者每次都会收到无效的请求尺寸。
我需要做什么才能实现这一点?
最佳答案
由于您的项目大小由 Layout
管理,因此您应该设置 Layout 的首选大小而不是项目的大小:
RowLayout {
anchors.fill: parent
Image {
source: "image://backend/1"
Layout.preferredHeight: parent.height
Layout.preferredWidth: parent.width / 2
}
Image {
source: "image://backend/1"
Layout.preferredHeight: parent.height
Layout.preferredWidth: parent.width / 2
}
}
可以找到更多信息 here
关于c++ - QML 和 QQuickImageProvider 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46174902/