Results 1 to 16 of 16

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

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

    I am working on QML app with Qt/C++ "back" logic. Now, I've created class named UeStatus, which resemble the state of app (which users are logged in, database connection succesfull or not, ...):
    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 
    As you can see, I've also created dedicated class/enum for describing database connection state:
    Qt Code:
    1. #ifndef UEDATABASECONNECTIONSTATUS
    2. #define UEDATABASECONNECTIONSTATUS
    3.  
    4. #include <QObject>
    5.  
    6. class UeDatabaseConnectionStatus : public QObject
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. enum UeTypeDatabaseConnectionStatus
    12. {
    13. NOT_CONNECTED=false,
    14. CONNECTED=true
    15. };
    16.  
    17. Q_ENUM(UeTypeDatabaseConnectionStatus)
    18. };
    19.  
    20. #endif // UEDATABASECONNECTIONSTATUS
    To copy to clipboard, switch view to plain text mode 
    Now, I register class UeDatabaseConnectionStatus inside main.cpp:
    Qt Code:
    1. #include <QtQml>
    2. #include <QApplication>
    3. #include <QQmlApplicationEngine>
    4.  
    5. #include "database/uepeoplemodel.h"
    6. #include "core/uestatus.h"
    7. #include "core/uedatabaseconnectionstatus.h"
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication app(argc, argv);
    12. QQmlApplicationEngine engine;
    13.  
    14. UeStatus* ueApplicationStatus=new UeStatus(qApp);
    15. UePeopleModel* uePeopleModel=new UePeopleModel(qApp);
    16.  
    17. QObject::connect(uePeopleModel,
    18. SIGNAL(ueSignalDatabaseConnectionChanged(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus)),
    19. ueApplicationStatus,
    20. SLOT(ueSlotDatabaseConnectionChanged(UeDatabaseConnectionStatus::UeTypeDatabaseConnectionStatus)));
    21.  
    22. uePeopleModel->ueConnectToDatabase();
    23.  
    24. engine.rootContext()->setContextProperty("uePeopleModel",
    25. uePeopleModel);
    26. engine.rootContext()->setContextProperty("ueApplicationStatus",
    27. ueApplicationStatus);
    28. engine.addImageProvider(QLatin1String("uePeopleModel"),
    29. uePeopleModel);
    30.  
    31. qmlRegisterUncreatableType<UeDatabaseConnectionStatus>("si.test",
    32. 1,
    33. 0,
    34. "UeTypeDatabaseConnectionStatus",
    35. "Database Connection Status");
    36.  
    37. engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    38.  
    39. return app.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 
    and the GUI (QML) is defined in main.qml:
    Qt Code:
    1. import QtQuick 2.4
    2. import QtQuick.Controls 1.3
    3. import QtQuick.Window 2.2
    4. import QtQuick.Dialogs 1.2
    5. import QtQuick.Layouts 1.0
    6. import QtQuick.Controls.Styles 1.4
    7.  
    8. import "gui/windows"
    9. import "gui/items"
    10.  
    11. import si.mikroelektronika 1.0
    12.  
    13. ApplicationWindow
    14. {
    15. id: ueWindowMain
    16.  
    17. signal ueSignalDatabaseConnectionChanged()
    18.  
    19. title: qsTr("uBlagajna Mobile Client ver 1.00")
    20.  
    21. width: Screen.desktopAvailableWidth
    22. height: Screen.desktopAvailableWidth
    23.  
    24. visible: true
    25.  
    26. opacity: 1.0
    27.  
    28. contentOrientation: Qt.LandscapeOrientation
    29.  
    30. color: "black"
    31.  
    32. UeKeypad
    33. {
    34. id: ueLoginKeypad
    35. } // ueLoginKeypad
    36.  
    37. statusBar:
    38. StatusBar
    39. {
    40. id: ueStatusBar
    41.  
    42. height: 96
    43.  
    44. clip: true
    45.  
    46. antialiasing: true
    47.  
    48.  
    49. style:
    50. StatusBarStyle
    51. {
    52. background:
    53. Rectangle
    54. {
    55. color: "black"
    56. } // Rectangle
    57.  
    58. panel:
    59. Rectangle
    60. {
    61. color: "grey"
    62. }
    63. } // StatusBarStyle
    64.  
    65. RowLayout
    66. {
    67. spacing: 8
    68.  
    69. UeStatusIndicator
    70. {
    71. id: ueStatusIndicatorDatabaseConnected
    72.  
    73. ueParamImageStatusOn: "qrc:///ueIcons/icons/ueDbConnectionOk.png"
    74. ueParamImageStatusOff: "qrc:///ueIcons/icons/ueDbConnectionError.png"
    75. } // ueStatusIndicatorDatabaseConnected
    76. } // RowLayout
    77. } // ueStatusBar
    78. } // ueWindowMain
    To copy to clipboard, switch view to plain text mode 
    UeStatusIndicator.qml is item that containts two images according to its state:
    Qt Code:
    1. import QtQuick 2.0
    2.  
    3. Item
    4. {
    5. id: ueStatusIndicator
    6.  
    7. property string ueParamImageStatusOn
    8. property string ueParamImageStatusOff
    9.  
    10. state: "ueStatusIndicatorDabaseNotConnected"
    11.  
    12. Image
    13. {
    14. id: ueStatusIndicatorCurrentImage
    15.  
    16. smooth: true
    17.  
    18. fillMode: Image.PreserveAspectFit
    19.  
    20. width: 96
    21. height: 96
    22.  
    23. sourceSize.width: 96
    24. sourceSize.height: 96
    25. } // Image
    26.  
    27. Connections
    28. {
    29. } // Connections
    30.  
    31. states:
    32. [
    33. State
    34. {
    35. name: "ueStatusIndicatorDabaseConnected"
    36.  
    37. PropertyChanges
    38. {
    39. target: ueStatusIndicatorCurrentImage
    40. source: ueParamImageStatusOn
    41. } // PropertyChanges
    42. }, // State
    43.  
    44. State
    45. {
    46. name: "ueStatusIndicatorDabaseNotConnected"
    47.  
    48. PropertyChanges
    49. {
    50. target: ueStatusIndicatorCurrentImage
    51. source: ueParamImageStatusOff
    52. } // PropertyChanges
    53. } // State
    54. ] // states
    55. } // Item
    To copy to clipboard, switch view to plain text mode 
    Now, how do I catch signal void ueSignalDatabaseConnectionChanged(const UeDatabaseConnectionStatus::UeTypeDatabaseConnecti onStatus& newStatus); or slot void ueSlotDatabaseConnectionChanged(const UeDatabaseConnectionStatus::UeTypeDatabaseConnecti onStatus& newStatus); inside ueStatusIndicator?
    Sincerely,
    Marko
    Last edited by MarkoSan; 17th September 2015 at 08:53. Reason: updated contents
    Qt 5.3 Opensource & Creator 3.1.2

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

    Quote Originally Posted by MarkoSan View Post
    Now, how do I catch signal void ueSignalDatabaseConnectionChanged(const UeDatabaseConnectionStatus::UeTypeDatabaseConnecti onStatus& newStatus); or slot void ueSlotDatabaseConnectionChanged(const UeDatabaseConnectionStatus::UeTypeDatabaseConnecti onStatus& newStatus); inside ueStatusIndicator?
    Why would you want to catch the signal?
    The connection status is a property on one of the exported objects, you can simply bind to its value.

    Cheers,
    _

  3. #3
    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
    Why would you want to catch the signal?
    The connection status is a property on one of the exported objects, you can simply bind to its value.

    Cheers,
    _
    I would like to change database connection in QML StatusBar from one representing connection error to one representing connection OK if connection gets screwed and so I can reconnect inside connection dropped slot.
    Qt 5.3 Opensource & Creator 3.1.2

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

    I am not sure I understand.

    My assumption is that you want to change the value of "state" when the connection status changes.
    But since "state" is a property it can be bound to an expression containing other properties (property binding), in this case the connection status property of the ueApplicationStatus object.

    What JavaScript code do you want to execute on connection status change?

    Cheers,
    _

  5. #5
    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
    I am not sure I understand.

    My assumption is that you want to change the value of "state" when the connection status changes.
    But since "state" is a property it can be bound to an expression containing other properties (property binding), in this case the connection status property of the ueApplicationStatus object.

    What JavaScript code do you want to execute on connection status change?

    Cheers,
    _
    Ok, let's go from start: When the user starts app, the app is in NOT_CONNECTED state and therefore in QML's StatusBar there is icon, which represents NOT_CONNECTED state. Then, the app tries to connect to mysql server and when it does connect, the responsible object emits signal to listeners notifiyng them database connection succesfully opened - fetching data available. At this point, on QML side I need to change database connection icon from icon representing NOT_CONNECTED state to icon representing CONNECTED state. Do you understand now?

    Sincerely,
    Marko
    Qt 5.3 Opensource & Creator 3.1.2

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

    Yes, I already understood that

    Lets have a look at the code you posted:

    - You have a class named UeStatus
    - It has a property called m_ueDatabaseConnectionStatus (which, btw, is a very uncommon name for a property, m_ is usually used to designate member variables)
    - You export an object of that class as ueApplicationStatus

    Now, my assumption is that m_ueDatabaseConnectionStatus is updated then the connection status changes.
    Is that correct?

    If so, you can simply make "state" depend on the value of m_ueDatabaseConnectionStatus.
    You could even make the image source depend on that in remove the states, but maybe you want to do more settings in each state than just the image source.

    Cheers,
    _

  7. #7
    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
    Yes, I already understood that

    Lets have a look at the code you posted:

    - You have a class named UeStatus
    - It has a property called m_ueDatabaseConnectionStatus (which, btw, is a very uncommon name for a property, m_ is usually used to designate member variables)
    - You export an object of that class as ueApplicationStatus

    Now, my assumption is that m_ueDatabaseConnectionStatus is updated then the connection status changes.
    Is that correct?

    If so, you can simply make "state" depend on the value of m_ueDatabaseConnectionStatus.
    You could even make the image source depend on that in remove the states, but maybe you want to do more settings in each state than just the image source.

    Cheers,
    _
    Ok, but how do I access m_ueDatabaseConnectionStatus from QML?
    Qt 5.3 Opensource & Creator 3.1.2

  8. #8
    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

    Consider the first argument to setContextProperty() as equivalent to a QML "id" of the object.
    I think you've already done that with the model.

    Cheers,
    _

  9. #9
    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
    Consider the first argument to setContextProperty() as equivalent to a QML "id" of the object.
    I think you've already done that with the model.

    Cheers,
    _
    Ok, so there is no way to handle this via signal/slot, since this is what am I trying to do (this code does NOT work) - file UeStatusIndicator.qml, which is called from main.qml:
    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. signal ueSignalDatabaseConnectionChanged(UeTypeDatabaseConnectionStatus status) // from UeStatus
    13.  
    14. state: "ueStatusIndicatorDabaseNotConnected"
    15.  
    16. Image
    17. {
    18. id: ueStatusIndicatorCurrentImage
    19.  
    20. smooth: true
    21.  
    22. fillMode: Image.PreserveAspectFit
    23.  
    24. width: 96
    25. height: 96
    26.  
    27. sourceSize.width: 96
    28. sourceSize.height: 96
    29. } // Image
    30.  
    31. Connections
    32. {
    33. target: ueStatusIndicator
    34.  
    35. onUeSignalDatabaseConnectionChanged:
    36. {
    37. state=(status===UeTypeDatabaseConnectionStatus.CONNECTED):
    38. "ueStatusIndicatorDabaseConnected"?
    39. "ueStatusIndicatorDabaseNotConnected"
    40. }
    41. } // Connections
    42.  
    43. states:
    44. [
    45. State
    46. {
    47. name: "ueStatusIndicatorDabaseConnected"
    48.  
    49. PropertyChanges
    50. {
    51. target: ueStatusIndicatorCurrentImage
    52. source: ueParamImageStatusOn
    53. } // PropertyChanges
    54. }, // State
    55.  
    56. State
    57. {
    58. name: "ueStatusIndicatorDabaseNotConnected"
    59.  
    60. PropertyChanges
    61. {
    62. target: ueStatusIndicatorCurrentImage
    63. source: ueParamImageStatusOff
    64. } // PropertyChanges
    65. } // State
    66. ] // states
    67. } // Item
    To copy to clipboard, switch view to plain text mode 
    Last edited by MarkoSan; 17th September 2015 at 14:59. Reason: updated contents
    Qt 5.3 Opensource & Creator 3.1.2

  10. #10
    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,
    _

  11. #11
    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

  12. #12
    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,
    _

  13. #13
    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

  14. #14
    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,
    _

  15. #15
    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

  16. #16
    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,
    _

  17. 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, 12:05
  2. Catching exceptions with Qt
    By The_Fallen in forum Qt Programming
    Replies: 6
    Last Post: 30th July 2010, 20:39
  3. Replies: 1
    Last Post: 30th July 2010, 08:23
  4. Qt - catching interrupts
    By rishid in forum Qt Programming
    Replies: 2
    Last Post: 5th February 2008, 15:17
  5. Catching X Events
    By nupul in forum Qt Programming
    Replies: 3
    Last Post: 16th April 2006, 13: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.