如何在Component外部设置图像源

时间:2013-05-09 13:08:11

标签: image qt qml

我的QML应用程序中的对话框中显示了一个图像,我希望以后能够使用onClicked更改该图像,我通过一个函数来检查我想要的新源URL中的变量是我想要的其中一个。

我只是尝试使用Image.source =“NEWURL”这是一个不行。还有图像所在组件的id,以及如下对话框:id.source =“neurl” - 没有去。

我该怎么做?

编辑:添加更多代码;用于单击的函数和listitm。该图像是一个Web图像,我希望在url中包含conncectedUser值(这是一个用户名)。

以下是所有相关代码:

// Check if users is really a user, and if; show skin
function checkCurrentUser(currentUser) {
    console.debug('Debug: Check user "'+currentUser+'" if actually a user.')

    if (currentUser == "Ingen online") {
        currentUser = "Notch" // reset currentUser if pushed earlier
        console.debug('Debug: It was not a real user. Showing '+currentUser+' instead')
        Image.source = "http://blabla"+currentUser+"yesyes"
    }
    else {
        console.debug('Debug: It was a real user.')
        Image.source = "http://blabla"+currentUser+"yesyes"
    }
    return "http://blabla"+currentUser+"yesyes""
}

            // the dialog I want to show with a image
            Component {
                 id: userDialog
                 Dialog {
                     id: dialogueUser
                     title: i18n.tr("Image")
                     Image {
                         id: usersSkin
                         fillMode: Image.PreserveAspectFit
                         source: "URL"
                         sourceSize.height: 1200
                     }

                     Button {
                         text: i18n.tr("Close")
                         color: "red"
                         onClicked: PopupUtils.close(dialogueUser)
                     }
                 }
                }

     // and then the list containting each link, which on click should show the user image
     ListView {
                    id: userList
                    width: parent.width
                    height: units.gu(5)
                    model: msmData
                    delegate: ListItem.Standard {
                        text: connectedUser
                        onClicked: {
                            console.debug('Debug: User clicked "'+connectedUser+'"')
                            checkCurrentUser(connectedUser)
                            PopupUtils.open(userDialog, userList)
                        }
                    }
                    header: ListItem.Header { text: i18n.tr("Connected Users") }
                    section.property: "type"
                    section.criteria: ViewSection.FullString
                    section.delegate: ListItem.Header { text: i18n.tr(section) }
                }

2 个答案:

答案 0 :(得分:0)

我不确定我是否理解你的问题,但我会尝试一下:

   Component 
   {
             id: userDialog
             Dialog 
             {
                 property int sourceState : 1
                 id: dialogueUser
                 title: i18n.tr("Image")
                 Image 
                 {
                     id: usersSkin
                     fillMode: Image.PreserveAspectFit
                     source: 1 == sourceState ? "OLDURL" : "NEWURL"
                     sourceSize.height: 1200
                 }

                 Button 
                 {
                     text: i18n.tr("Close")
                     color: "red"
                     onClicked: 
                     {
                        PopupUtils.close(dialogueUser)
                        dialogueUser.sourceState = 0                            
                     }
                 }
              }
    }

答案 1 :(得分:0)

我最终做的只是重置图像URL中的变量,然后显示对话框。现在就工作。