这里有几个问题。
第一个已经由您收到的错误信息解释;您不能在列中使用垂直锚,并且anchors.fill: parent
意味着水平锚和垂直锚。您可以使用width
和height
属性,而不是:
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
文档中解释说:
Generates a colorized and blurred shadow image of the source and places it behind the original, giving the impression that source item is raised from the background.
所以,你可以在label1
和label2
设置visible: false
。
下一个问题是DropShadow
将被限制为Label
的边界矩形。随着DropShadow
文档中的例子,这是没有问题的,因为内容的范围比实际项目的范围要小得多:
由于没有像素太大的距离构成文本和Label
的边界,你必须自己解释。
我怀疑,这是最完美的解决方案,但我不知道有更好的一个:
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
}
}
}
}
请注意,我还减少了阴影的半径,使它更明显。
因为它是“DropShadow”,所以要求使用“不优雅”和“丑陋”的解决方案来应用它。 IMO是不可避免的。竖起大拇指为好的答案! – BaCaRoZzo