Results 1 to 11 of 11

Thread: RadioButtons with TextSwitch and Signal/Slot

  1. #1
    Join Date
    Apr 2014
    Posts
    116
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    8

    Default RadioButtons with TextSwitch and Signal/Slot

    Hi there,

    I am writing an App for Sailfish OS. Currently radio buttons are not available and I hope to rebuild their functionality with TextSwitches and signals but I am doing something wrong.
    Qt Code:
    1. SilicaListView {
    2. width: parent.width
    3. height: parent.height - header.height
    4. anchors.top: header.bottom
    5. spacing: Theme.paddingLarge
    6. VerticalScrollDecorator {}
    7.  
    8. model: playlistModel
    9.  
    10. delegate: ListItem {
    11. height: 50
    12. width: parent.width
    13.  
    14. TextSwitch {
    15. id: playlistSwitch
    16. property string tempUrl: url
    17.  
    18. signal gotChecked (string plName)
    19.  
    20. text: playlistName
    21. onCheckedChanged: {
    22. if(checked){
    23. m_url = tempUrl
    24. m_playlistName = text
    25. gotChecked(playlistName)
    26. }else{
    27. tempUrl = ""
    28. m_playlistName = ""
    29. }
    30. }
    31. onGotChecked: {
    32. if(plName !== text)
    33. checked = false
    34. }
    35. }
    36. }
    37. }
    To copy to clipboard, switch view to plain text mode 
    It looks like the signal is emitted but only heard within the TextSwitch that send the signal. How can I get the other TextSwitches within my listview to listen?

    Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: RadioButtons with TextSwitch and Signal/Slot

    You are only using the signal locally, within one instance.

    Put the signal into the list view or a any other object addressable from the delegates or use a property in that object, e.g. a "current item/index" indicator.

    Cheers,
    _

  3. #3
    Join Date
    Apr 2014
    Posts
    116
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    8

    Default Re: RadioButtons with TextSwitch and Signal/Slot

    I already tried that. If I put the signal gotChecked (string plName) directly under ListView nothing happens. I think the signal is not emitted. Also if I type onGo Qt Creator does not autocomplete for me. Did I declare the signals wrong?

    Thanks for helping!

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: RadioButtons with TextSwitch and Signal/Slot

    Not sure what you mean with not emitted, in JavaScript a signal is called like a function, see your current code.

    To react to a signal outside the current instance you can use a Connections element:
    Qt Code:
    1. ListView {
    2. id: view
    3. signal gotChecked(string name);
    4.  
    5. delegate: Switch {
    6. Connections {
    7. target: view
    8. onGotChecked: ....
    9. }
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    But a "current selected index" property of some kind would likely be way more declarative

    Cheers,
    _

  5. #5
    Join Date
    Apr 2014
    Posts
    116
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    8

    Default Re: RadioButtons with TextSwitch and Signal/Slot

    Hi _,

    I tried your code but got a ReferenceError: gotChecked is not defined

    I have no clue what you mean by current selected index. Does a ListView always have an index?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: RadioButtons with TextSwitch and Signal/Slot

    If you want a list of items where only one item can be selected then the usual approach is to keep the index of the selected item in the view and update that index from within an item:

    javascript Code:
    1. ListView {
    2. id: view
    3. model: ...
    4. property int selected: -1
    5.  
    6. delegate: Rectangle {
    7. id: delegateRoot
    8. width: view.width
    9. height: 50
    10. color: index === view.selected ? "red" : "white"
    11. MouseArea {
    12. anchors.fill:parent
    13. onClicked: view.selected = delegateRoot.index
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2014
    Posts
    116
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    8

    Default Re: RadioButtons with TextSwitch and Signal/Slot

    Thanks for the time you spent helping!
    I tried your approach but I get stuck at line 13. Error: Cannot assign [undefined] to int
    The index is a number I logged that so I really do not understand why this does not run...

    [edit]
    Sorry index is undefined!

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: RadioButtons with TextSwitch and Signal/Slot

    If it doesn't work directly then first assign index to a local property and then use that in the mouse area.

    javascript Code:
    1. Rectangle {
    2. id: delegateRoot
    3. property int myIndex: index
    4. MouseArea {
    5. onClicked: view.selected = delegateRoot.myIndex
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Apr 2014
    Posts
    116
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    8

    Default Re: RadioButtons with TextSwitch and Signal/Slot

    Hello wysota,

    thanks again for helping me out! I finally got it working. The problem was not assigning the index but binding the TextSwitch checked property to the selected index. Here is my final working code:
    Qt Code:
    1. SilicaListView {
    2. id: myListView
    3. width: parent.width
    4. height: parent.height - header.height
    5. anchors.top: header.bottom
    6. spacing: Theme.paddingLarge
    7. VerticalScrollDecorator {}
    8.  
    9. model: playlistModel
    10.  
    11. property int selected: -1
    12.  
    13. delegate: TextSwitch {
    14. id: playlistSwitch
    15. height: 50
    16. width: parent.width
    17. property string tempUrl: url.slice(7)
    18. property bool isChecked: (index === myListView.selected? playlistSwitch.checked = true:playlistSwitch.checked = false)
    19.  
    20. text: playlistName
    21.  
    22. onClicked: {
    23. if(playlistSwitch.checked){
    24. m_url = tempUrl
    25. m_playlistName = playlistName
    26. myListView.selected = index
    27. }else{
    28. tempUrl = ""
    29. m_playlistName = ""
    30. }
    31. }
    32. }
    33. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: RadioButtons with TextSwitch and Signal/Slot

    Quote Originally Posted by KeineAhnung View Post
    I tried your code but got a ReferenceError: gotChecked is not defined
    The solution with the property is way better, but for future reference: did you specifiy the view's id when "emitted/called" the signal?

    Qt Code:
    1. view.gotChecked(...);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  11. #11
    Join Date
    Apr 2014
    Posts
    116
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    8

    Default Re: RadioButtons with TextSwitch and Signal/Slot

    Yes, I think I tried all combinations of positioning the code and adding ids.

Similar Threads

  1. Replies: 1
    Last Post: 14th August 2014, 17:08
  2. Replies: 6
    Last Post: 4th March 2014, 15:09
  3. Replies: 8
    Last Post: 7th November 2012, 14:10
  4. How to add 2 column of RadioButtons in qtableview ?
    By smichaud in forum Qt Programming
    Replies: 1
    Last Post: 23rd July 2011, 08:52
  5. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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.