Attached Properties problem



  • I'm using Qt Creator 4.2.1 and am having problems using the Keys.onRightPressed signal. My understanding is it has something to do with the fact that Keys is an "Attached Property". The only way I could get it to work is to put the code for onRightPressed inside MainForm.ui.qml. The problem with that is the UI Designer doesn't like that code in that file. But I can't figure out how to move it to main.qml and have access to the data I need. All the code does is increment a counter and change the source property of an image based on that counter. Here is the code from MainForm.ui.qml

        Item {
            Image {
                property var buttonPageList: ["file:/C:/Users/Chris/Documents/QuickTest03/ButtonSet01.png", "file:/C:/Users/Chris/Documents/QuickTest03/ButtonSet02.png", "file:/C:/Users/Chris/Documents/QuickTest03/ButtonSet03.png" ,"file:/C:/Users/Chris/Documents/QuickTest03/ButtonSet04.png", "file:/C:/Users/Chris/Documents/QuickTest03/ButtonSet05.png", "file:/C:/Users/Chris/Documents/QuickTest03/ButtonSet06.png"]
                property var buttonPageIdx: 0
                property int pageCnt: buttonPageList.length
    
                id: image
                anchors.fill: parent
                source: "file:/C:/Users/Chris/Documents/QuickTest03/ButtonSet01.png"
                opacity: 1
            }
    
            anchors.fill: parent
            focus: true
            Keys.onRightPressed:         {
                event.accepted = true;
                image.buttonPageIdx = (image.buttonPageIdx + 1) % image.pageCnt;
                image.source = image.buttonPageList[image.buttonPageIdx];
            }
    
            Keys.onLeftPressed: {
                image.buttonPageIdx--
                if(image.buttonPageIdx < 0){
                    image.buttonPageIdx = image.pageCnt - 1
                }
                image.source = image.buttonPageList[image.buttonPageIdx];
            }
        }
    
    


  • @cdw3423 The way it is done in the Qt examples is to make a wrapper file, for example MyComponentForm.ui.qml and MyComponent.qml. MyComponentForm.ui.qml is edited with the wysiwyg editor and has only declarative code. All the functions go into MyComponent.qml which would have

    MyComponentForm {
    Keys.onRightPressed: {}
    }
    

    and in main.qml or somewhere else you use

    MyComponent {
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.