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. Multiple "valueChanged"-events in QSlider
QtWS25 Last Chance

Multiple "valueChanged"-events in QSlider

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 9.5k 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
    Sohoquetaros
    wrote on last edited by
    #1

    Hello,

    I noticed some strange behavior with the valueChanged event in QSlider (I'm currently porting from QT3 where I didn't have those problems).

    When I click on an arrow to increment or decrement the slider by one, I receive a valueChanged event. So far so good. But when I do some lengthy computing in a method receiving this event, I will get another valueChanged event - just as If I would have been on the arrow with the mouse for a longer time.

    Is there any way to inhibit this behavior? Maybe some property not to react on a mouse held on the arrow?

    Any help would be appreciated.

    Thanks a lot

    1 Reply Last reply
    0
    • D Offline
      D Offline
      Dii
      wrote on last edited by
      #2

      The complicated way is to catch the mouse events, let's skip this. The not so complicated is to block the signal during your process. Like this:

      @void yourapp::sliderchanged(int)
      {
      ui->yourslider->blocksignals(true);

      ... long process ...

      ui->yourslider->blocksignals(false); // back to normal
      }@

      Another possibility is to use somewhere (maybe at the start) in your function the

      @qApp->processEvents();@

      where your computing will stop for a bit to receive all application events.

      Hope this helps.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sohoquetaros
        wrote on last edited by
        #3

        Thanks a lot,

        "blockSignals" solves the problem.
        Thank you for your fast answer !

        1 Reply Last reply
        0
        • I Offline
          I Offline
          ivan
          wrote on last edited by
          #4

          The problem with blocking signals is that it blocks the later ones - that is, if you slide from 1 to 10, it will process 1 although (I guess) the more important one would be 10.

          If this is undesired, you can use delayed processing of some sorts - add a timer (for eg QBasicTimer) and start it when you get sliderchanged event. Set the delay to be around 100ms.

          When you get the last signal, no new ones will appear in the next 100ms and you can freely process the single event. (this is something that is usually done when people implement incremental search)

          Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
          Qt Ambassador (from the old Nokia days)

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Sohoquetaros
            wrote on last edited by
            #5

            Mmh, that's a point.
            But I read in the QT-documentation, that QBasicTimer is just for internal use and QTimer should be used instead of.

            I try it with QTimer::singleShot with 100ms delay. That should be fine too.

            1 Reply Last reply
            0
            • I Offline
              I Offline
              ivan
              wrote on last edited by
              #6

              Since it is public API, it is meant to be used. QTimer is recommended, that is true, but for the basic delay this is (IMO) a better option since it does exactly what is needed with much less overhead.

              Ivan Čukić | ivan.cukic(at)kde.org | KDE e.V.
              Qt Ambassador (from the old Nokia days)

              1 Reply Last reply
              0
              • T Offline
                T Offline
                tzander
                wrote on last edited by
                #7

                As a general point it is important to avoid doing something in the event handling that takes a lot of time exactly for this kind of problems.

                It will block your applications repainting as well because spending time in your calculation at every event will mean you will have less time to repaint.

                This is important since the signal you respond to; valueChanged() is emitted before the result of that is painted on screen. So the user will not see the slider move until your calculation is done. If the user moves around the mouse a lot this can cause the repaint to not happen for various seconds. That will look ugly :)

                The solution is to make your slot return very quickly; it can be something as simple as
                <pre>
                m_timer.stop(); //stop previous running timer
                m_timer.start(); // handle the change in 800ms or so
                </pre>

                Then your timer can read the current value from the slider and do its calculation at most only once per timeout. Making sure that the screen keeps updated in between.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sohoquetaros
                  wrote on last edited by
                  #8

                  Well, what shoud I say?

                  Thanks for so much help!
                  It really solves the problem.

                  1 Reply Last reply
                  0

                  • Login

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