首页 > 解决方案 > 无法在 Component.onCompleted 之外动态更改 KDE Plasma 5 Widget Expandable FullRepresentation 组件大小

问题描述

我正在编写一个小部件,可以通过单击将其扩展为 FullRepresentation:

                onClicked: {
                    plasmoid.expanded = !plasmoid.expanded 
                }

我有一个更新其内容并将它们放入 Gridview 的函数。update 函数首先从 Component.onCompleted 中调用,但之后我每次用户更新数据时都会调用它。问题是我输入可扩展 FullRepresentation 的数据可能包含不同数量的元素,这些元素我放入 gridview 并根据此数字计算其 AND FullRepresentation 的大小。

但是,我发现我只能从 Component.onCompleted 块中指定表示的大小。当我在它之外调用更新函数时,它不会改变大小,无论大小是否在项目属性中定义,如下所示:

Item 
{
    id: fullRepresentation
    width: no_items > 2 ? 465 : no_items * 155
    height: Math.ceil(no_items / 3)* 155    
    property int no_items

...我只尝试从 UpdateFunction 更改 no_items 属性,或者只是尝试从中运行两个方程。

有没有办法解决?我还尝试了implicitHeight 和implicitWidth 属性。我真的很希望能够动态调整主要可扩展表示的大小,不得不对其进行硬编码然后有很多未填充的空间感觉很糟糕。

编辑:这是请求的示例:

根.qml

import org.kde.plasma.plasmoid 2.0
import QtQuick 2.2

Item
{
    id: root
    width: 185; height: 185
    Plasmoid.compactRepresentation: Compact {}
    Plasmoid.fullRepresentation: Full {}
    Plasmoid.preferredRepresentation: Plasmoid.compactRepresentation
    signal addItems() 
}

紧凑的.qml

import QtQuick 2.2

Item
{
    Rectangle
    {      
        width: root.width; height: root.height
        MouseArea
        {
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton | Qt.RightButton
            onClicked: if (mouse.button == Qt.RightButton) root.addItems()
                       else plasmoid.expanded = !plasmoid.expanded
        }
    }
}

完整的.qml

import QtQuick 2.2

Item 
{
    width: no_items > 2 ? 465 : no_items * 155
    height: Math.ceil(no_items / 3)* 155    
    property int no_items : 0
    function redrawGridView()  
    {        
        readingListModel.clear()       
        var i
        for (i = 0; i < no_items; i++) readingListModel.append({})                
    }     
    function addItems()
    {
        no_items = 6
        width = no_items > 2 ? 465 : no_items * 155
        height = Math.ceil(no_items / 3)* 155    
        redrawGridView()
    }

    ListModel 
    {
        id: readingListModel
    }

    GridView 
    { 
        id: readingGridView
        width: count > 2 ? 465 : count * 155
        height: Math.ceil(count/3) * 155
        cellWidth: 155; cellHeight: 155
        model: readingListModel
        delegate: Rectangle
        {                  
            width: 150; height: 150;
        }
    }

   Component.onCompleted:
    {            
        root.addItems.connect(addItems)    
        no_items = 3  
        redrawGridView()
    }
}

标签: qmlkdeplasmoidkde-plasma

解决方案


好吧,我没有得到答案,但我自己已经弄清楚了。实际上,正如我在评论中提到的,这似乎不可能使用具有完整表示的 plasmoid.expanded 方法,并且会产生其他错误。

但是您可以从代码中的任何位置动态调整 Window 项目的大小,所以这很好用:

在 Compact.qml 中

    Full
    {  
        id: fullRep
    }  
    MouseArea
    {
        anchors.fill: parent
        onClicked: 
        {
            if (!fullRep.visible) fullRep.show()
            else fullRep.close()
        }
    }

Full.qml 的开始

Window
{
    x: 100;y: 100
    width: 465;height: 195
    color: theme.backgroundColor
    flags: Qt.FramelessWindowHint //You can make it frameless like the expandable  
                                 //representation is when using the previous method

推荐阅读