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. slowing down a Listwidget Verticalscrollbar
Forum Updated to NodeBB v4.3 + New Features

slowing down a Listwidget Verticalscrollbar

Scheduled Pinned Locked Moved Unsolved General and Desktop
listwidgetscrollbar
1 Posts 1 Posters 168 Views
  • 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.
  • S Offline
    S Offline
    supratik123
    wrote on 10 Sept 2024, 07:29 last edited by VRonin 9 Oct 2024, 14:21
    #1

    I am using a listwidget vertical scrollbar to have an alarm clock kind of thing, where i can set values of minutes and seconds. The problem i am facing is the scroll bar is very fast even with trackpad of laptop as well as the touch screen device on which my code is deployed. It reaches 0 -30 at one scroll.
    i tried to catch the slider released signal and slow it down but the signal is not getting emitted.
    all i want is to slow the scroller down when user release the scroller on screen.

    Attaching my code

    listWidget1 = new QListWidget(this);
            //listWidget1->setVerticalScrollMode(QAbstractItemView::ScrollMode::ScrollPerPixel);
            QFont number1Font("DINCond-Bold",28,QFont::Medium);
            //$$
            QFont userScroller("DINCond-Bold",28,QFont::Medium);
            for(int i=0; i<30; i++)
            {
                QListWidgetItem *item;
                if(i < 10)
                    item = new QListWidgetItem("0" + QString::number(i));
                else
                    item = new QListWidgetItem(QString::number(i));
    
                item->setFont(userScroller);
                listWidget1->addItem(item);
                item->setTextAlignment(Qt::AlignRight);
                if(i == 1)
                    item->setFont(userScroller);
    
            }
            listWidget1->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
            double scalingFactor = 0.5; // Reduce the scrolling speed by half
            int itemIndex = static_cast<int>(listWidget1->verticalScrollBar()->value() * scalingFactor / listWidget1->sizeHintForRow(0));
            listWidget1->setCurrentRow(itemIndex);
            listWidget1->verticalScrollBar()->setSingleStep(1);
            listWidget1->setFocusPolicy(Qt::NoFocus);
            listWidget1->setSelectionMode(QAbstractItemView::NoSelection);
    
    
            //$$
            listWidget1->setStyleSheet("background:rgba(0,0,0,0.025);border-radius : 0px;border:0px;color: rgba(255, 255, 255, 0.2);");
            listWidget1->setSpacing(25);
    
    
            listWidget1->show();
            listWidget1->setAutoScroll(false);
            listWidget1->setDragEnabled(false);
            listWidget1->setAcceptDrops(false);
    
            qDebug() << "before list widget 2";
    
            listWidget2 = new QListWidget(this);
            for(int i=0; i<60; i++)
            {
                QListWidgetItem *item;
                if(i < 10)
                    item = new QListWidgetItem("0" + QString::number(i));
                else
                    item = new QListWidgetItem(QString::number(i));
                listWidget2->addItem(item);
                item->setFlags(Qt::ItemIsEnabled);
                //$$
                item->setFont(userScroller);
                item->setTextAlignment(Qt::AlignLeft);
            }
    
            listWidget1->setAttribute(Qt::WA_AcceptTouchEvents,true);
            listWidget2->setFocusPolicy(Qt::NoFocus);
            listWidget2->setSelectionMode(QAbstractItemView::NoSelection);
            listWidget1->setFixedHeight(350);
    
            listWidget2->setAttribute(Qt::WA_AcceptTouchEvents,true);
    
            QScroller::grabGesture(listWidget1, QScroller::LeftMouseButtonGesture);
            QScroller::grabGesture(listWidget2, QScroller::LeftMouseButtonGesture);
            // QScroller::grabGesture(listWidget1, QScroller::ScrollerGestureType::TouchGesture);
            // QScroller::grabGesture(listWidget2, QScroller::ScrollerGestureType::TouchGesture);
    
            listWidget2->setFixedHeight(350);
        
            connect(listWidget1->verticalScrollBar(), &QScrollBar::sliderReleased, [=]() {
            qDebug() << "User released the slider in listWidget1";
    
            //     // Adjust QScroller properties for slow deceleration after release
                 QScroller *scroller = QScroller::scroller(listWidget1->viewport());
            /    QScrollerProperties scrollerProperties = scroller->scrollerProperties();
    
               // Slow down scrolling when the user releases the slider
                scrollerProperties.setScrollMetric(QScrollerProperties::DecelerationFactor, 0.05);  // Slow deceleration
                scrollerProperties.setScrollMetric(QScrollerProperties::MaximumVelocity, 0.3);      // Reduce max velocity
              scroller->setScrollerProperties(scrollerProperties);
            });
    
             connect(listWidget1->verticalScrollBar(), &QScrollBar::actionTriggered, this, [=](int action) {
                 qDebug() << "Action triggered: " << action;
    
                 if (action == QAbstractSlider::SliderMove) {
                     // Get the current position of the slider
                     int currentPos = listWidget1->verticalScrollBar()->sliderPosition();
    
                    // Apply a deceleration factor (adjust according to the desired slowdown effect)
                    double decelerationFactor = 0.9; // Slow down by 50%
                    int slowedPosition = static_cast<int>(currentPos * decelerationFactor);
    
                     // Set the slowed position (smaller increments make the scroll feel slower)
                     listWidget1->verticalScrollBar()->setSliderPosition(slowedPosition);
    
                     qDebug() << "Slowed position: " << slowedPosition;
                 }
             });
            QHBoxLayout *listWidgetLayout1 = new QHBoxLayout();
            listWidgetLayout1->addWidget(listWidget1);
            listWidgetLayout1->addWidget(scrollBar1);
    
            QHBoxLayout *listWidgetLayout2 = new QHBoxLayout();
            listWidgetLayout2->addWidget(listWidget2);
            listWidgetLayout2->addWidget(scrollBar2);
    
            listWidgetLayout4 = new QHBoxLayout();
            listWidgetLayout4->addLayout(listWidgetLayout1);
            listWidgetLayout4->addLayout(listWidgetLayout2);
    
    1 Reply Last reply
    0

    1/1

    10 Sept 2024, 07:29

    • Login

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