How to toggle setText when a pushButton is clicked?



  • 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.



  • @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.



  • 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]



  • 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.



  • 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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.