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. JS function not returning expected value
Forum Updated to NodeBB v4.3 + New Features

JS function not returning expected value

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 2 Posters 369 Views 1 Watching
  • 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.
  • mzimmersM Offline
    mzimmersM Offline
    mzimmers
    wrote on last edited by
    #1

    Hi all -

    I tried moving a width calculation out of QML into a JS function, but it's not returning the correct value.

    Flickable {
        id: flickable
        property real netWidth: flickable.width - (scroller.width * 2.0)
    
        GridLayout {
            id: equipmentGrid
    
            property int nbrColumns: 4
            
            // this works as expected
            property real tileWidth: (flickable.netWidth - 
                                     (mainWindow.gutter * (nbrColumns - 1))) / nbrColumns
    
            // this doesn't work
    //      property real tileWidth: Fn.getColumnWidth(nbrColumns, flickable.netWidth)
    
        }
        ScrollBar.vertical: Scrollbar_custom {
            id: scroller
        }
    }
    

    Here's the JS function:

    function getColumnWidth(nbrCols, totalWidth) {
        var colWidth
    
        colWidth = (width -
                    (mainWindow.gutter * (nbrCols - 1))) / nbrCols
        if (colWidth < 0) {
            colWidth = 0
        }
    
        return colWidth;
    }
    

    The problem appears to be related to the value of the scroller width. Using the function, it seems to be 0. I'm guessing that this has something to do with when the function is actually called, evidently prior to the creation of the scroller, and therefore the setting of its width.

    Can anyone shed some light on this? This particular problem is easy enough to work around, but I'd like to have a better understanding of what's going on for the future.

    Thanks...

    1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by JoeCFD
      #2

      Try this? I guess widgets sizes are not available at the beginning.

          property real netWidth: 0
      
          Component.onCompleted: {
              netWidth = flickable.width - (scroller.width * 2.0)
          }
      
      mzimmersM 1 Reply Last reply
      0
      • JoeCFDJ JoeCFD

        Try this? I guess widgets sizes are not available at the beginning.

            property real netWidth: 0
        
            Component.onCompleted: {
                netWidth = flickable.width - (scroller.width * 2.0)
            }
        
        mzimmersM Offline
        mzimmersM Offline
        mzimmers
        wrote on last edited by
        #3

        @JoeCFD that didn't change anything.

        JoeCFDJ 1 Reply Last reply
        0
        • mzimmersM mzimmers

          @JoeCFD that didn't change anything.

          JoeCFDJ Offline
          JoeCFDJ Offline
          JoeCFD
          wrote on last edited by
          #4

          @mzimmers use console.log() to print out values of them

          mzimmersM 1 Reply Last reply
          0
          • JoeCFDJ JoeCFD

            @mzimmers use console.log() to print out values of them

            mzimmersM Offline
            mzimmersM Offline
            mzimmers
            wrote on last edited by
            #5

            @JoeCFD this is weird:

                Flickable {
                    id: flickable
            
                    Layout.fillHeight: true
                    Layout.fillWidth: true
                    clip: true
                    contentHeight: equipmentGrid.height
                    property real netWidth: 0//flickable.width - (scroller.width * 2.0)
                    Component.onCompleted: {
                        netWidth = flickable.width - (scroller.width * 2.0)
                        console.log("flickable.width is " + flickable.width)
                        console.log("netWidth is " + netWidth)
                        console.log("scroller.width is " + scroller.width)
                    }
            ...
            

            qml: flickable.width is 48 // ???
            qml: netWidth is 28
            qml: scroller.width is 10

            Could part of the problem be that my flickable is inside a ColumnLayout?

            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