user9205320
user9205320

Reputation:

How can i make the QSlider indicate the current value

I can't figure out from the QT Assistant how to set the QSlider to show the current value while dragging, I came with this idea initially:

QLabel* lblYear = new QLabel();
QSlider* sldYear = new QSlider(Qt::Horizontal);

QObject::connect(sldYear, &QSlider::valueChanged, [&]() {
    lblYear->setText(sldYear->value);
});

But it doesn't work:

'QAbstractSlider::value': non-standard syntax; use '&' to create a pointer to member

I don't know if the label idea is ok, and I don't mind changing it. The only requirement is to see the value of the slider at any time.

Upvotes: 0

Views: 1468

Answers (1)

vtronko
vtronko

Reputation: 488

QObject::connect(sldYear, &QSlider::valueChanged, [](int value) {
    lblYear->setText(QString::number(value));
});

You need to convert int value to QString to be able to use it with setText. Also, capturing pointer by reference is not always safe and it also doesn't make sense, since it is as cheap as capture it by value. Moreover, in this case you don't even need to, you get the value from the signal interface.

Upvotes: 2

Related Questions