QScrollBar *scrollBar = new QScrollBar(Qt::Horizontal, this); // create a horizontal scrollbar scrollBar->setRange(0, 100); // set the minimum and maximum values of the scrollbar scrollBar->setValue(50); // set the initial value of the scrollbar
QVBoxLayout *layout = new QVBoxLayout(this); QScrollBar *scrollBar = new QScrollBar(Qt::Vertical, this); // create a vertical scrollbar QLabel *label = new QLabel("Scroll Value: 0", this); layout->addWidget(scrollBar); layout->addWidget(label); connect(scrollBar, &QScrollBar::valueChanged, [=]() { int value = scrollBar->value(); label->setText("Scroll Value: " + QString::number(value)); });In the first example, we create a horizontal scrollbar, set its minimum and maximum values, and set its initial value to 50. In the second example, we create a vertical scrollbar and a label to display its value. We connect the scrollbar's valueChanged signal to a lambda function that reads the scrollbar's value and updates the label text accordingly. The Qt package library provides the QScrollBar class.