Results 1 to 16 of 16

Thread: Catching Qt/C++ signal from QML custom Item/code - howto

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: Catching Qt/C++ signal from QML custom Item/code - howto

    Quote Originally Posted by MarkoSan View Post
    Ok, so there is no way to handle this via signal/slot
    Of course there is, I just pointing out that in this case it is unnecessary since it can be done much more elegantly using a property binding.

    But if you insist on doing it the ugly way
    Qt Code:
    1. Connections
    2. {
    3. target: ueApplicationStatus
    4.  
    5. onUeSignalDatabaseConnectionChanged:
    6. {
    7. state=(status===UeTypeDatabaseConnectionStatus.CONNECTED) ?
    8. "ueStatusIndicatorDabaseConnected" : "ueStatusIndicatorDabaseNotConnected"
    9. }
    10. } // Connections
    To copy to clipboard, switch view to plain text mode 
    - you want to handle the signal of the ueApplicationStatus object (which has a nice property that you should be using instead)
    - the syntax for the conditional assignment is:
    Qt Code:
    1. condition ? valueIf : valueElse
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  2. #2
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: Catching Qt/C++ signal from QML custom Item/code - howto

    Quote Originally Posted by anda_skoa View Post
    Of course there is, I just pointing out that in this case it is unnecessary since it can be done much more elegantly using a property binding.

    But if you insist on doing it the ugly way
    Qt Code:
    1. Connections
    2. {
    3. target: ueApplicationStatus
    4.  
    5. onUeSignalDatabaseConnectionChanged:
    6. {
    7. state=(status===UeTypeDatabaseConnectionStatus.CONNECTED) ?
    8. "ueStatusIndicatorDabaseConnected" : "ueStatusIndicatorDabaseNotConnected"
    9. }
    10. } // Connections
    To copy to clipboard, switch view to plain text mode 
    - you want to handle the signal of the ueApplicationStatus object (which has a nice property that you should be using instead)
    - the syntax for the conditional assignment is:
    Qt Code:
    1. condition ? valueIf : valueElse
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _
    Hmm, now I have following code - UeStatus header:
    Qt Code:
    1. #ifndef UESTATUS_H
    2. #define UESTATUS_H
    3.  
    4. #include <QObject>
    5. #include <QList>
    6.  
    7. #include "../core/uetypes.h"
    8. #include "../core/uedatabaseconnectionstatus.h"
    9.  
    10. class UeStatus : public QObject
    11. {
    12. Q_OBJECT
    13.  
    14. Q_PROPERTY(UeTypeLoggedUsers* m_ueLoginCandidates
    15. READ ueLoginCandidates
    16. WRITE ueSetLoginCandidates
    17. NOTIFY ueSignalLoginCandidatesChanged)
    18. Q_PROPERTY(UeTypeLoggedUsers* m_ueLoggedUsers
    19. READ ueLoggedUsers
    20. WRITE ueSetLoggedUsers
    21. NOTIFY ueSignalLoggedUsersChanged)
    22. Q_PROPERTY(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus m_ueDatabaseConnectionStatus
    23. READ ueDbConnectionStatus
    24. WRITE ueSetDbConnectionStatus
    25. NOTIFY ueSignalDatabaseConnectionChanged)
    26.  
    27. private:
    28. UeTypeLoggedUsers* m_ueLoginCandidates;
    29. UeTypeLoggedUsers* m_ueLoggedUsers;
    30. UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus m_ueDatabaseConnectionStatus;
    31.  
    32. public:
    33. explicit UeStatus(QObject *parent = 0);
    34. ~UeStatus();
    35.  
    36. inline UeTypeLoggedUsers* ueLoginCandidates() const
    37. { return this->m_ueLoginCandidates; }
    38. inline UeTypeLoggedUsers* ueLoggedUsers() const
    39. { return this->m_ueLoggedUsers; }
    40. inline UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus ueDbConnectionStatus() const
    41. { return this->m_ueDatabaseConnectionStatus; }
    42.  
    43. inline void ueSetLoginCandidates(UeTypeLoggedUsers* const m_ueLoginCandidates)
    44. { this->m_ueLoginCandidates=m_ueLoginCandidates; }
    45. inline void ueSetLoggedUsers(UeTypeLoggedUsers* const loggedUsers)
    46. { this->m_ueLoggedUsers=loggedUsers; }
    47. inline void ueSetDbConnectionStatus(const UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus& status)
    48. { this->m_ueDatabaseConnectionStatus=status; }
    49.  
    50. signals:
    51. void ueSignalLoginCandidatesChanged();
    52. void ueSignalLoggedUsersChanged();
    53. void ueSignalDatabaseConnectionChanged(const UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus& newStatus);
    54.  
    55. public slots:
    56. void ueSlotDatabaseConnectionChanged(const UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus& newStatus);
    57. };
    58.  
    59. #endif // UESTATUS_H
    To copy to clipboard, switch view to plain text mode 
    and its implementation:
    Qt Code:
    1. #include "uestatus.h"
    2.  
    3. UeStatus::UeStatus(QObject *parent)
    4. : QObject(parent)
    5. {
    6. this->ueSetLoginCandidates(new UeTypeLoggedUsers());
    7. this->ueSetLoggedUsers(new UeTypeLoggedUsers());
    8. //this->ueSetDbConnectionStatus(NOT_CONNECTED);
    9. } // constructor
    10.  
    11. UeStatus::~UeStatus()
    12. {
    13. delete this->ueLoginCandidates();
    14. delete this->ueLoggedUsers();
    15. } // destructor
    16.  
    17. void UeStatus::ueSlotDatabaseConnectionChanged(const UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus& newStatus)
    18. {
    19. this->ueSetDbConnectionStatus(newStatus);
    20.  
    21. emit this->ueSignalDatabaseConnectionChanged(newStatus);
    22. } // ueSignalDatabaseConnectionChanged
    To copy to clipboard, switch view to plain text mode 
    and here is item:
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. import si.mikroelektronika 1.0
    4.  
    5. Item
    6. {
    7. id: ueStatusIndicator
    8.  
    9. property string ueParamImageStatusOn
    10. property string ueParamImageStatusOff
    11.  
    12. state: "ueStatusIndicatorDabaseNotConnected"
    13.  
    14. Image
    15. {
    16. id: ueStatusIndicatorCurrentImage
    17.  
    18. smooth: true
    19.  
    20. fillMode: Image.PreserveAspectFit
    21.  
    22. width: 96
    23. height: 96
    24.  
    25. sourceSize.width: 96
    26. sourceSize.height: 96
    27. } // Image
    28.  
    29. Connections
    30. {
    31. target: ueApplicationStatus
    32.  
    33. onUeSignalDatabaseConnectionChanged:
    34. {
    35. state=(newStatus===UeTypeDatabaseConnectionStatus.CONNECTED)?
    36. "ueStatusIndicatorDabaseConnected":
    37. "ueStatusIndicatorDabaseNotConnected"
    38. }
    39. } // Connections
    40.  
    41. states:
    42. [
    43. State
    44. {
    45. name: "ueStatusIndicatorDabaseConnected"
    46.  
    47. PropertyChanges
    48. {
    49. target: ueStatusIndicatorCurrentImage
    50. source: ueParamImageStatusOn
    51. } // PropertyChanges
    52. }, // State
    53.  
    54. State
    55. {
    56. name: "ueStatusIndicatorDabaseNotConnected"
    57.  
    58. PropertyChanges
    59. {
    60. target: ueStatusIndicatorCurrentImage
    61. source: ueParamImageStatusOff
    62. } // PropertyChanges
    63. } // State
    64. ] // states
    65. } // Item
    To copy to clipboard, switch view to plain text mode 
    and this code combo DOES NOT work, i.e., signal code does not get executed in UeStatusIndicator. What did we still miss?
    Qt 5.3 Opensource & Creator 3.1.2

  3. #3
    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: Catching Qt/C++ signal from QML custom Item/code - howto

    Try changing the signal's signature to have an int instead of the enum.

    Or use the property binding approach.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Question Re: Catching Qt/C++ signal from QML custom Item/code - howto

    Quote Originally Posted by anda_skoa View Post
    Try changing the signal's signature to have an int instead of the enum.

    Or use the property binding approach.

    Cheers,
    _
    Ok, but binding works only in QML=>C++ way, not vice versa, as seen in http://doc.qt.io/qt-5/qml-qtqml-binding.html#details, what I need is update QML element based on C++ class member value:
    Detailed Description

    Binding to an Inaccessible Property

    Sometimes it is necessary to bind to a property of an object that wasn't directly instantiated by QML - generally a property of a class exported to QML by C++. In these cases, regular property binding doesn't work. Binding allows you to bind any value to any property.

    For example, imagine a C++ application that maps an "app.enteredText" property into QML. You could use Binding to update the enteredText property like this.

    TextEdit { id: myTextField; text: "Please type here..." }
    Binding { target: app; property: "enteredText"; value: myTextField.text }
    and the last sentence in the mentioned docs states:
    Whenever the text in the TextEdit is updated, the C++ property will be updated also.
    Can you show me please what did you plan to do, since I need contra scenarium.

    Sincerely,
    Marko
    Qt 5.3 Opensource & Creator 3.1.2

  5. #5
    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: Catching Qt/C++ signal from QML custom Item/code - howto

    Quote Originally Posted by MarkoSan View Post
    Ok, but binding works only in QML=>C++ way, not vice versa
    No

    Quote Originally Posted by MarkoSan View Post
    Which is totally irrelevant here since you are not trying to bind a property of a C++ object to an expression.

    Quote Originally Posted by MarkoSan View Post
    what I need is update QML element based on C++ class member value
    Indeed, which is just a plain and simple property binding.
    The "state" property gets bound to an expression using the context property object's "connection status" property.

    Cheers,
    _

  6. #6
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Catching Qt/C++ signal from QML custom Item/code - howto

    Quote Originally Posted by anda_skoa View Post
    No


    Which is totally irrelevant here since you are not trying to bind a property of a C++ object to an expression.


    Indeed, which is just a plain and simple property binding.
    The "state" property gets bound to an expression using the context property object's "connection status" property.

    Cheers,
    _
    Can you give me example please?!?!?!
    Qt 5.3 Opensource & Creator 3.1.2

  7. #7
    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: Catching Qt/C++ signal from QML custom Item/code - howto

    You've done that numerous times already, I am sure
    Qt Code:
    1. state: ueApplicationStatus.m_ueDatabaseConnectionStatus === UeTypeDatabaseConnectionStatus.CONNECTED ?
    2. "ueStatusIndicatorDabaseConnected" : "ueStatusIndicatorDabaseNotConnected"
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

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

    MarkoSan (18th September 2015)

Similar Threads

  1. Catching signal from Mobile Dialpad buttons
    By baluk in forum Qt Programming
    Replies: 1
    Last Post: 27th September 2010, 11:05
  2. Catching exceptions with Qt
    By The_Fallen in forum Qt Programming
    Replies: 6
    Last Post: 30th July 2010, 19:39
  3. Replies: 1
    Last Post: 30th July 2010, 07:23
  4. Qt - catching interrupts
    By rishid in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2008, 14:17
  5. Catching X Events
    By nupul in forum Qt Programming
    Replies: 3
    Last Post: 16th April 2006, 12:43

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
  •  
Qt is a trademark of The Qt Company.