- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我得到了Qt
类,它是QQuickImageProvider
的子类,这里是requestPixmap
函数实现:
QPixmap MyImageProvider::requestPixmap(const QString &id, QSize *size, const QSize
&requestedSize){
int width = 100;
int height = 50;
if (size)
*size = QSize(width, height);
QPixmap pixmap(requestedSize.width() > 0 ? requestedSize.width() : width,
requestedSize.height() > 0 ? requestedSize.height() : height);
pixmap.fill(QColor(id).rgba());
return pixmap;
}
在 qml
我有 GridLayout
哪些项目来自图像提供者。通过单击按钮,我可以显示 1、2 或 4 个项目。这是 qml
文件:
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1
import QtQuick.Controls.Styles 1.1
import QtQuick.Window 2.2
Window{
id: root
title: "settings"
modality: Qt.ApplicationModal
flags: Qt.Dialog
minimumHeight: 700
minimumWidth: 700
maximumHeight: 700
maximumWidth: 700
ColumnLayout{
id: columnLayout
anchors.fill: parent
RowLayout{
ExclusiveGroup{id: exgroup}
Button{
text: "1x1"
checkable: true
checked: true
Layout.minimumWidth: 100
Layout.minimumHeight: 100
exclusiveGroup: exgroup
onCheckedChanged: {
if(checked===true){
grid.columns=1
grid.rows=1
im1.visible = true
im2.visible = false
im3.visible = false
im4.visible = false
im1.source = "image://plotPixmap/yellow"
}
}
}
Button{
text: "1x2"
checkable: true
checked: false
Layout.minimumWidth: 100
Layout.minimumHeight: 100
exclusiveGroup: exgroup
onCheckedChanged: {
if(checked===true){
grid.columns=1
grid.rows=2
im1.visible = true
im2.visible = true
im3.visible = false
im4.visible = false
im1.source = "image://plotPixmap/red"
im2.source = "image://plotPixmap/black"
}
}
}
Button{
text: "2x1"
checkable: true
checked: false
Layout.minimumWidth: 100
Layout.minimumHeight: 100
exclusiveGroup: exgroup
onCheckedChanged: {
if(checked===true){
grid.columns=2
grid.rows=1
im1.visible = true
im2.visible = true
im3.visible = false
im4.visible = false
im1.source = "image://plotPixmap/blue"
im2.source = "image://plotPixmap/green"
}
}
}
Button{
text: "2x2"
checkable: true
checked: false
Layout.minimumWidth: 100
Layout.minimumHeight: 100
exclusiveGroup: exgroup
onCheckedChanged: {
if(checked===true){
grid.columns=2
grid.rows=2
im1.visible = true
im2.visible = true
im3.visible = true
im4.visible = true
im1.source = "image://plotPixmap/blue"
im2.source = "image://plotPixmap/green"
im3.source = "image://plotPixmap/black"
im4.source = "image://plotPixmap/red"
}
}
}
}
GridLayout {
id: grid
Layout.fillHeight: true
Layout.fillWidth: true
Image{
id: im1
Layout.fillHeight: true
Layout.fillWidth: true
sourceSize.height: height
sourceSize.width: width
Layout.rowSpan: 1
Layout.columnSpan: 1
}
Image{
id: im2
Layout.fillHeight: true
Layout.fillWidth: true
sourceSize.height: height
sourceSize.width: width
Layout.rowSpan: 1
Layout.columnSpan: 1
}
Image{
id: im3
Layout.fillHeight: true
Layout.fillWidth: true
sourceSize.height: height
sourceSize.width: width
Layout.rowSpan: 1
Layout.columnSpan: 1
}
Image{
id: im4
Layout.fillHeight: true
Layout.fillWidth: true
sourceSize.height: height
sourceSize.width: width
Layout.rowSpan: 1
Layout.columnSpan: 1
}
}
}
}
在 C++ 中主要:
engine->addImageProvider(QLatin1String("plotPixmap"), new MyImageProvider());
一切正常,但是当我多次按下按钮时,底部图像的尺寸变得越来越小。如何固定图像大小?我希望所有图像的大小都相同,并且它们应该填满按钮下方的所有空间。
最佳答案
这是不同的 fillHeight
/fillwidth
集之间相互作用的结果。如文档中所述:
The fillWidth and fillHeight properties can either be true or false. If it is false, the item's size will be fixed to its preferred size. Otherwise, it will grow or shrink between its minimum and maximum size as the layout is resized.
在这种情况下,没有为四张图片设置最小宽度。因此,随着 GridLayout
结构的变化,根据按下的按钮,重新计算约束并在某些模式下(例如 2x1 -> 1x1 -> 2x1
)重新计算宽度/高度为第一张图片提供更多空间(根据 flow
)。要解决此问题,您应确保为每个图像设置最小宽度/高度,即 Layout.minimumWidth
和 Layout.minimumHeight
附加属性具有正确的值。
在代码中直接设置此类值会导致绑定(bind)循环。再次来自文档:
Note: It is not recommended to have bindings to the x, y, width, or height properties of items in a layout, since this would conflict with the goal of the Layout, and also cause binding loops.
为避免此问题,GridLayout
嵌入到 Item
中,该 Item
填充 ColumnLayout
代替 GridLayout
本身。然后将尺寸约束安全地应用于图像。这是最终代码:
import QtQuick 2.3
import QtQuick.Controls 1.2
import QtQuick.Layouts 1.1
import QtQuick.Window 2.2
Window{
id: root
title: "settings"
modality: Qt.ApplicationModal
flags: Qt.Dialog
minimumHeight: 700
minimumWidth: 700
maximumHeight: 700
maximumWidth: 700
visible: true
ColumnLayout{
id: columnLayout
anchors.fill: parent
RowLayout{
ExclusiveGroup{id: exgroup}
Button{
text: "1x1"
checkable: true
checked: true
Layout.minimumWidth: 100
Layout.minimumHeight: 100
exclusiveGroup: exgroup
onCheckedChanged: {
if(checked){
grid.columns = grid.rows = 1
im1.visible = true
im2.visible = im3.visible = im4.visible = false
im1.source = "image://plotPixmap/yellow"
}
}
}
Button{
text: "1x2"
checkable: true
checked: false
Layout.minimumWidth: 100
Layout.minimumHeight: 100
exclusiveGroup: exgroup
onCheckedChanged: {
if(checked){
grid.columns = 1
grid.rows = 2
im1.visible = im2.visible = true
im3.visible = im4.visible = false
im1.source = "image://plotPixmap/red"
im2.source = "image://plotPixmap/black"
}
}
}
Button{
text: "2x1"
checkable: true
checked: false
Layout.minimumWidth: 100
Layout.minimumHeight: 100
exclusiveGroup: exgroup
onCheckedChanged: {
if(checked){
grid.columns = 2
grid.rows = 1
im1.visible = im2.visible = true
im3.visible = im4.visible = false
im1.source = "image://plotPixmap/blue"
im2.source = "image://plotPixmap/green"
}
}
}
Button{
text: "2x2"
checkable: true
checked: false
Layout.minimumWidth: 100
Layout.minimumHeight: 100
exclusiveGroup: exgroup
onCheckedChanged: {
if(checked){
grid.columns = grid.rows = 2
im1.visible = im2.visible = im3.visible = im4.visible = true
im1.source = "image://plotPixmap/blue"
im2.source = "image://plotPixmap/green"
im3.source = "image://plotPixmap/black"
im4.source = "image://plotPixmap/red"
}
}
}
}
Item { // layout ensure to fill the available space
Layout.fillHeight: true
Layout.fillWidth: true
GridLayout {
id: grid
anchors.fill: parent // anchor to the available space
Image{
id: im1
Layout.fillHeight: true
Layout.fillWidth: true
Layout.minimumWidth: grid.width / 2 // constraint to the min width
Layout.minimumHeight: grid.height / 2 // constraint to the min height
Layout.rowSpan: 1
Layout.columnSpan: 1
}
Image{
id: im2
Layout.fillHeight: true
Layout.fillWidth: true
Layout.minimumWidth: grid.width / 2 // constraint to the min width
Layout.minimumHeight: grid.height / 2 // constraint to the min height
Layout.rowSpan: 1
Layout.columnSpan: 1
}
Image{
id: im3
Layout.fillHeight: true
Layout.fillWidth: true
Layout.minimumWidth: grid.width / 2 // constraint to the min width
Layout.minimumHeight: grid.height / 2 // constraint to the min height
Layout.rowSpan: 1
Layout.columnSpan: 1
}
Image{
id: im4
Layout.fillHeight: true
Layout.fillWidth: true
Layout.minimumWidth: grid.width / 2 // constraint to the min width
Layout.minimumHeight: grid.height / 2 // constraint to the min height
Layout.rowSpan: 1
Layout.columnSpan: 1
}
}
}
}
}
关于qml - GridLayout 项目大小彼此不相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27360083/
我收到错误 "Caused by: java.lang.ClassCastException: androidx.gridlayout.widget.GridLayout cannot be cast
我收到错误 "Caused by: java.lang.ClassCastException: androidx.gridlayout.widget.GridLayout cannot be cast
我正在使用 gridlayout 对我的 View 进行分组。在里面,我有两列。一列有一个子项 relativelayout,另一列有一个子项 linearlayout。 我的linearlayout
我正在开发一个应用程序,我需要像提供的图像这样的布局。 我应该使用哪种布局?为什么? GridView 和有什么区别和 GridLayout ?为什么他们在遗留部分?什么是替代方案和最佳实践? 以下是
我正在尝试在我的 Android 2.2 项目中使用 GridLayout,在我的工作区中安装 gridlayout_v7 项目后,将其添加到我的项目 Android 依赖项中,为其自定义属性添加自定
我是 Android 开发的新手,所以这可能是一个简单的问题。如果是的话,我深表歉意 - 我花了很多时间进行搜索,但找不到任何有用的东西。 我正在尝试在我的项目中使用 GridLayout,但最好使用
我将 GridLayout 作为 subview ,并设置了(空)GridLayout.LayoutParams setLayoutParams(new GridLayout.LayoutParams
仅当应用程序运行某些设备时我才会收到 StackOverflowError 错误,它在其他设备上运行良好。 这是堆栈跟踪: java.lang.StackOverflowError at androi
我有一个使用 GridLayoutManager 作为布局管理器的 recyclerview,我想要实现的是如下图所示 如您所见,每一行可能仅根据项目宽度有多个项目,每个项目都是一个具有 wrap_c
我得到了Qt类,它是QQuickImageProvider的子类,这里是requestPixmap函数实现: QPixmap MyImageProvider::requestPixmap(const
我尝试将单元格添加到我的 GridLayout通过使用 Repeater .我的数据存储在一个模型中,每个元素包含两个属性: Title Value 我的目标是获得一个 GridLayout包含 Ti
以下是我的 main.qml : Window { id: window visible: true width: 800 height: 480 title:
在我的应用程序中,我希望用动态生成的按钮填充 JPanel。我为此使用了 GridBagLayout。 1.) 当我将布局管理器设置为仅用多列填充一行时,即使只有一个按钮,它也会填充整个区域,这很好。
首先,我是编程新手,这是我在 java 和一般编程方面的第一个主要作业,所以如果我做了一些非常愚蠢的事情,请告诉我,以便我可以改正这个坏习惯。 不管怎样,我目前正在尝试创建一个具有可变行数的 grid
我正在尝试用 Java 编写游戏代码,目前正在制作标题屏幕。 我想在中间放置三个按钮,并将它们放置在彼此下方。这些按钮是“播放”、“选项”和“退出”。 我使用 GridLayout 订购按钮,现在我希
帮助了解问题可能是什么。我编写了这段代码,它有操作的描述: int countPosition = 0; for (int i = 0; i params = new ArrayLi
我的布局代码及其图形表示如下所示: 它看起来是这样的: 当我尝试在网格布局中放入按钮时,它不显示。我尝试将行数和列数设置为 2,但似乎不起作用。 我想让布局看起来像这样: 这就是我的 xml 文件的样
我有一个带有 GridLayout(1, 3) 的面板,但我想集中此布局中单元格的内容(不添加面板): public MainMenu() { setLayout(new GridLa
我正在制作一个程序,它会询问您的姓名和年龄,根据您的回答,它会输出一个文本短语和适当的图像,但我的问题是我无法控制网格布局中组件的大小,因此图片顶部被切掉了,所以我只能看到一小部分 public cl
我需要为 GridLayout 制作固定大小,并在 BorderLayout 的中心部分包含 100 个按钮。边框布局的东边部分是另一个 Gridlayout,只要文本长于位于东边的当前 JTex
我是一名优秀的程序员,十分优秀!