Visible property usage



  • Hello, I have 5 devices on the main screen. Each time I display one device I have to make the other 4 visible:false. I do it in this way as I am new to qml

    device1.visible:true
    device2.visible:false
    device3.visible:false
    device4.visible:false
    device5.visible:false

    . Instead of doing this everywhere is there any short method to implement this. Thanks for your time.

    Alternatively i also thought of using-- propery bool view:false and then assigning it to other device but this would also take many lines of code. I want to do it with min. LOC


  • Moderators

    Would that be OK?
    @
    property int visibilityFlag: 0x10000;

    device1.visible: (visibilityFlag & 0x10000)? true: false;
    device2.visible: (visibilityFlag & 0x01000)? true: false;
    device3.visible: (visibilityFlag & 0x00100)? true: false;
    device4.visible: (visibilityFlag & 0x00010)? true: false;
    device5.visible: (visibilityFlag & 0x00001)? true: false;
    @

    Warning: not tested. Use at your own responsibility. :)



  • Hello

    This problem makes me think about ListView, ListModel and Component. You could have been able to set a default property to false for the delegate. Then, each time you click on an Item, you set it to true with :
    @
    device.visible : Element.isCurrentItem? true : false /Element is ListView for instance/
    @

    Sierdzio solution is logic though...This kind of problem usually has a n complexity solution.

    Why not create a x elements listView to represent your x elements devices then each time you click on the corresponding element device in the listview, you retrieve the status of each variable :) ? That's straightforward



  • The correct solution for your case is the use of "States":http://doc.qt.digia.com/4.7-snapshot/qdeclarativestates.html. Define 5 States which describe the 5 different looks of your qml and just change the state whenever needed. So your code will look like this:

    @
    Rectangle{

    state: "device1Visible"
    
    device1.visible: state == "device1Visible"
    device2.visible: state == "device2Visible"
    device3.visible: state == "device3Visible"
    device4.visible: state == "device4Visible"
    device5.visible: state == "device5Visible"
    
    states:[
        State{ name: "none"},
        State{ name: "device1Visible" },
        State{ name: "device2Visible" },
        State{ name: "device3Visible" },
        State{ name: "device4Visible" },
        State{ name: "device5Visible" }
    ]
    

    }
    @

    Of course in this case it could be done using a simple string but the use of States can give you more.

    PS: I know it is not the most compact way of writing this so probably it doesn't answer your exact question but in my opinion this is how it should be done that is why i mention it.

    Regards.



  • Thank you all for your suggestion.:)



  • Feel free to ask more questions if you want. ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.