Results 1 to 2 of 2

Thread: QML Tab and State

  1. #1
    Join Date
    Jul 2017
    Posts
    11
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default QML Tab and State

    Good afternoon,

    I am developing an application using QML (Qt 5.8) where a TabView is used to control the different forms used. To reduce the number of Tab elements, I am using State in some tabs to control minor differences in the visible Tab content (depending on a test result for instance). I do assign a default state in each Tab, with state transitions are based on a Timer (declared in the Tab) or external test result. This approach works well for cases where a Timer (or external test) is started after a Tab is made current, and using onStateChanged().

    Note that the TabView.tabsVisible is set to false.

    There is however a case that is failing. The external test is executed in the background while Tab A is current. Depending on the test result Tab B becomes current (test passed), otherwise Tab C is selected to be current, with additional items specific to the test failure. The display the additional items is based on State (specific to Tab C). I have included a partial definition of Tab C

    Qt Code:
    1. Item {
    2. id: sensorIntegrityTest
    3.  
    4. onEnabledChanged:
    5. {
    6. console.log( "sensorIntegrityTest.Item.onEnabledChanged, visible: ", visible, ", enabled: ", enabled)
    7. }
    8. onVisibleChanged:
    9. {
    10. console.log( "sensorIntegrityTest.Item.onVisibleChanged, visible: ", visible, ", enabled: ", enabled)
    11. }
    12.  
    13. property int imageShown : 0
    14.  
    15. states: [
    16. State {
    17. name: "integrity setup"
    18. PropertyChanges { target: sensorIntegrityTestFail; visible: false }
    19. PropertyChanges { target: sensorIntegrityTestPass; visible: false }
    20. },
    21. State {
    22. name: "integrity test"
    23. PropertyChanges { target: sensorIntegrityTestFail; visible: false }
    24. PropertyChanges { target: sensorIntegrityTestPass; visible: false }
    25. },
    26. State {
    27. name: "integrity failed"
    28. PropertyChanges { target: sensorIntegrityTestPass; visible: false }
    29. PropertyChanges { target: sensorIntegrityTestFail; visible: true }
    30. },
    31. State {
    32. name: "integrity passed"
    33. PropertyChanges { target: sensorIntegrityTestFail; visible: false }
    34. PropertyChanges { target: sensorIntegrityTestPass; visible: true }
    35. }
    36.  
    37. ]
    38. onStateChanged:
    39. {
    40. console.log( "sensorIntegrityTest.onStateChanged: state= ", state)
    41. if ("integrity test" === state) {
    42. integrityTestTimer.start()
    43. } else {
    44. integrityTestTimer.stop()
    45. }
    46. }
    47.  
    48. // Assign the default state
    49. state: "integrity setup"
    To copy to clipboard, switch view to plain text mode 

    I am using a JavaScript function to select the current Tab. When Tab C is selected the following content appears in the console log:
    Qt Code:
    1. qml: sensorIntegrityTest.onStateChanged: state= integrity setup
    To copy to clipboard, switch view to plain text mode 
    When trying the read the state property value from the Javascript function using the command
    Qt Code:
    1. console.log( "state ", tabView.getTab( nextIndex).sensorIntegrityTest.state)
    To copy to clipboard, switch view to plain text mode 
    the following error is reported: qrc:/screen.js:208: TypeError: Cannot read property 'state' of undefined

    What am I missing? Is it possible to access the state property as currently declared here from a Javascript function?

    My last attempt to address this issue, I defined an alias for the state variable in Tab C definition of using the statement
    Qt Code:
    1. property alias integrityState: sensorIntegrityTest.state
    To copy to clipboard, switch view to plain text mode 
    resulting in the following error reported by the Javascript functions: qrc:/screen.js:211: Error: Cannot assign to non-existent property "integrityState"

    Regards,
    Daniel

  2. #2
    Join Date
    Jul 2017
    Posts
    11
    Qt products
    Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: QML Tab and State

    Hi again,

    I got something working and I thought I would share how I proceeded.

    In my application, the active/visible Tab is selected programmatically. As stated in the original message, the content of the Tab is dependent on the result/state of an external test. That test might be completed before the Tab is made visible.

    So, I first defined a set of enumerators in C++, made available to my QML code by using Q_ENUM macro and used Q_PROPERTY to define a variable in C++ that can be accessed in QML. Let's refer to this variable as testResult.

    I also used a set of (internal) State in my QML Tab content (see original post). In the QML file defining the Tab content, I added a Component.onCompleted which is used to the internal State based on the value of testResult. Once the Tab is visible, different controls (BUTTON) are used to let the operator select different operations, repeating the execution of the test for example, setting a new value to the internal State.

    Using QML State allows me to take advantage of onStateChange, and consequently set the visible content of the Tab according to the internal State.

    Communication between C++ and QML when the value of testResult changes is achieved using an emit notification.

    This solution avoids exposing internal QML State values and the variable testResult to the javascript code I am using.

    Thanks,
    Daniel

Similar Threads

  1. Change state property within a current state?
    By prophet0 in forum Qt Quick
    Replies: 1
    Last Post: 30th May 2012, 20:23
  2. State Machine returning to previous state
    By akiross in forum Qt Programming
    Replies: 2
    Last Post: 10th May 2011, 01:39
  3. Replies: 0
    Last Post: 19th December 2010, 17:03
  4. saving the state of the ui
    By zgulser in forum Qt Programming
    Replies: 3
    Last Post: 24th March 2009, 13:17
  5. QProcess state
    By patcito in forum Qt Programming
    Replies: 1
    Last Post: 17th January 2006, 20:56

Tags for this Thread

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.