Modify a QLabel with a QSlider



  • Hello !

    I'm started to use Qt and I did some programs to test many things like a QSlider which is moving a QProgressBar or a button to open a file (by example). However, I wanted to try something else, but I don't succeed.

    I want to write a program with a QLabel and a QSlider in a QWidget. In the QLabel, there is written "I am X years old !" and the X is an int that can be changed by the QSlider. By example, if I move the QSlider to 50, there is written "I am 50 years old !" etc ...

    Is there someone to help me with a simple function to do this ?


  • Moderators

    @Meugiwara

    void onSliderValueChangedSlot(int value)
    {
        label->setText( QString("I am %1 years old").arg(value);
    }
    

    connect this slot to the sliders valueChanged signal.



  • If I don't use class, how can I add your code in the main function ?

    /* Référence à un fichier include */
    #include "main.h"
    
    /* Fonction principale pour les essais */
    int     main(int argc, char **argv)
    {
        /* Exécuter un programme Qt */
        QApplication    program(argc, argv);
        /* Inscrire une fenêtre dans le programme Qt */
        QWidget         window;
        /* Mise en page intérieure des widgets de façon verticale */
        QVBoxLayout     layout;
        /* Les boutons "Open" et "Close" */
        QPushButton     obutton("Open");
        QPushButton     cbutton("Close");
        /* Le fichier désiré à ouvrir */
        QString         file;
        /* Le widget slider */
        QSlider         slider;
        /* La barre de progression */
        QProgressBar    bar;
        /* La phrase */
        QLabel          label;
    
        /* Titre de la fenêtre */
        window.setWindowTitle("BASELFI v1.0");
        /* Taille de la fenêtre */
        window.setFixedSize(800, 600);
        /* Boutons dans la fenêtre */
        layout.addWidget(&obutton);
        layout.addWidget(&cbutton);
        /* Slider dans la fenêtre */
        layout.addWidget(&slider);
        /* Barre dans la fenêtre */
        layout.addWidget(&bar);
        /* La phrase */
        layout.addWidget(&label);
        /* Lier une action à chaque bouton */
        QObject::connect(&obutton, &QPushButton::clicked, [&window, &file] {file = QFileDialog::getOpenFileName(&window, QObject::tr("Sélectionner un fichier audio ..."), "C:/", QObject::tr("Audio (*.mp3)"));});
        QObject::connect(&cbutton, SIGNAL(clicked()), &program, SLOT(quit()));
        /* Lier la barre de progression au slider */
        QObject::connect(&slider, &QSlider::valueChanged, &bar, &QProgressBar::setValue);
        /* Modifier la phrase avec le slider */
        QObject::connect(&slider, &QSlider::valueChanged, &label, &QLabel::setText(QString("I am %1 years old !").arg(int)));
        /* Inscrire les widgets dans la fenêtre */
        window.setLayout(&layout);
        /* Montrer la fenêtre */
        window.show();
        /* Exécution de la fenêtre */
        return program.exec();
    }
    

    Sorry, I forgot my code.


  • Moderators

    @Meugiwara said in Modify a QLabel with a QSlider:

    If I don't use class, how can I add your code in the main function ?

    This is only possible if you are compiling with a C++11-compatible compiler.

    connect(&slider, &QSlider::valueChanged, [label](int value) {
      label.setText( QString("I am %1 years old").arg(value);
    });
    


  • Thank you a lot and I have just a little question about the QLabel. At the kick-off of the program, the QLabel doesn't appear, but if I move the QSlider, I can see it. How do you do to see everytime the QLabel ?


  • Qt Champions 2016

    @Meugiwara
    Hi
    Try call setText on it just after its created.



  • I wrote this :

    label.setText(QString("I am 0 years old !"));
    

    This is like an initialization because the counter begins at 0. Thank you !


Log in to reply
 

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