Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to toggle setText when a pushButton is clicked?
Forum Updated to NodeBB v4.3 + New Features

How to toggle setText when a pushButton is clicked?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    frost
    wrote on last edited by
    #1

    I've checked some threads for the answer, but most of them are about initialization. My question is, for example, one button which has been setText("More"), and I want to change the text "More" to "Less" when it is clicked.
    !http://v1.freep.cn/3tb_140327113756d0n6512293.png!
    Thanks you all.

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qxoz
      wrote on last edited by
      #2

      @void on_pushButton_Clicked()
      {
      state != state;
      if(state)
      {
      ui->pushButton->setText(QStringLiteral("More"));
      }
      else
      {
      ui->pushButton->setText(QStringLiteral("Less"));
      }
      }@
      like this? If i do not understand you correctly please explain more.

      ps: i can't see attached image.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        frost
        wrote on last edited by
        #3

        thans for your kind reply. In fact I want to implement this with a signal-slot, like this.
        @QObject::connect(moreButton, SIGNAL(clicked()), moreButton, SLOT(setText("Less")));@
        but the error output is like this,
        @QObject::connect: No such slot QPushButton::setText("Less")
        QObject::connect: (sender name: 'moreButton')
        QObject::connect: (receiver name: 'moreButton')
        QXcbWindow: Unhandled client message: "_GTK_LOAD_ICONTHEMES"
        @
        [quote author="qxoz" date="1395891816"]@void on_pushButton_Clicked()
        {
        state != state;
        if(state)
        {
        ui->pushButton->setText(QStringLiteral("More"));
        }
        else
        {
        ui->pushButton->setText(QStringLiteral("Less"));
        }
        }@
        like this? If i do not understand you correctly please explain more.

        ps: i can't see attached image.[/quote]

        1 Reply Last reply
        0
        • Q Offline
          Q Offline
          qxoz
          wrote on last edited by
          #4

          For passing data to slots you will need "functors":http://qt-project.org/wiki/New_Signal_Slot_Syntax .
          but if i understand your issue, it is not solution for you. On click you can pass always one value. If you want change text on every click then you need some slot as i wrote.

          1 Reply Last reply
          0
          • F Offline
            F Offline
            frost
            wrote on last edited by
            #5

            I think I've got what you said. The solution you promoted means "When click, it changes to "Less", on next click, it changes to "More" again." Thank you, I will try it out.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved