How to toggle the text on a button



  • In the book, "C++ GUI Programming with Qt4 (second edition)", at the end of the implementation of the sort extension dialog, the statement is made that it is easy to change the text on a button so that it shows "Advanced >>>" when only a basic dialog is visible and "Advanced <<<" when the extension is shown. I have tried to change the text on the button from "More" to "Less" when the extension is shown and back to "More" when only the basic dialog is shown, but have been unable to get this to work

    I have managed to get control to go to my code by connecting the "clicked" signal to my code, but for some reason my code is called more than once for each click, which screws up my algorithm that is trying to toggle the displayed text. I have also tried "released" and "pressed" signals, to no avail.

    Here is my code. I would appreciate any tips on how to get this to work, seeing that it is so "easy".
    Thank you.

    @
    #include <QtGui>

    #include "sortdialog.h"

    SortDialog::SortDialog(QWidget *parent)
    : QDialog(parent)
    {
    setupUi(this);

    secondaryGroupBox->hide();
    tertiaryGroupBox->hide();
    layout()->setSizeConstraint((QLayout::SetFixedSize));
    
    setColumnRange('A','Z');
    
    connect(moreButton,SIGNAL(clicked()),this,SLOT(on_moreButton_clicked()));
    

    }

    void SortDialog::setColumnRange(QChar first, QChar last)
    {
    primaryColumnCombo->clear();
    secondaryColumnCombo->clear();
    tertiaryColumnCombo->clear();

    secondaryColumnCombo->addItem(tr("None"));
    tertiaryColumnCombo->addItem(tr("None"));
    
    primaryColumnCombo->setMinimumSize(
            secondaryColumnCombo->sizeHint());
    
    QChar ch = first;
    while (ch <= last) {
        primaryColumnCombo->addItem(QString(ch));
        secondaryColumnCombo->addItem(QString(ch));
        tertiaryColumnCombo->addItem(QString(ch));
        ch = ch.unicode() + 1;
    }
    

    }

    void SortDialog::on_moreButton_clicked()
    {
    if (moreButton->text() == tr("More"))
    moreButton->setText(tr("Less"));
    else
    moreButton->setText(tr("More"));
    }

    @



  • Never mind; I figured out that I should be using the toggled signal instead. Now it works.


Log in to reply
 

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