TextLabel and Buttons



  • Hello all,

    i have a Button (checkable) and a TextLabel ("checked" and "uncheked").
    When thu Button is checked I want that the TextLabel "cheked" appears and when the Button is unchecked, the TextLabel "uncheked" appears.

    I tryed it like this:
    @MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    {
    ui->setupUi(this);

    ui->label_1->hide();
    ui->label_2->hide();
    
    if (ui->pushButton->isChecked()) {
        ui->label1-show();
    }
    else
    ui->label_2->show();
    

    @

    But it doesnt work.
    Can someone help me please?



  • What does not work? What does it do?

    Why don't you connect the triggered signal of the button to your slot where you just change the text of the label or of course you can hide/show different labels as well.



  • Hi,

    Without using "signal/slot mechanism":http://qt-project.org/doc/qt-5.0/qtcore/signalsandslots.html, you will always have label1 showed. You should do something like this instead:
    @
    connect(ui->pushButton, SIGNAL(toggled(bool)), this, SLOT(updateLabel(bool)));

    void MainWindow::updateLabel(bool _is_checked)
    {
    if(_is_checked)
    {
    ui->label->setText(tr("Checked"));
    }
    else
    {
    ui->label->setText(tr("Unchecked"));
    }
    }
    @



  • EDIT:
    @tilsitt Thank you very much. It works :)

    [quote author="butterface" date="1383732104"]What does not work? What does it do?

    Why don't you connect the triggered signal of the button to your slot where you just change the text of the label or of course you can hide/show different labels as well.[/quote]

    for Example:

    When Button cheked:
    !http://s14.directupload.net/images/131106/oix7bshd.jpg(Bild)!

    When uncheked:
    !http://s1.directupload.net/images/131106/6zuqc43n.jpg(picture)!

    @tilsitt
    Thanks, I would try it now.


Log in to reply
 

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