Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Attached Properties problem
Forum Updated to NodeBB v4.3 + New Features

Attached Properties problem

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 635 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cdw3423
    wrote on last edited by
    #1

    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];
            }
        }
    
    
    E 1 Reply Last reply
    0
    • C cdw3423

      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];
              }
          }
      
      
      E Offline
      E Offline
      Eeli K
      wrote on last edited by
      #2

      @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 {
      }
      
      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved