Skip to content

Fix: example code in Part-1 Lesson-3 Properties #10

Description

@Fosssil

I found a bug in the following example from Part-1 QML Fundamentals Lesson-3 Properties
The current quickshell version is: 3.1

But I see you are refrencing this problem in Lesson-4 IDs

import QtQuick
import QtQuick.Window

Window {
  width: 400
  height: 300
  visible: true
  title: "Properties"

  property string greeting: "Hello!"
  property int count: 0

  Rectangle {
    id: box
    x: 50; y: 50
    width: 300
    height: 100
    color: count % 2 === 0 ? "#4ecdc4" : "#ff6b6b"
    radius: 8

    Text {
      text: parent.parent.greeting + " Count: " + parent.parent.count
      anchors.centerIn: parent
      color: "white"
      font.pixelSize: 18
    }
  }

  Text {
    text: "Click the window to increment"
    anchors.bottom: parent.bottom
    anchors.horizontalCenter: parent.horizontalCenter
    anchors.bottomMargin: 20
    color: "#888"
    font.pixelSize: 12
  }

  MouseArea {
    anchors.fill: parent
    onClicked: {
      count = count + 1
    }
  }
}

The code in text section is wrong as per the latest 3.1 version of quickshell

text: parent.parent.greeting + " Count: " + parent.parent.count

It outputs the following in the box:

undefined Count: undefined

Instead of:

Hello! Count: 0

The issue is caused by trying to access the Window properties using:

parent.parent.greeting
parent.parent.count

Solution:

Give the Window an id:

window {
    id: window
}

// Then access its property directly
Text {
    text: window.greeting + " Count: " + window.count
}

Use an object's id to explicitly reference its properties rather than relying on parent.parent traversal.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions