Connect Dynamically created Sliders and Labels (Solved)



  • 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"


  • Lifetime Qt Champion

    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



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


  • Lifetime Qt Champion

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



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



  • 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?



  • 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()));
    }

    @


  • Lifetime Qt Champion

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



  • 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.



  • thx SGaist that acutally works!


  • Lifetime Qt Champion

    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 :)


Log in to reply
 

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