Results 1 to 2 of 2

Thread: Code improvement for yes/no popup message

  1. #1
    Join Date
    Apr 2014
    Posts
    53
    Thanks
    9

    Default Code improvement for yes/no popup message

    II have created my own yes-no popup dialog (because QtQuick2 doesn't have one) and I would like to ask if the code I made can be improved. I am asking because I am new in QML and I have the suspicion that I didn't engineer a good solution. So I would like your opinions.

    This is the YesNoMsg.qml , that contains the popup:

    Qt Code:
    1. import QtQuick 2.7
    2. import QtQuick.Controls 2.0
    3.  
    4. Popup {
    5. id: yesno_msg_popup
    6. property string question
    7. signal yesno_yes_pressed()
    8. signal yesno_no_pressed()
    9.  
    10. Column {
    11. Label {
    12. id: yesno_msg_popup_title
    13. text: question
    14. }
    15. Text {
    16. id: yesno_msg_popup_text
    17. }
    18. Row {
    19. Button {
    20. id: yesno_msg_popup_yes_btn
    21. text: "Yes"
    22. onClicked: {
    23. yesno_msg_popup.close()
    24. yesno_msg_popup.yesno_yes_pressed()
    25. }
    26. }
    27. Button {
    28. id: yesno_msg_popup_no_btn
    29. text: "No"
    30. onClicked: {
    31. yesno_msg_popup.close()
    32. yesno_msg_popup.yesno_no_pressed()
    33. }
    34. }
    35. }
    36. }
    37.  
    38. }
    To copy to clipboard, switch view to plain text mode 


    And this is the code where I am using it:

    Qt Code:
    1. Button {
    2. text: "test dialog"
    3. onClicked: {
    4. yes_no_msg.question="yes or no?"
    5. yes_no_msg.open()
    6. yes_no_msg.yesno_yes_pressed.connect(yes)
    7. yes_no_msg.yesno_no_pressed.connect(no)
    8. }
    9. function yes() {
    10. console.log("yes")
    11. yes_no_msg.yesno_yes_pressed.disconnect(yes)
    12. yes_no_msg.yesno_no_pressed.disconnect(no)
    13. }
    14. function no() {
    15. console.log("no");
    16. yes_no_msg.yesno_yes_pressed.disconnect(yes)
    17. yes_no_msg.yesno_no_pressed.disconnect(no)
    18. }
    19. }
    20. YesNoMsg {
    21. id: yes_no_msg
    22. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, I have to connect() and disconnect() everytime I use the yesno dialog and this is not very comfortable thing to do.

  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: Code improvement for yes/no popup message

    You could just use the usual "on" syntax for signal handlers in the YesNoMsg element

    Qt Code:
    1. YesNoMsg {
    2. onYesNo_no_pressed:
    3. }
    To copy to clipboard, switch view to plain text mode 

    Though I would recommend to also use more "normal" signal names, e.g. yesClicked() and noClicked()
    The QtQuick.Controls 1.2 MessageDialog calls them yes() and no()

    Maybe look at that for other inspiration as well http://doc.qt.io/qt-5/qml-qtquick-di...agedialog.html

    Cheers,
    _

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

    nuliknol (24th November 2016)

Similar Threads

  1. Suggestions for improvement
    By ritikaemily in forum General Discussion
    Replies: 0
    Last Post: 23rd October 2013, 11:15
  2. Replies: 12
    Last Post: 1st May 2011, 11:38
  3. Crypto++ How do I use a message authentication code ?
    By Thành Viên Mới in forum General Programming
    Replies: 5
    Last Post: 5th August 2010, 10:22
  4. Replies: 7
    Last Post: 18th May 2010, 12:49
  5. Replies: 3
    Last Post: 17th May 2009, 20:17

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.