Results 1 to 4 of 4

Thread: Very basic question on how to setup a QT quick checkbox.

  1. #1
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Very basic question on how to setup a QT quick checkbox.

    I am new to QT quick and am having difficulty understanding the basic sequence of how items are executed. Sorry if the question is so basic. I have worked my way through an example that has the date/time and weather information. The top level item is a Rectangle and it has a bool property called forceOffline. Now this property can be changed by either a Config button which brings up a dialog set of checkboxes (one of which modifies it) or a single button at the top level.
    My problem is that if I tick the checkbox to change the forceOffline property and then use the single button to switch back and then go back to the dialog the checkbox remains checked. Sorry if I have not made this very clear. Below I have my checkbox item
    Qt Code:
    1. import QtQuick 2.3
    2.  
    3. Item {
    4. id: root
    5. property bool checked: true
    6. width: checkBox.width
    7. height: checkBox.height
    8.  
    9. Image {
    10. id: checkBox
    11. source: root.checked ?
    12. "../../content/resources/checkbox.png" :
    13. "../../content/resources/draw-rectangle.png"
    14. Keys.onPressed: {
    15. if (event.key == Qt.Key_Return ||
    16. event.key == Qt.Key_Enter ||
    17. event.key == Qt.Key_Space)
    18. root.checked = !root.checked;
    19. }
    20. MouseArea {
    21. anchors.fill: parent
    22. onClicked: {
    23. root.checked = !root.checked;
    24. }
    25. }
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 
    I use code like
    Qt Code:
    1. CheckBox {
    2. id: offlineCheckBox
    3. checked: root.forceOffline
    4. KeyNavigation.up: secondsCheckBox
    5. KeyNavigation.down: locationTextInput
    6. }
    To copy to clipboard, switch view to plain text mode 
    to create an instance of the Checkbox. I thought that the binding of "checked: root.forceOffline" would ensure that the checkbox was correctly ticked or not depending on its boolean state. Can anybody help me understand this?
    AS another quick question, when dealing with ordinary QT C++ you can always add a diagnostic to discover the current status of a variable. Not sure how you do this in QT quick..the closest I seem to be able to do is to do things like:
    Qt Code:
    1. onForceOfflineChanged: {
    2. console.log ("onForceOfflineChanged= " + forceOffline)
    3.  
    4. }
    To copy to clipboard, switch view to plain text mode 
    QT Quick is very different to what I have been used to.

    Thank you for reading this.
    Last edited by anda_skoa; 1st September 2015 at 21:52. Reason: changed [quote] to [code]

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Very basic question on how to setup a QT quick checkbox.

    The problem you encounter is that the imperative code inside checkbox assigns a value to the checked property.
    This assignment overwrites the property binding you had initially set on that property.

    You have a couple of options (none of which are particularily pretty):

    1) define a signal in CheckBox and send that signal instead of changing the checked property.
    In the signal handler toggle the forceOffline property

    2) keep CheckBox as it is and restore the binding when it is overwritten

    Qt Code:
    1. CheckBox {
    2. id: offlineCheckBox
    3. checked: root.forceOffline
    4.  
    5. onCheckedChanged: {
    6. root.forceOffline = checked;
    7. checked = Qt.binding(function() { return root.forceOffline; });
    8. }
    9. }
    To copy to clipboard, switch view to plain text mode 

    3) using a Connections element instead of the original binding
    Qt Code:
    1. CheckBox {
    2. id: offlineCheckBox
    3. checked: root.forceOffline
    4.  
    5. onCheckedChanged: root.forceOffline = checked
    6. Connections {
    7. target: root
    8. onForceOfflineChanged: parent.checked = root.forceOffline
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 

    And probably more.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    philp (3rd September 2015)

  4. #3
    Join Date
    May 2011
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Very basic question on how to setup a QT quick checkbox.

    Thank you for your help anda_skoa; I really appreciate your help. I tried your second solution and it worked beautifully . As soon as I get enough time I will try the other solutions out as well. I have a lot to learn about QT quick and I am still confused about how the binding works. Initially i thought that they work a bit like reference variables...in that if one property changes then the other will also. Would you be so kind as to explain a little more about your statement
    The problem you encounter is that the imperative code inside checkbox assigns a value to the checked property.
    This assignment overwrites the property binding you had initially set on that property.
    I assume that the original binding is "checked: root.forceOffline" as specified when the CheckBox is first created i.e. in the code
    Qt Code:
    1. CheckBox {
    2. id: offlineCheckBox
    3. checked: root.forceOffline
    4. }
    To copy to clipboard, switch view to plain text mode 

    Is it the line "root.checked = !root.checked;" that destroys this binding when inside of the CheckBox item (Module)?
    I am really sorry if my question is very basic and has totally a misunderstanding of bindings.
    Thanks again very much for your time

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Very basic question on how to setup a QT quick checkbox.

    Quote Originally Posted by philp View Post
    Initially i thought that they work a bit like reference variables...in that if one property changes then the other will also.
    That is actually not a bad way to look at it.
    A property binding is just even more advanced since it can contain references to several other properties as part of the binding expression.


    Quote Originally Posted by philp View Post
    I assume that the original binding is "checked: root.forceOffline" as specified when the CheckBox is first created i.e. in the code
    Qt Code:
    1. CheckBox {
    2. id: offlineCheckBox
    3. checked: root.forceOffline
    4. }
    To copy to clipboard, switch view to plain text mode 
    Yes, that I what I meant.

    Quote Originally Posted by philp View Post
    Is it the line "root.checked = !root.checked;" that destroys this binding when inside of the CheckBox item (Module)?
    Yes, you are correct.

    Basically you are writing the "checked" property in two places, once during setup of the element and once during execution of the onClicked signal handler.

    If we forget for a moment that the initial value is not just a fixed value but a binding, we can look at this like having two places in C++ code writing to the same member variable.
    After the second write that value is the current one and any knowledge about any earlier value is "lost".

    The problems here are that
    (a) this is not very visible (one write happening inside the component)
    and
    (b) the original "value" being a binding which we know would be updating the property automatically

    So the assignment executed by the onClicked signal handler does not only change the value (which would be OK, we want it to do that), but it also removes this "auto-update" feature and we want to keep that.

    Quote Originally Posted by philp View Post
    I am really sorry if my question is very basic and has totally a misunderstanding of bindings.
    Thanks again very much for your time
    Not at all!
    This is one of the trickier parts of QML and even experienced developers hit it now and then, sometimes not recognizing it immediately.

    Cheers,
    _

    P.S.: in case you haven't discovered it yet: the QtQuick.Controls module provides standard UI components such as a check box

Similar Threads

  1. Basic Qt Sql Question
    By sidenelen in forum Newbie
    Replies: 8
    Last Post: 31st March 2012, 13:47
  2. basic C++ question
    By Maluko_Da_Tola in forum Newbie
    Replies: 3
    Last Post: 24th August 2010, 08:29
  3. very basic Qt/c++ question
    By Maluko_Da_Tola in forum Newbie
    Replies: 2
    Last Post: 25th July 2010, 14:02
  4. A few basic question
    By salmanmanekia in forum Newbie
    Replies: 12
    Last Post: 17th June 2010, 07:46
  5. Basic question
    By giacomelli.fabio in forum Newbie
    Replies: 4
    Last Post: 18th December 2009, 00:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.