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. Why can't the contentData property of the menu be used in initialization?
Qt 6.11 is out! See what's new in the release blog

Why can't the contentData property of the menu be used in initialization?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
18 Posts 3 Posters 3.4k 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.
  • M Offline
    M Offline
    mirro
    wrote on last edited by
    #1
    Menu{
         height:contentData.lenght*50
    }
    
    jsulmJ 1 Reply Last reply
    0
    • M mirro

      @J-Hilk sorry,I mean that clicking the mouse button triggers the listView to slide left or right effect.
      Does the ListView provide relevant signals or methods?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #9

      @mirro nothing specific only stuff all QML items have like for example:

      import QtQuick 2.12
      import QtQuick.Window 2.0
      import QtQuick.Controls 2.12
      
      import Qt.labs.settings 1.0
      
      Window {
          id: window
      
          width: 300
          height: 300
      
          visible: true
      
          Button{
              id:switchModel
      
      
              height: 50
              width: 300
      
              text: "switch"
              onClicked: {
                  leftSide = !leftSide
              }
          }
      
          property bool leftSide: true
      
          ListView{
              id:lView
              width: parent.width / 2
              height: 250
              y:50
      
              x: leftSide ? 0 : window.width - width
              Behavior on x {
                  NumberAnimation{duration:500}
              }
      
              model: 20
              delegate: Rectangle{
                  width: lView.width
                  height: 50
                  color: "green"
                  Text{
                      text: modelData
                  }
              }
          }
      }
      
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      M 1 Reply Last reply
      0
      • M mirro
        Menu{
             height:contentData.lenght*50
        }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @mirro Is that the real code you have?
        Because you do not have any children in Menu, so contentData.lenght will be 0, don't you think so?

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        M 1 Reply Last reply
        0
        • jsulmJ jsulm

          @mirro Is that the real code you have?
          Because you do not have any children in Menu, so contentData.lenght will be 0, don't you think so?

          M Offline
          M Offline
          mirro
          wrote on last edited by
          #3

          @jsulm Menuitem must have been added

          errors: QQmlExpression:Expression test.qml depends on non-NOTIFYable properties:QQuickMenu:contentData    
          Unbale to assign [undefined] to double
          
          jsulmJ J.HilkJ 2 Replies Last reply
          0
          • M mirro

            @jsulm Menuitem must have been added

            errors: QQmlExpression:Expression test.qml depends on non-NOTIFYable properties:QQuickMenu:contentData    
            Unbale to assign [undefined] to double
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #4

            @mirro said in Why can't the contentData property of the menu be used in initialization?:

            Menuitem must have been added

            No idea what you are doing and what you are trying to tell me...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M mirro

              @jsulm Menuitem must have been added

              errors: QQmlExpression:Expression test.qml depends on non-NOTIFYable properties:QQuickMenu:contentData    
              Unbale to assign [undefined] to double
              
              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #5

              @mirro well, there you have your answer, it is used during initialization, but the Menu height won't update when contentData changes. So you assign a constant to height that depending on implementation may be any number, who knows 🤷‍♂️

              Use a real property or a real(known) constant


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              M 1 Reply Last reply
              1
              • J.HilkJ J.Hilk

                @mirro well, there you have your answer, it is used during initialization, but the Menu height won't update when contentData changes. So you assign a constant to height that depending on implementation may be any number, who knows 🤷‍♂️

                Use a real property or a real(known) constant

                M Offline
                M Offline
                mirro
                wrote on last edited by
                #6

                @J-Hilk I have individual question to consult next .
                How can I click a button to make the ListView switch between different views?

                J.HilkJ 1 Reply Last reply
                0
                • M mirro

                  @J-Hilk I have individual question to consult next .
                  How can I click a button to make the ListView switch between different views?

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #7

                  @mirro you mean switch between different model? Switching between different views doesn't make too much sense

                  import QtQuick 2.12
                  import QtQuick.Window 2.0
                  import QtQuick.Controls 2.12
                  
                  import Qt.labs.settings 1.0
                  
                  Window {
                      id: window
                  
                      width: 300
                      height: 300
                  
                      visible: true
                  
                      Button{
                          id:switchModel
                  
                  
                          height: 50
                          width: 300
                  
                          text: "switch"
                          onClicked: {
                              if(lView.model == model1)
                                  lView.model = model2
                              else
                                  lView.model = model1
                          }
                      }
                  
                      ListModel{
                          id:model1
                          ListElement{
                              content: "a"
                          }
                  
                          ListElement{
                              content: "b"
                          }
                          ListElement{
                              content: "c"
                          }
                      }
                  
                      ListModel{
                          id:model2
                          ListElement{
                              content: "1"
                          }
                  
                          ListElement{
                              content: "2"
                          }
                          ListElement{
                              content: "3"
                          }
                      }
                  
                      ListView{
                          id:lView
                          width: 300
                          height: 250
                          y:50
                  
                          model: model1
                          delegate: Text{
                              text: content
                          }
                      }
                  }
                  
                  

                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  M 1 Reply Last reply
                  1
                  • J.HilkJ J.Hilk

                    @mirro you mean switch between different model? Switching between different views doesn't make too much sense

                    import QtQuick 2.12
                    import QtQuick.Window 2.0
                    import QtQuick.Controls 2.12
                    
                    import Qt.labs.settings 1.0
                    
                    Window {
                        id: window
                    
                        width: 300
                        height: 300
                    
                        visible: true
                    
                        Button{
                            id:switchModel
                    
                    
                            height: 50
                            width: 300
                    
                            text: "switch"
                            onClicked: {
                                if(lView.model == model1)
                                    lView.model = model2
                                else
                                    lView.model = model1
                            }
                        }
                    
                        ListModel{
                            id:model1
                            ListElement{
                                content: "a"
                            }
                    
                            ListElement{
                                content: "b"
                            }
                            ListElement{
                                content: "c"
                            }
                        }
                    
                        ListModel{
                            id:model2
                            ListElement{
                                content: "1"
                            }
                    
                            ListElement{
                                content: "2"
                            }
                            ListElement{
                                content: "3"
                            }
                        }
                    
                        ListView{
                            id:lView
                            width: 300
                            height: 250
                            y:50
                    
                            model: model1
                            delegate: Text{
                                text: content
                            }
                        }
                    }
                    
                    
                    M Offline
                    M Offline
                    mirro
                    wrote on last edited by mirro
                    #8

                    @J-Hilk sorry,I mean that clicking the mouse button triggers the listView to slide left or right effect.
                    Does the ListView provide relevant signals or methods?

                    J.HilkJ 1 Reply Last reply
                    0
                    • M mirro

                      @J-Hilk sorry,I mean that clicking the mouse button triggers the listView to slide left or right effect.
                      Does the ListView provide relevant signals or methods?

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #9

                      @mirro nothing specific only stuff all QML items have like for example:

                      import QtQuick 2.12
                      import QtQuick.Window 2.0
                      import QtQuick.Controls 2.12
                      
                      import Qt.labs.settings 1.0
                      
                      Window {
                          id: window
                      
                          width: 300
                          height: 300
                      
                          visible: true
                      
                          Button{
                              id:switchModel
                      
                      
                              height: 50
                              width: 300
                      
                              text: "switch"
                              onClicked: {
                                  leftSide = !leftSide
                              }
                          }
                      
                          property bool leftSide: true
                      
                          ListView{
                              id:lView
                              width: parent.width / 2
                              height: 250
                              y:50
                      
                              x: leftSide ? 0 : window.width - width
                              Behavior on x {
                                  NumberAnimation{duration:500}
                              }
                      
                              model: 20
                              delegate: Rectangle{
                                  width: lView.width
                                  height: 50
                                  color: "green"
                                  Text{
                                      text: modelData
                                  }
                              }
                          }
                      }
                      
                      

                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      M 1 Reply Last reply
                      0
                      • J.HilkJ J.Hilk

                        @mirro nothing specific only stuff all QML items have like for example:

                        import QtQuick 2.12
                        import QtQuick.Window 2.0
                        import QtQuick.Controls 2.12
                        
                        import Qt.labs.settings 1.0
                        
                        Window {
                            id: window
                        
                            width: 300
                            height: 300
                        
                            visible: true
                        
                            Button{
                                id:switchModel
                        
                        
                                height: 50
                                width: 300
                        
                                text: "switch"
                                onClicked: {
                                    leftSide = !leftSide
                                }
                            }
                        
                            property bool leftSide: true
                        
                            ListView{
                                id:lView
                                width: parent.width / 2
                                height: 250
                                y:50
                        
                                x: leftSide ? 0 : window.width - width
                                Behavior on x {
                                    NumberAnimation{duration:500}
                                }
                        
                                model: 20
                                delegate: Rectangle{
                                    width: lView.width
                                    height: 50
                                    color: "green"
                                    Text{
                                        text: modelData
                                    }
                                }
                            }
                        }
                        
                        
                        M Offline
                        M Offline
                        mirro
                        wrote on last edited by
                        #10

                        @J-Hilk Thank you very much.One last question.
                        Is menuItem.highlighted available in QT5.9.7? The test program has no effect.

                         Menu {
                                id: menu
                                widh:100
                                height:100
                                MenuItem {
                                    id:menuItem 
                                    width:100       
                                    height:40         
                                    text: "New..."   
                                    background:Rectangle{
                                            color:menuItem.highlighted?'black':'red'
                                     }
                                }
                            }
                        
                        J.HilkJ 1 Reply Last reply
                        0
                        • M mirro

                          @J-Hilk Thank you very much.One last question.
                          Is menuItem.highlighted available in QT5.9.7? The test program has no effect.

                           Menu {
                                  id: menu
                                  widh:100
                                  height:100
                                  MenuItem {
                                      id:menuItem 
                                      width:100       
                                      height:40         
                                      text: "New..."   
                                      background:Rectangle{
                                              color:menuItem.highlighted?'black':'red'
                                       }
                                  }
                              }
                          
                          J.HilkJ Offline
                          J.HilkJ Offline
                          J.Hilk
                          Moderators
                          wrote on last edited by J.Hilk
                          #11

                          @mirro take a look at the documentation:
                          https://doc.qt.io/qt-5/qtquickcontrols2-customize.html#customizing-menu

                          for more information about that.

                          Since MenuItem inherits from Control, yes it has a background property since the very beginning :D, version independent
                          same for highlighted, its a thing since MenuItem was introduced in 5.7


                          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                          Q: What's that?
                          A: It's blue light.
                          Q: What does it do?
                          A: It turns blue.

                          M 1 Reply Last reply
                          1
                          • J.HilkJ J.Hilk

                            @mirro take a look at the documentation:
                            https://doc.qt.io/qt-5/qtquickcontrols2-customize.html#customizing-menu

                            for more information about that.

                            Since MenuItem inherits from Control, yes it has a background property since the very beginning :D, version independent
                            same for highlighted, its a thing since MenuItem was introduced in 5.7

                            M Offline
                            M Offline
                            mirro
                            wrote on last edited by
                            #12

                            @J-Hilk

                            Well, I don't know, but menuItem.highlighted is also false when I set hoverEnable:true

                            Menu {
                                    id: menu
                                    widh:100
                                    height:100
                                    MenuItem {
                                        id:menuItem 
                                        hoverEnable:true
                                        width:100       
                                        height:40         
                                        text: "New..."   
                                        background:Rectangle{
                                                color:menuItem.highlighted?'black':'red'
                                         }
                                    }
                                }
                            
                            J.HilkJ 1 Reply Last reply
                            0
                            • M mirro

                              @J-Hilk

                              Well, I don't know, but menuItem.highlighted is also false when I set hoverEnable:true

                              Menu {
                                      id: menu
                                      widh:100
                                      height:100
                                      MenuItem {
                                          id:menuItem 
                                          hoverEnable:true
                                          width:100       
                                          height:40         
                                          text: "New..."   
                                          background:Rectangle{
                                                  color:menuItem.highlighted?'black':'red'
                                           }
                                      }
                                  }
                              
                              J.HilkJ Offline
                              J.HilkJ Offline
                              J.Hilk
                              Moderators
                              wrote on last edited by
                              #13

                              @mirro for once there are 2 typos in your code

                              widh:100 -> width:100
                              and
                              hoverEnable:true -> hoverEnabled:true


                              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                              Q: What's that?
                              A: It's blue light.
                              Q: What does it do?
                              A: It turns blue.

                              M 1 Reply Last reply
                              2
                              • J.HilkJ J.Hilk

                                @mirro for once there are 2 typos in your code

                                widh:100 -> width:100
                                and
                                hoverEnable:true -> hoverEnabled:true

                                M Offline
                                M Offline
                                mirro
                                wrote on last edited by
                                #14

                                @J-Hilk
                                Sorry, my work machine has no network, so I can't copy the code correctly.
                                The program I tested had no syntax errors, but menuItem.highlighted was always false, I don't know why.

                                Menu {
                                        id: menu
                                        width:100
                                        height:100
                                        MenuItem {
                                            id:menuItem 
                                            hoverEnabled:true
                                            width:100       
                                            height:40         
                                            text: "New..."   
                                            background:Rectangle{
                                                    color:menuItem.highlighted?'black':'red'
                                             }
                                        }
                                    }
                                
                                J.HilkJ 1 Reply Last reply
                                0
                                • M mirro

                                  @J-Hilk
                                  Sorry, my work machine has no network, so I can't copy the code correctly.
                                  The program I tested had no syntax errors, but menuItem.highlighted was always false, I don't know why.

                                  Menu {
                                          id: menu
                                          width:100
                                          height:100
                                          MenuItem {
                                              id:menuItem 
                                              hoverEnabled:true
                                              width:100       
                                              height:40         
                                              text: "New..."   
                                              background:Rectangle{
                                                      color:menuItem.highlighted?'black':'red'
                                               }
                                          }
                                      }
                                  
                                  J.HilkJ Offline
                                  J.HilkJ Offline
                                  J.Hilk
                                  Moderators
                                  wrote on last edited by J.Hilk
                                  #15

                                  @mirro works fine for me:

                                  384367cd-d9a5-4371-809f-72f69f334d69-image.png

                                  import QtQuick 2.12
                                  import QtQuick.Controls 2.12
                                  import QtQuick.Window 2.2
                                  
                                  ApplicationWindow {
                                      visible: true
                                      width: 640
                                      height: 200
                                      title: qsTr("Hello World")
                                      id:root
                                  
                                      Component.onCompleted: menu.open()
                                  
                                      Menu{
                                          id:menu
                                          width: 640
                                          height: 100
                                  
                                          MenuItem{
                                              id:menuItem2
                                              width: 100
                                              height: 40
                                              text:"New..."
                                              hoverEnabled:true
                                              background:Rectangle{
                                                  implicitWidth: 100
                                                  implicitHeight: 40
                                                  color: menuItem2.highlighted ? "black" : "red"
                                              }
                                  
                                          }
                                          MenuItem{
                                              id:menuItem1
                                              width: 100
                                              height: 40
                                              text:"Delete..."
                                              hoverEnabled:true
                                              background:Rectangle{
                                                  implicitWidth: 100
                                                  implicitHeight: 40
                                                  color: menuItem1.highlighted ? "black" : "red"
                                              }
                                          }
                                      }
                                  }
                                  
                                  

                                  but I'm on 5.12.6 🤷‍♂️ I do not have any other version installed


                                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                  Q: What's that?
                                  A: It's blue light.
                                  Q: What does it do?
                                  A: It turns blue.

                                  M 1 Reply Last reply
                                  0
                                  • J.HilkJ J.Hilk

                                    @mirro works fine for me:

                                    384367cd-d9a5-4371-809f-72f69f334d69-image.png

                                    import QtQuick 2.12
                                    import QtQuick.Controls 2.12
                                    import QtQuick.Window 2.2
                                    
                                    ApplicationWindow {
                                        visible: true
                                        width: 640
                                        height: 200
                                        title: qsTr("Hello World")
                                        id:root
                                    
                                        Component.onCompleted: menu.open()
                                    
                                        Menu{
                                            id:menu
                                            width: 640
                                            height: 100
                                    
                                            MenuItem{
                                                id:menuItem2
                                                width: 100
                                                height: 40
                                                text:"New..."
                                                hoverEnabled:true
                                                background:Rectangle{
                                                    implicitWidth: 100
                                                    implicitHeight: 40
                                                    color: menuItem2.highlighted ? "black" : "red"
                                                }
                                    
                                            }
                                            MenuItem{
                                                id:menuItem1
                                                width: 100
                                                height: 40
                                                text:"Delete..."
                                                hoverEnabled:true
                                                background:Rectangle{
                                                    implicitWidth: 100
                                                    implicitHeight: 40
                                                    color: menuItem1.highlighted ? "black" : "red"
                                                }
                                            }
                                        }
                                    }
                                    
                                    

                                    but I'm on 5.12.6 🤷‍♂️ I do not have any other version installed

                                    M Offline
                                    M Offline
                                    mirro
                                    wrote on last edited by
                                    #16

                                    @J-Hilk

                                    highlighted attribute may be need to maintain yourself in QT5.9.7

                                    J.HilkJ 1 Reply Last reply
                                    0
                                    • M mirro

                                      @J-Hilk

                                      highlighted attribute may be need to maintain yourself in QT5.9.7

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #17

                                      @mirro have you tried my fully working example code?


                                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                                      Q: What's that?
                                      A: It's blue light.
                                      Q: What does it do?
                                      A: It turns blue.

                                      M 1 Reply Last reply
                                      0
                                      • J.HilkJ J.Hilk

                                        @mirro have you tried my fully working example code?

                                        M Offline
                                        M Offline
                                        mirro
                                        wrote on last edited by mirro
                                        #18

                                        @J-Hilk

                                        My working environment does not have Internet. I have tested it with similar code.
                                        I maintaining this highlighted attribute in the background: Rectangle: MouseArea {} in Qt5.9.7 now.

                                        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