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. Modify a QLabel with a QSlider

Modify a QLabel with a QSlider

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 4.2k 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.
  • M Offline
    M Offline
    Meugiwara
    wrote on last edited by
    #1

    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 ?

    raven-worxR 1 Reply Last reply
    0
    • M Meugiwara

      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 ?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Meugiwara

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

      connect this slot to the sliders valueChanged signal.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      2
      • M Offline
        M Offline
        Meugiwara
        wrote on last edited by Meugiwara
        #3

        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.

        raven-worxR 1 Reply Last reply
        0
        • M Meugiwara

          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.

          raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by raven-worx
          #4

          @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);
          });
          

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          2
          • M Offline
            M Offline
            Meugiwara
            wrote on last edited by
            #5

            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 ?

            mrjjM 1 Reply Last reply
            0
            • M Meugiwara

              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 ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              1 Reply Last reply
              1
              • M Offline
                M Offline
                Meugiwara
                wrote on last edited by
                #7

                I wrote this :

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

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

                1 Reply Last reply
                2

                • Login

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