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. Qt Quick GridView variable cellWidth depending on Model is not working
Forum Updated to NodeBB v4.3 + New Features

Qt Quick GridView variable cellWidth depending on Model is not working

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 2.2k 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.
  • B Offline
    B Offline
    boogerlad
    wrote on last edited by
    #1

    My model is called ContactModel.qml with this code:
    @
    import QtQuick 2.2

    ListModel {
        ListElement {
            name: "Jim Williams"
            portrait: "item.png"
        }
        ListElement {
            name: "John Brown"
            portrait: "item.png"
        }
        ListElement {
            name: "Bill Smyth"
            portrait: "item.png"
        }
    }
    

    @
    and in my main.qml,

    @ GridView
    {
    width: 1360
    height: 1120
    cellWidth: width / ((ContactModel.count < 3) ? ContactModel.count: 3)
    cellHeight: 386
    model: ContactModel {}
    delegate: Column
    {
    Image
    {
    source: portrait;
    anchors.horizontalCenter: parent.horizontalCenter
    }
    Text
    {
    text: name;
    anchors.horizontalCenter: parent.horizontalCenter
    }
    }
    }
    @

    Am I not allowed to use ternary operators/logic in qml files? I would like to have the width, height, and positioning vary depending on the amount of input. When I try to output ContactModel.count, I get nothing via messagebox, text, etc.

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      Hi,

      You can definitely use the ternary operators. But the ternary operator expression you have used i think is wrong.
      It should be something like this
      @
      cellWidth: model.count < 3 ? 250 : 120
      //Replace 250 and 120 with what ever values you want or expressions that evaluate to a valid value.
      @
      Also where did you try to output model's count ?
      An ideal way would be when Component's onCompleted function.
      So in gridview,
      @
      Component.onCompleted: {
      console.log(grid.model.count)
      }
      @

      will work.
      Hope this helps.

      157

      1 Reply Last reply
      0
      • jeremy_kJ Offline
        jeremy_kJ Offline
        jeremy_k
        wrote on last edited by
        #3

        The problem is that the code is referring to ContactModel as if it was an instance, rather than a type. The error output from qmlscene gives this hint:
        untitled1.qml:7: ReferenceError: ContactModel is not defined

        Changing the reference for the cellWidth binding from ContactModel.count to model.count works.

        In anything beyond an example, the code should also be prepared for model being undefined, or count being 0.

        Asking a question about code? http://eel.is/iso-c++/testcase/

        1 Reply Last reply
        0
        • B Offline
          B Offline
          boogerlad
          wrote on last edited by
          #4

          I'm using Qt Creator. There didn't appear to be any errors before. Perhaps I haven't set up Qt Creator properly, since the design tab is also completely broken. Thanks for your help though!

          1 Reply Last reply
          0
          • jeremy_kJ Offline
            jeremy_kJ Offline
            jeremy_k
            wrote on last edited by
            #5

            There should be an "Application Output" box at the bottom of the Creator window. Clicking that opens a pane that displays the standard output and error streams from the application, as well as some execution information. It might not be open by default.

            Asking a question about code? http://eel.is/iso-c++/testcase/

            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