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. Connect Dynamically created Sliders and Labels (Solved)
Forum Updated to NodeBB v4.3 + New Features

Connect Dynamically created Sliders and Labels (Solved)

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 7.3k 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.
  • H Offline
    H Offline
    hypnomaki
    wrote on last edited by
    #1

    Im creating QSliders and QLabels in my Program and i want to connect them so when I'm changing the Slider the Value should be shown within the label.

    So i thought about something like this

    @
    Qlabel label;
    QSlider silder;
    connect(slider, SIGNAL(valueChanged(int)), this, ChangeText(slider, label));

    void ChangeText(&slider, &label)
    {
    label.setText(Slider.Value);
    }@

    but this wont work.
    The Compiler tells me "invalid use of void expression"

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      There are several things that are wrong with your code:
      Your ChangeText function signature is invalid
      Your connect statement is wrong.
      If you have put that code as is in a cpp file then it won't even compile

      You should check the "Signals And Slots":http://doc.qt.io/qt-5/signalsandslots.html chapter of Qt's documentation

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rondog
        wrote on last edited by
        #3

        I am not aware of any way to pass a pointer of the widget when the slot is called. There might be but I am not aware of it.

        If you have only a few slider-label combinations you could have a slot for each. Example:
        @
        change_text_slider1(int){;}
        change_text_slider2(int){;}
        // etc.
        @

        You could create a subclass of the slider. With a subclass you can emit your own signals (which can include a pointer or some other identifying information).

        @
        void MySlider::value_changed(int value)
        {
        int id=(identifier, maybe pointer,?);

        emit Value_Changed(id, value);
        }
        @

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          "QSignalMapper":http://doc.qt.io/qt-5/qsignalmapper.html would probably help for that purpose

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Rondog
            wrote on last edited by
            #5

            Thanks SGaist. Although this is not my thread I wasn't aware of QSignalMapper. Learned something new.

            1 Reply Last reply
            0
            • H Offline
              H Offline
              hypnomaki
              wrote on last edited by
              #6

              Rondog, I cant create So many Slots because i dont know how many sliders are going to be created.

              SGaist, this looks good, I just had a look at it but its not as easy.
              I tried:
              @
              QSlider *slider;
              QLabel *label;
              QSignalMapper *mapper = new QSignalMapper(this);
              connect(slider, SIGNAL(valueChanged(int)), mapper, SIGNAL(mapped(int)));
              connect(mapper,SIGNAL(mapped(int)),this,SIGNAL(LabelSlider(label,slider)));
              @
              @
              void Dashboard::LabelSlider(QLabel *label, QSlider *slider)
              {
              label->setText(QString::number(slider->value()));
              }
              @
              Its compiling but it doesnt work. What am I doing wrong?

              1 Reply Last reply
              0
              • R Offline
                R Offline
                Rondog
                wrote on last edited by
                #7

                From what I read it looks like you need to do something like this:

                @
                signalMapper = new QSignalMapper(this);
                ...
                connect(slider, SIGNAL(valueChanged(int)), signalMapper, SLOT(map()));
                signalMapper->setMapping(slider, slider); // not sure?
                ...
                connect(signalMapper, SIGNAL(mapped(QSlider*)),this, SLOT(value_changed(QSlider*)));

                ...

                void Dashboard::value_changed(QSlider *slider)
                {
                label->setText(QString::number(slider->value()));
                }

                @

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Not exactly no. You can't modify signal/slots signature like that.

                  @
                  signalMapper = new QSignalMapper(this);
                  connect(slider, SIGNAL(valueChanged(int)), signalMapper, SLOT(map()));
                  signalMapper->setMapping(slider, label);
                  connect(signalMapper, SIGNAL(mapped(QWidget*)),this, SLOT(onMapped(QWidget*)));

                  void Dashboard:: onMapped(QWidget *widget)
                  {
                  QSlider slider = qobject_cast<QSlider>(signalMapper->mapping(widget));
                  QLabel label = qobject_cast<QLabel>(widget);
                  if (label && slider) {
                  label->setText(QString::number(slider->value()));
                  }
                  }
                  @

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Rondog
                    wrote on last edited by
                    #9

                    I was close I guess (hope that counts for something - lol).

                    This opens up a few possiblities for me. I had problems like this in the past and never realized this class existed. It can make a few things easier.

                    1 Reply Last reply
                    0
                    • H Offline
                      H Offline
                      hypnomaki
                      wrote on last edited by
                      #10

                      thx SGaist that acutally works!

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Well, I try not to post buggy code ;)

                        Since it's all working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        • Login

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