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

Cannot assign QObject* to QQuickItem*



  • Hi All,

    i am getting error at runtime (line number:29)

    1     import QtQuick 2.0
    2     import QtQuick.Window 2.2
    3
    4     Window {
    5         id: rootW
    6         visible: true
    7
    8         width : Screen.desktopAvailableWidth * 0.75
    9         height: Screen.desktopAvailableHeight * 0.75
    10
    11         Rectangle {
    12               id: parasite
    13              parent: rootW
    14              x: 10; y: 10; width: 100; height: 100
    15               color: "blue"
    16         }
    17
    18        Rectangle {
    19           id: centreR
    20            x: rootW.width * 0.25
    21             y: rootW.height * 0.25
    22             width: rootW.width * 0.5
    23             height: rootW.height * 0.5
    24            color: "green"
    25
    26              MouseArea {
    27                    anchors.fill: centreR
    28                    onClicked: {
    29                          parasite.parent = rootW   // Error: Cannot assign QObject* to QQuickItem*
    30                          centreR.destroy()
    31                    }
    32              }
    33        }
    34
    35  }
    
    
    

    I have two questions,
    1. Is there any way we could do casting such as reinterpret_cast inside QML file, to overcome this error @ line number 29 ?
    2. I wonder why error happens @ line 29, whereas we don't have any issue @ line number 13, doing the same operation ?

    Thanks in advance!


  • Moderators

    @Vinoth-Rajendran4 said in Cannot assign QObject* to QQuickItem*:

    2. I wonder why error happens @ line 29, whereas we don't have any issue @ line number 13, doing the same operation ?

    I'm not convinced that line #13 is meaningful. I think it's a silent error in Qt -- you should have received an error message.

    Note: lines 13 and 29 are very different operations. Line #13 (:) sets up a property binding while line #29 (=) does an assignment. See https://doc.qt.io/qt-5/qtqml-syntax-propertybinding.html for details.

    1. Is there any way we could do casting such as reinterpret_cast inside QML file, to overcome this error @ line number 29 ?

    You cannot "overcome" the error.

    Line #29 tries to call QQuickItem::setParentItem() (https://doc.qt.io/qt-5/qquickitem.html#parent-prop ). This function cannot accept a QQuickWindow*.

    See also https://doc.qt.io/qt-5/qtquick-visualcanvas-visualparent.html to understand the different types of parents.

    Can you tell us why you want to set the Rectangle's parent?



  • @JKSH , Thanks for the reply

    Line #13 produce error, i din't see it before.
    https://doc.qt.io/qt-5/qtqml-syntax-propertybinding.html , this link was useful.

    I wrote this code to understand the difference between visual parent and object parent. I know it makes no sense. But since i found an error, i thought to get it clarified.

    Thank You!


Log in to reply