Qt Can';在列中不使用DropShadow?
我正在使用QML开发一个小应用程序,我需要在文本组件上添加一个小阴影,以使其更具可读性,但当我将阴影添加到列中的标签时,它会抛出一个错误: QML列:无法为列中的项目指定顶部、底部、垂直中心、填充或中心定位。列将不起作用 但是文本应该相互重叠,下面是我的示例代码:Qt Can';在列中不使用DropShadow?,qt,qml,qtquick2,Qt,Qml,Qtquick2,我正在使用QML开发一个小应用程序,我需要在文本组件上添加一个小阴影,以使其更具可读性,但当我将阴影添加到列中的标签时,它会抛出一个错误: QML列:无法为列中的项目指定顶部、底部、垂直中心、填充或中心定位。列将不起作用 但是文本应该相互重叠,下面是我的示例代码: import QtQuick 2.2 import QtGraphicalEffects 1.0 Item { width: 100 height: 200 Column { Label {
import QtQuick 2.2
import QtGraphicalEffects 1.0
Item {
width: 100
height: 200
Column {
Label {
id: label1
anchors.horizontalCenter: parent.horizontalCenter
text: "First label"
}
DropShadow {
anchors.fill: label1
radius: 16
samples: 32
color: "#b0000000"
source: label1
}
Label {
id: label2
anchors.horizontalCenter: parent.horizontalCenter
text: "Second label"
}
DropShadow {
anchors.fill: label2
radius: 16
samples: 32
color: "#b0000000"
source: label2
}
}
}
我犯了一些错误吗?或者我不能在列中使用DropShadow吗?
我应该使用项目而不是列吗?
提前谢谢你 按照现在的方式,放置阴影标签将占据其自己的列,因此不能使用anchors.fill关键字 尝试将DropShadow放置在标签内:
Label {
id: label1
anchors.horizontalCenter: parent.horizontalCenter
text: "First label"
DropShadow {
anchors.fill: label1
horizontalOffset: 1
verticalOffset: 1
radius: 1
samples: 3
color: "Red"
source: label1
}
}
更好的是,将所有这些代码放在一个单独的文件中:DropShadowText.qml,并将其作为一个唯一的组件使用:
DropShadowText{
}
祝你好运 这里有几个问题 第一个问题已由您收到的错误消息解释;不能在列中使用垂直定位,并且
定位。fill:parent
同时表示水平定位和垂直定位。您可以改用宽度
和高度
属性:
import QtQuick 2.2
import QtGraphicalEffects 1.0
import QtQuick.Controls 1.0
import QtQuick.Window 2.0
Window {
width: 100
height: 200
visible: true
Column {
Label {
id: label1
anchors.horizontalCenter: parent.horizontalCenter
text: "First label"
}
DropShadow {
width: label1.width
height: label1.height
radius: 16
samples: 32
color: "#b0000000"
source: label1
}
Label {
id: label2
anchors.horizontalCenter: parent.horizontalCenter
text: "Second label"
}
DropShadow {
width: label2.width
height: label2.height
radius: 16
samples: 32
color: "#b0000000"
source: label2
}
}
}
然而,这带来了新的问题:
您可以看到标签有重复项。这在DropShadow
的文档中有说明:
生成源的彩色模糊阴影图像,并将其放置在原始图像的后面,给人以源项从背景升起的印象
因此,您可以在label1
和label2
上设置visible:false
下一个问题是DropShadow
将被限制在标签的边框内。对于DropShadow
文档中的示例,这不是问题,因为内容的边界比实际项目的边界小得多:
由于构成文本的像素与标签
的边界之间没有太大的距离,因此您必须自己进行解释
我怀疑这是最优雅的解决方案,但我不知道还有更好的解决方案:
import QtQuick 2.4
import QtQuick.Window 2.0
import QtQuick.Controls 1.0
import QtGraphicalEffects 1.0
Window {
id: win
width: 150
height: 150
visible: true
Item {
width: 100
height: 200
Column {
Item {
width: label1.implicitWidth + 20
height: label1.implicitHeight + 20
anchors.horizontalCenter: parent.horizontalCenter
visible: false
Label {
id: label1
text: "First label"
anchors.centerIn: parent
}
}
DropShadow {
width: label1.width
height: label1.height
radius: 4
samples: 8
color: "#b0000000"
source: label1
}
Item {
width: label2.implicitWidth + 20
height: label2.implicitHeight + 20
anchors.horizontalCenter: parent.horizontalCenter
visible: false
Label {
id: label2
text: "Second label"
anchors.centerIn: parent
}
}
DropShadow {
width: label2.width
height: label2.height
radius: 4
samples: 8
color: "#b0000000"
source: label2
}
}
}
}
请注意,我还减小了阴影的半径以使其更明显。它很难看,但与您提供的设置配合使用-播放或删除“半径”和“采样”设置和颜色…并尝试添加horizontalOffset:3;verticalOffset:3到DropShadow我会用一个更清晰的例子来编辑我的答案。你真的尝试过你发布的代码吗ShaderEffectSource:递归渲染时,必须将“recursive”设置为true。
事实上,DropShadow调用“不优雅”到“丑陋”的解决方案来应用它。在我看来,这是不可避免的。对这个好答案竖起大拇指!