vendredi 26 février 2016

Qt signal slot with std::function

Hello I have a slider and a spinbox to control one object, so I need to update spinbox when user use slider and vice versa.

    QSlider* slider = new QSlider();
    QSpinBox* spin = new QSpinBox();

    std::function<void(int)> setValue = [=]( int value ){
        slider->blockSignals( true );
        slider->setValue( value );
        slider->blockSignals( true );
        spin->blockSignals( true );
        spin->setValue( value );
        spin->blockSignals( true );
        doSmthUsefull();
    };

    connect( slider, &QSlider::valueChanged,  setValue );
    connect( spin,   &QSpinBox::valueChanged, setValue );

But it's not working because compiler coudn't find function overload for connect :( At the same time without std::function it's works ok, but I don't want to declare 2 lambda in each connect

    connect( slider, &QSlider::valueChanged, [=]( int value ){
        slider->blockSignals( true );
        slider->setValue( value );
        slider->blockSignals( true );
        spin->blockSignals( true );
        spin->setValue( value );
        spin->blockSignals( true );
        doSmthUsefull();
    });

Aucun commentaire:

Enregistrer un commentaire