Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QtQuickControls version



  • Hi there!!,

    I'm developing a ground station in Qt, using both C++ and QML files. I use the QML files to implement a map. The problem is that, following the Map Viewer example, when dynamically creating menu items and connecting the "Triggered" signals with other functions, the latest versions of QtQuickControls give runtime errors, while older version run without errors. As an example, the following lines:

    Menu {
      ....
      function update() {
        clear()
        addItem(qsTr("Add Marker")).triggered.connect(function(){itemClicked("addMarker")})
        ....
      }
    }
    

    With QtQuickControls 2.12 the errors are:

    ReferenceError: clear is not defined
    TypeError: Cannot read property 'triggered' of undefined
    

    Using QtQuickControls 1.4 I get no errors. Can someone help me understand the reason?



  • @DavidPL Hi,

    QtQuickControls 1.x and 2.x are two different API. Most of time you cannot switch between these versions without modifying your code.

    If you see documentation of Menu from QtQuickControls 2.x you will see that indeed clear() doesn't exist.



  • @Gojir4
    Yes, I've already checked that but I thought that maybe there is an equivalent expression in QtQuickControls 2.x for "clear()" and the connect expression, but I couldn't find anything.



  • Can you loop?:

    for(var item in menuId){
        menuId.removeItem(item)
    }
    

    I cannot find clear() method in Menu (QtQuick.Controls 1.4) documentation. So I am not sure exactly what clear does.

    Edit:
    I also don't know if its safe to modify a list I am looping through in javascript. Is it?



  • @fcarney said in QtQuickControls version:

    for(var item in menuId){
    menuId.removeItem(item)
    }

    Don't do this. It does a lot more than I expected. I did this:

                for(var item in menu1){
                    console.log(item)
                    menu1.removeItem(item)
                }
    

    The output is kinda scary:

    qml: objectName
    qml: x
    qml: y
    qml: z
    qml: width
    qml: height
    qml: implicitWidth
    qml: implicitHeight
    qml: contentWidth
    qml: contentHeight
    qml: availableWidth
    qml: availableHeight
    qml: margins
    qml: topMargin
    qml: leftMargin
    qml: rightMargin
    qml: bottomMargin
    qml: padding
    qml: topPadding
    qml: leftPadding
    qml: rightPadding
    qml: bottomPadding
    qml: locale
    qml: font
    qml: parent
    qml: background
    qml: contentItem
    qml: contentData
    qml: contentChildren
    qml: clip
    qml: focus
    qml: activeFocus
    qml: modal
    qml: dim
    qml: visible
    qml: opacity
    qml: scale
    qml: closePolicy
    qml: transformOrigin
    qml: enter
    qml: exit
    qml: spacing
    qml: opened
    qml: mirrored
    qml: enabled
    qml: palette
    qml: horizontalPadding
    qml: verticalPadding
    qml: anchors
    qml: implicitContentWidth
    qml: implicitContentHeight
    qml: implicitBackgroundWidth
    qml: implicitBackgroundHeight
    qml: topInset
    qml: leftInset
    qml: rightInset
    qml: bottomInset
    qml: contentModel
    qml: contentData
    qml: title
    qml: count
    qml: cascade
    qml: overlap
    qml: delegate
    qml: currentIndex
    qml: objectNameChanged
    qml: opened
    qml: closed
    qml: aboutToShow
    qml: aboutToHide
    qml: xChanged
    qml: yChanged
    qml: zChanged
    qml: widthChanged
    qml: heightChanged
    qml: implicitWidthChanged
    qml: implicitHeightChanged
    qml: contentWidthChanged
    qml: contentHeightChanged
    qml: availableWidthChanged
    qml: availableHeightChanged
    qml: marginsChanged
    qml: topMarginChanged
    qml: leftMarginChanged
    qml: rightMarginChanged
    qml: bottomMarginChanged
    qml: paddingChanged
    qml: topPaddingChanged
    qml: leftPaddingChanged
    qml: rightPaddingChanged
    qml: bottomPaddingChanged
    qml: fontChanged
    qml: localeChanged
    qml: parentChanged
    qml: backgroundChanged
    qml: contentItemChanged
    qml: contentChildrenChanged
    qml: clipChanged
    qml: focusChanged
    qml: activeFocusChanged
    qml: modalChanged
    qml: dimChanged
    qml: visibleChanged
    qml: opacityChanged
    qml: scaleChanged
    qml: closePolicyChanged
    qml: enterChanged
    qml: exitChanged
    qml: windowChanged
    qml: spacingChanged
    qml: openedChanged
    qml: mirroredChanged
    qml: enabledChanged
    qml: paletteChanged
    qml: horizontalPaddingChanged
    qml: verticalPaddingChanged
    qml: implicitContentWidthChanged
    qml: implicitContentHeightChanged
    qml: implicitBackgroundWidthChanged
    qml: implicitBackgroundHeightChanged
    qml: topInsetChanged
    qml: leftInsetChanged
    qml: rightInsetChanged
    qml: bottomInsetChanged
    qml: open
    qml: close
    qml: forceActiveFocus
    qml: forceActiveFocus
    qml: titleChanged
    qml: countChanged
    qml: cascadeChanged
    qml: overlapChanged
    qml: delegateChanged
    qml: currentIndexChanged
    qml: itemAt
    qml: addItem
    qml: insertItem
    qml: moveItem
    qml: removeItem
    qml: takeItem
    qml: menuAt
    qml: addMenu
    qml: insertMenu
    qml: removeMenu
    qml: takeMenu
    qml: actionAt
    qml: addAction
    qml: insertAction
    qml: removeAction
    qml: takeAction
    qml: popup
    qml: dismiss
    

    So, yeah, don't do that.



  • @fcarney @DavidPL
    Maybe

    for(var item in menu1.contentData){
        console.log(item)
        menu1.removeItem(item)
    }
    

    or even something more ambitious :)

    menu1.contentData = []
    


  • @Gojir4 said in QtQuickControls version:

    for(var item in menu1.contentData){

    That one removes every other item on first pass. I had tried that one.

    menu1.contentData = []

    Doesn't seem to do anything.

    This works:

                while(menu1.count){
                    menu1.removeItem(0)
                }
    


  • Does doing this create a memory leak? It doesn't address destruction of the item.


Log in to reply