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. Avoid unqualified access with required property implementation trouble
Forum Updated to NodeBB v4.3 + New Features

Avoid unqualified access with required property implementation trouble

Scheduled Pinned Locked Moved Solved QML and Qt Quick
3 Posts 2 Posters 1.7k Views 1 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.
  • A Offline
    A Offline
    Aymeric_Qt
    wrote on last edited by Aymeric_Qt
    #1

    Hello,

    I got a question about QML best practices implementation. My case is simple, opening a dialog when ckicking on a toolbar button.
    In the first place I wrote this:

    main.qml:

    ApplicationWindow {
        readonly property ApplicationWindow appWindow: this
    
        width: 640
        height: 480
        visible: true
        // ...
    }
    

    MenuAbout.qml:

    Menu {
        title: qsTr("A propos")
        MenuItem {
            text: qsTr("&A Propos")
            display: AbstractButton.TextOnly
            onTriggered: aboutDialog.open()
        }
        DialogAbout {
            id: aboutDialog
        }
    }
    

    DialogAbout.qml:

    Dialog {
        id: aboutDialog
    
        anchors.centerIn: parent
    
        parent: appWindow.contentItem
        modal: false
        title: "About this"
        // ...
    }
    

    This was based on what I've found (on stackoverflow) at the time to solve the "problem" about the need of the Dialog component to have a parent to open in.
    So this is what I've done to get a reference to a parent component even if I had concerns about the concept of a global property "floating" arround, accessible to any children.

    Then I found this video of a talk made during the QtWS 2021. And there is a section named 'Avoid unaqualifed access - Outside property' where I think I've found a better solution.

    So as it is done in the video I've modify main.qml:

    ApplicationWindow {
        id: appWindow
        
        width: 640
        height: 480
        visible: true
        // ...
    }
    

    and AboutDialog.qml:

    Dialog {
        required property ApplicationWindow appWindow
    
        id: aboutDialog
    
        anchors.centerIn: parent
    
        parent: appWindow.contentItem
        modal: false
        title: "About this"
        // ...
    }
    

    But in this case the application crash when lauching it with the following error:
    "Required property appWindow was not initialized".

    And that's where I get confused. For me, as 'appWindow' is the id I gave to the application window in the main.qml file it should be initialized when the application start, no?
    So I don't know if I got in the wrong way (required property should not be use in this case) or if I just missing a piece.

    Thanks for reading.

    JKSHJ 1 Reply Last reply
    0
    • JKSHJ JKSH

      @Aymeric_Qt said in Avoid unqualified access with required property implementation trouble:

      For me, as 'appWindow' is the id I gave to the application window in the main.qml file it should be initialized when the application start, no?
      So I don't know if I got in the wrong way (required property should not be use in this case) or if I just missing a piece.

      If your AboutDialog has a required property called appWindow, that means your main.qml needs to assign something to the dialog's appWindow property:

      AboutDialog {
          appWindow: <something> // This line is required. Removing this line will give you an error.
      }
      

      The name of the dialog's property is completely unrelated to the ID that you assign to your root window.

      A Offline
      A Offline
      Aymeric_Qt
      wrote on last edited by Aymeric_Qt
      #3

      Hello @JKSH
      Thank you for your reply therefore there still something that I don't understand.
      In my main.qml I added

      AboutDialog {
          appWindowRef: appWindow //I've changed the required property's name in AboutDialog to avoid confusion
      }
      

      But I still get the same error message "Required property appWindowRef was not initialized". I feel that I'm missing something obvious, sorry about that.

      And I got an other question in terms of best practices is it ok to "intialize" all the component which will need a reference to the Application Window in the main.qml file ? Or is there a cleaner way ?

      Edit: There is something that I've forgot to mention.
      I've also tried to initialoze a required property in an other component (just to test) which is also declared in the main.qml and in this case it is working perfectly.

      main.qml

      ApplicationWindow {
      
      id: appWindow
      
      //...
          MainStackLayout {
              appWindowRef: appWindow
              anchors.top: mainToolBar.bottom
              anchors.bottom: mainTabBar.top
              currentIndex: mainTabBar.currentIndex
          }
      
      

      MainStackLayout.qml

      StackLayout {
          id: layout
      
          required property ApplicationWindow appWindowRef
      
         //...
      }
      

      So is it supposed to be different with Dialog ?

      And concerning my question about good pratices, I've look to some projects examples (those available in Qt Creator Examples tab) and it seems to be done that way. So I guess it is the right way at least for someone whith my level of knowledge which is not very high.

      1 Reply Last reply
      0
      • A Aymeric_Qt

        Hello,

        I got a question about QML best practices implementation. My case is simple, opening a dialog when ckicking on a toolbar button.
        In the first place I wrote this:

        main.qml:

        ApplicationWindow {
            readonly property ApplicationWindow appWindow: this
        
            width: 640
            height: 480
            visible: true
            // ...
        }
        

        MenuAbout.qml:

        Menu {
            title: qsTr("A propos")
            MenuItem {
                text: qsTr("&A Propos")
                display: AbstractButton.TextOnly
                onTriggered: aboutDialog.open()
            }
            DialogAbout {
                id: aboutDialog
            }
        }
        

        DialogAbout.qml:

        Dialog {
            id: aboutDialog
        
            anchors.centerIn: parent
        
            parent: appWindow.contentItem
            modal: false
            title: "About this"
            // ...
        }
        

        This was based on what I've found (on stackoverflow) at the time to solve the "problem" about the need of the Dialog component to have a parent to open in.
        So this is what I've done to get a reference to a parent component even if I had concerns about the concept of a global property "floating" arround, accessible to any children.

        Then I found this video of a talk made during the QtWS 2021. And there is a section named 'Avoid unaqualifed access - Outside property' where I think I've found a better solution.

        So as it is done in the video I've modify main.qml:

        ApplicationWindow {
            id: appWindow
            
            width: 640
            height: 480
            visible: true
            // ...
        }
        

        and AboutDialog.qml:

        Dialog {
            required property ApplicationWindow appWindow
        
            id: aboutDialog
        
            anchors.centerIn: parent
        
            parent: appWindow.contentItem
            modal: false
            title: "About this"
            // ...
        }
        

        But in this case the application crash when lauching it with the following error:
        "Required property appWindow was not initialized".

        And that's where I get confused. For me, as 'appWindow' is the id I gave to the application window in the main.qml file it should be initialized when the application start, no?
        So I don't know if I got in the wrong way (required property should not be use in this case) or if I just missing a piece.

        Thanks for reading.

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #2

        @Aymeric_Qt said in Avoid unqualified access with required property implementation trouble:

        For me, as 'appWindow' is the id I gave to the application window in the main.qml file it should be initialized when the application start, no?
        So I don't know if I got in the wrong way (required property should not be use in this case) or if I just missing a piece.

        If your AboutDialog has a required property called appWindow, that means your main.qml needs to assign something to the dialog's appWindow property:

        AboutDialog {
            appWindow: <something> // This line is required. Removing this line will give you an error.
        }
        

        The name of the dialog's property is completely unrelated to the ID that you assign to your root window.

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

        A 1 Reply Last reply
        1
        • JKSHJ JKSH

          @Aymeric_Qt said in Avoid unqualified access with required property implementation trouble:

          For me, as 'appWindow' is the id I gave to the application window in the main.qml file it should be initialized when the application start, no?
          So I don't know if I got in the wrong way (required property should not be use in this case) or if I just missing a piece.

          If your AboutDialog has a required property called appWindow, that means your main.qml needs to assign something to the dialog's appWindow property:

          AboutDialog {
              appWindow: <something> // This line is required. Removing this line will give you an error.
          }
          

          The name of the dialog's property is completely unrelated to the ID that you assign to your root window.

          A Offline
          A Offline
          Aymeric_Qt
          wrote on last edited by Aymeric_Qt
          #3

          Hello @JKSH
          Thank you for your reply therefore there still something that I don't understand.
          In my main.qml I added

          AboutDialog {
              appWindowRef: appWindow //I've changed the required property's name in AboutDialog to avoid confusion
          }
          

          But I still get the same error message "Required property appWindowRef was not initialized". I feel that I'm missing something obvious, sorry about that.

          And I got an other question in terms of best practices is it ok to "intialize" all the component which will need a reference to the Application Window in the main.qml file ? Or is there a cleaner way ?

          Edit: There is something that I've forgot to mention.
          I've also tried to initialoze a required property in an other component (just to test) which is also declared in the main.qml and in this case it is working perfectly.

          main.qml

          ApplicationWindow {
          
          id: appWindow
          
          //...
              MainStackLayout {
                  appWindowRef: appWindow
                  anchors.top: mainToolBar.bottom
                  anchors.bottom: mainTabBar.top
                  currentIndex: mainTabBar.currentIndex
              }
          
          

          MainStackLayout.qml

          StackLayout {
              id: layout
          
              required property ApplicationWindow appWindowRef
          
             //...
          }
          

          So is it supposed to be different with Dialog ?

          And concerning my question about good pratices, I've look to some projects examples (those available in Qt Creator Examples tab) and it seems to be done that way. So I guess it is the right way at least for someone whith my level of knowledge which is not very high.

          1 Reply Last reply
          0
          • A Aymeric_Qt has marked this topic as solved on

          • Login

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