Nominate our 2022 Qt Champions!

how to draw a custom slider which can be moved by mouse as the Qt intrinsic slider?

  • how to draw a custom slider which can be moved by mouse as the Qt intrinsic slider?
    i already use QPainter draw some additional effect on the slider-bar.

  • Qt Champions 2017

    Is it repeat of the question ?

    As explained earlier, you need to draw things on your own using the paintEvent(..) & handle the mouse events appropriately. Please look at the topics how to write customWidgets.

  • yes.
    i used QGraphicsView before.
    but this time seems no need to use QGraphicsView, and now i am wondering how to draw the slider and how to move it by the mouse
    thank you very much

  • Qt Champions 2017

    You can do it in graphics view also. Again I'm repeating the following steps.

    1. Look at how custom widgets are created.
    2. re-impliment the paintEvent(...) function.
    3. Draw slider, handler, bar everything on your own.
    4. re-implement mousePress, mouseRelease events.

    Look at the examples I have sent earlier. You are asking the code of the custom slider which you wanted. This will will be very difficult. Here people will help you with specific problem.

  • ok,i just wonder if not use QGraphicsView, just use the QPainter, is it more simple and easy?
    and can u simply give a hint to to move the slider by the mouse while not use QGraphicsView? check the mouse pos in the slider-rect?

  • Qt Champions 2017

    1. Draw your slider handle in side the paintEvent(..) function.
    2. Handle mouse press, release and move function.
    3. Change the x, y position of your handle inside the mouse event handlers.
    4. Call paintEvent(...) again by calling update(..)

    Look at the pathStroke example. It may help you.

  • Lifetime Qt Champion

    Just as a note.
    QtCreator comes with something like the gradient thing.
    alt text
    You could look around its code to see how its done.

Log in to reply