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. Cannot assign QObject* to QQuickItem*
Forum Updated to NodeBB v4.3 + New Features

Cannot assign QObject* to QQuickItem*

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.9k Views 2 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.
  • V Offline
    V Offline
    Vinoth Rajendran4
    wrote on 25 Mar 2020, 14:17 last edited by
    #1

    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!

    J 1 Reply Last reply 26 Mar 2020, 02:05
    0
    • V Vinoth Rajendran4
      25 Mar 2020, 14:17

      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!

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 26 Mar 2020, 02:05 last edited by
      #2

      @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?

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      V 1 Reply Last reply 26 Mar 2020, 04:58
      2
      • J JKSH
        26 Mar 2020, 02:05

        @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?

        V Offline
        V Offline
        Vinoth Rajendran4
        wrote on 26 Mar 2020, 04:58 last edited by
        #3

        @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!

        1 Reply Last reply
        0

        1/3

        25 Mar 2020, 14:17

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved