+ Reply to Thread
Results 1 to 2 of 2

Thread: Changing the text of a button

  1. #1
    Join Date
    Jan 2016
    Posts
    45
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Changing the text of a button

    Hi,

    Using this code in a QML program I want to change the text of a button when clicked:

    main.qml:
    Qt Code:
    1. import QtQuick 2.9
    2. import QtQuick.Window 2.2
    3. import QtQuick.Controls 1.4
    4. import QtQuick.Controls.Styles 1.4
    5.  
    6. Window {
    7. visible: true
    8. width: 720; height: 720
    9. color: "gray"
    10.  
    11. Rectangle {
    12. id: root
    13. x: 10; y: 10
    14. width: 300; height: 300
    15. color: "lightblue"
    16.  
    17. function change() {
    18. st_text.text = "Clicked"
    19. }
    20.  
    21. Button {
    22. id: b1
    23. x: 100; y: 100
    24. style: ButtonStyle {
    25. background: Rectangle {
    26. implicitHeight: 50
    27. implicitWidth: 100
    28. color: "lightGrey"
    29. border.width: 4
    30. border.color: "lightBlue"
    31. radius: 15
    32. Text {
    33. id: st_text
    34. anchors.centerIn: parent
    35. text: "Start"
    36. font.bold: true
    37. font.pixelSize: 15
    38. color: "green"
    39. }
    40. }
    41. }
    42. onClicked: root.change()
    43. }
    44. }
    45. }
    To copy to clipboard, switch view to plain text mode 

    But when I click the button I get this error:
    qrc:/main.qml:18: ReferenceError: st_text is not defined

    Why and how to solve that please?

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    3,700
    Thanks
    212
    Thanked 566 Times in 556 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Changing the text of a button

    Maybe try moving the definition of your function change() to -after- the declaration of Button. I don't know much QML, but it may require that ids must be defined before they can be used. st_text isn't defined until after Button is declared.

    If that isn't it, then maybe st_text is ambiguous, and you need to qualify it as "b1.st_text".
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

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

    franky (4th January 2018)

+ Reply to Thread

Similar Threads

  1. QSS for changing the width of the drop-down button
    By lf460 in forum Qt Programming
    Replies: 0
    Last Post: 3rd August 2015, 04:10
  2. Changing text of button in no relation to button
    By Sabre Runner in forum Newbie
    Replies: 22
    Last Post: 23rd September 2010, 12:29
  3. Replies: 6
    Last Post: 21st August 2010, 21:09
  4. changing color of push button
    By babu198649 in forum Newbie
    Replies: 1
    Last Post: 2nd March 2008, 12:55

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