Results 1 to 6 of 6

Thread: pass data between qt controls (slider) and C++ application

  1. #1
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default pass data between qt controls (slider) and C++ application

    Hello ,

    I am facing some confusion regarding passing data between the qt qick slider and QQuickItem object in C++, that results in application crash. I have the following class with the defined properties:

    Qt Code:
    1. class TessellationSceneItem : public QQuickItem
    2. {
    3. Q_OBJECT
    4. //the property defintion must be at the top of the class
    5. //the notify signal is needed for correct property bindings
    6. Q_PROPERTY(float outer READ outer WRITE setOuter NOTIFY outerChanged)
    7. Q_PROPERTY(float inner READ inner WRITE setInner NOTIFY innerChanged)
    8. Q_PROPERTY(float maxPatchVertices READ maxPatchVertices NOTIFY maxPatchVerticesChanged)
    9.  
    10. public:
    11. TessellationSceneItem();
    12.  
    13. void setOuter(float);
    14. void setInner(float);
    15.  
    16. float inner();
    17. float outer();
    18.  
    19. float maxPatchVertices() const;
    20.  
    21. signals:
    22. void innerChanged();
    23. void outerChanged();
    24. void maxPatchVerticesChanged();
    25. .............
    26. ............
    27. private:
    28.  
    29. TeapotTessellation* mTessScene;
    30.  
    31. QTime mTIme;
    32.  
    33. };
    To copy to clipboard, switch view to plain text mode 

    The definition of the setter and getter methods:

    Qt Code:
    1. void TessellationSceneItem::setOuter(float outerFactor)
    2. {
    3. if(outerFactor == mTessScene->outerTessellationFactor())
    4. return;
    5.  
    6. mTessScene->setOuterTessellationFactor(outerFactor);
    7. //do not emit notifier if value does not actually change
    8. emit outerChanged();
    9. }
    10.  
    11. void TessellationSceneItem::setInner(float innerFactor)
    12. {
    13. if(innerFactor == mTessScene->innerTessellationFactor())
    14. return;
    15.  
    16. mTessScene->setInnerTessellationFactor(innerFactor);
    17. emit innerChanged();
    18. }
    19.  
    20. float TessellationSceneItem::inner()
    21. {
    22. return (float)(mTessScene->innerTessellationFactor());
    23. }
    24.  
    25. float TessellationSceneItem::outer()
    26. {
    27. return (float)(mTessScene->outerTessellationFactor());
    28. }
    29.  
    30. float TessellationSceneItem::maxPatchVertices() const
    31. {
    32. return (float) (mTessScene->getMaxPatchVertices());
    33. }
    To copy to clipboard, switch view to plain text mode 

    The idea is to have the slider value loaded with the value set in the underlying C++ application. Then with every slider interaction , the underlying value( outer, inner) in the C++ side will be updated. I tried the following in the .qml file , but the application crashes.

    Qt Code:
    1. import QtQuick 2.0
    2. import QtQuick.Controls 1.1
    3. import QtQuick.Layouts 1.1
    4. import TeapotTessellation 1.0
    5.  
    6. Item {
    7. id: root
    8. width: 512; height: 512
    9.  
    10.  
    11. TessellationSceneItem
    12. {
    13. id: tessSceneItem
    14. // outer: outerTessellationSlider.value
    15. // inner: innerTessellationSlider.value
    16. }
    17.  
    18. GridLayout {
    19. id: controlLayout
    20. columns: 2
    21.  
    22. Text {
    23. id: outerTessellationText
    24. text: qsTr("Outer Tess. Factor: ")
    25. color: "black"
    26. opacity: 1.0
    27. }
    28.  
    29. Slider {
    30. id: outerTessellationSlider
    31. opacity: 1.0
    32. // value: tessSceneItem.outer
    33. minimumValue: 1.0
    34. maximumValue: 64//tessSceneItem.maxPatchVertices
    35. stepSize: 1.0
    36. Layout.fillWidth: true
    37. }
    38.  
    39.  
    40. Text {
    41. id: innerTessellationText
    42. text: qsTr("Inner Tess. Factor: ")
    43. color: "black"
    44. opacity: 1.0
    45. }
    46.  
    47.  
    48.  
    49. Slider {
    50. id: innerTessellationSlider
    51. opacity: 1.0
    52. // value: tessSceneItem.inner
    53. minimumValue: 1.0
    54. maximumValue: 64.0//tessSceneItem.maxPatchVertices
    55. stepSize: 1.0
    56. Layout.fillWidth: true
    57. }
    58. }
    59.  
    60.  
    61. Rectangle {
    62. id: rect
    63. color: "red"
    64. radius: 10
    65. opacity: 0.1
    66. border.color: "black"
    67. anchors.fill: controlLayout
    68. }
    69. }
    To copy to clipboard, switch view to plain text mode 

    The commented part of the .qml file are the places that cause the application crash. Any hint ?

    Thanks

  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: pass data between qt controls (slider) and C++ application

    Did the crash backtrace contain any information on where it crashes?
    Does it work when you assign fixed values in QML?
    DId you try with having matching types for your properties and the slider's value property?

    Cheers,
    _

  3. #3
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: pass data between qt controls (slider) and C++ application

    Quote Originally Posted by anda_skoa View Post
    Did the crash backtrace contain any information on where it crashes?
    It depends which of the propetties I am working with. For example I am working with the follwoing property:

    Qt Code:
    1. Q_PROPERTY(float inner READ inner WRITE setInner NOTIFY innerChanged)
    To copy to clipboard, switch view to plain text mode 

    Then inside the .qml file I have the following snippet:

    Qt Code:
    1. TessellationSceneItem
    2. {
    3. id: tessSceneItem
    4.  
    5. inner: 10
    6. }
    To copy to clipboard, switch view to plain text mode 

    I get the crashing point while running with debugger at the following points:

    Qt Code:
    1. void TessellationSceneItem::setInner(float innerFactor)
    2. {
    3. if(innerFactor == mTessScene->innerTessellationFactor())
    4. return;
    5.  
    6. mTessScene->setInnerTessellationFactor(innerFactor);
    7. emit innerChanged();
    8. }
    9.  
    10. ..............................
    11. ..............................
    12.  
    13. GLfloat TeapotTessellation::innerTessellationFactor()
    14. {
    15. return mInnerTessellationFactor;
    16. }
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by anda_skoa View Post
    Does it work when you assign fixed values in QML?
    The issue prevails.

    Quote Originally Posted by anda_skoa View Post
    DId you try with having matching types for your properties and the slider's value property?
    I did not understand this part.

    Thanks

  4. #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: pass data between qt controls (slider) and C++ application

    Quote Originally Posted by sajis997 View Post
    I get the crashing point while running with debugger at the following points:

    Qt Code:
    1. void TessellationSceneItem::setInner(float innerFactor)
    2. {
    3. if(innerFactor == mTessScene->innerTessellationFactor())
    4. return;
    5.  
    6. mTessScene->setInnerTessellationFactor(innerFactor);
    7. emit innerChanged();
    8. }
    9.  
    10. ..............................
    11. ..............................
    12.  
    13. GLfloat TeapotTessellation::innerTessellationFactor()
    14. {
    15. return mInnerTessellationFactor;
    16. }
    To copy to clipboard, switch view to plain text mode 
    You forgot to mark the line where the crash happens.


    Quote Originally Posted by sajis997 View Post
    I did not understand this part.
    Your properties have a different type (float) than the slider's value property (qreal).

    Cheers,
    _

  5. #5
    Join Date
    Jan 2011
    Posts
    212
    Thanks
    24
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: pass data between qt controls (slider) and C++ application

    The issue is resolved. It was trying to access the class member functions without even being instantiated at the first time. I have one question though , when does the Qt Property System gets initialized ? right after the call of the class constructor ?

    What if the property system do have to access some read-only value after some other classes initialization ? Is it possible to make the Qt property system to be inintialized in lazy manner ?

    Because I need to access some opengl related value that depends on the opengl driver installed and then pass that value to the qml slider maximumvalue

    Now it seems that the Qt property system gets the initialization call before the opengl initialization at the back-end ? Any idea to get it working ?

    Qt Code:
    1. //the property defintion must be at the top of the class
    2. //the notify signal is needed for correct property bindings
    3. Q_PROPERTY(float outer READ outer WRITE setOuter NOTIFY outerChanged)
    4. Q_PROPERTY(float inner READ inner WRITE setInner NOTIFY innerChanged)
    5. Q_PROPERTY(float maxPatchVertices READ maxPatchVertices NOTIFY maxPatchVerticesChanged)
    To copy to clipboard, switch view to plain text mode 

  6. #6
    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: pass data between qt controls (slider) and C++ application

    Quote Originally Posted by sajis997 View Post
    I have one question though , when does the Qt Property System gets initialized ? right after the call of the class constructor ?
    The property system doesn't need any initialization, it is based on invoking member functions which are callable as soon as an object instance has been created.

    Quote Originally Posted by sajis997 View Post
    What if the property system do have to access some read-only value after some other classes initialization ? Is it possible to make the Qt property system to be inintialized in lazy manner ?
    You can change a property's value at any time, all bindings on that property will get re-evaluated.

    Quote Originally Posted by sajis997 View Post
    Now it seems that the Qt property system gets the initialization call before the opengl initialization at the back-end ? Any idea to get it working ?
    As there is no "initializtion" of the property system, just make sure that the getter of the property returns the correct value once it is available.
    Make sure you have emitted the property's change notifiier signal when you need QML to update the bindings for the new value.

    Cheers,
    _

Similar Threads

  1. Extract data from db then pass it to qtreeview
    By nethanjavier in forum Newbie
    Replies: 5
    Last Post: 24th July 2012, 10:45
  2. Best way to pass data between Windows and Linux
    By deepakn in forum General Programming
    Replies: 1
    Last Post: 28th December 2011, 12:40
  3. Pass data from QT to javascript function
    By thanhluanbk88 in forum Qt Programming
    Replies: 3
    Last Post: 3rd March 2011, 10:40
  4. Replies: 5
    Last Post: 1st March 2010, 15:55
  5. How to disable 3 controls on the main application
    By sarode in forum Qt Programming
    Replies: 9
    Last Post: 30th January 2007, 12:37

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.