Important: Please read the Qt Code of Conduct -

slider and plus/minus signs

  • i have a qslider object, on the left side of which i place "-" sign, on the right side a "+" sign. on the application they appear in separate "rectangles". what's the problem here? should i reimplement slider's paintEvent?

  • Lifetime Qt Champion

    How do you place them? QLabel?
    While you can overlay widgets to some degree, if you scale window , it often goes broke.
    So yes, painting theme will work better.

  • but how do i actually paint that? i thought this would suffice but no:

    void Slider::paintEvent(QPaintEvent *event) {
        QPainter painter(this);
        painter.setPen(QPen(Qt::SolidPattern, 1));
        painter.drawText(-5, height() / 2, QString("-"));
        painter.drawText(width() + 5, height() / 2, QString("+"));

  • Lifetime Qt Champion

    Try move
    to the top so base paints first and then you.
    else it might override.

    also drawText(-5, might not work as
    you cant paint outside the area of the widget.
    same with painter.drawText(width() + 5

    you cant have them inside the widget ?

  • that didn't help.

    what do you mean? have a container that contains qlabel, qslider, qlabel?

  • Lifetime Qt Champion

    No, i mean draw them inside the widget. You give it x values that
    is outside the clipping area. You cannot use negative value nor bigger
    than width() to draw anything. You cannot paint outside a widget.

    Maybe i misunderstood what you mean by
    "appear in separate "rectangles"
    Sounded like you did use a layout to make a composite widget but
    wanted the +/- to overlap the slider area.

    So do you want the +/- to be over the slider area or is right next to both side fine enough?

  • i want "- slider +" to appear as one widget

  • Lifetime Qt Champion


    Hehe i must ask in a stupid way :)
    So its ok that + and minus are right next to the slider and not over it ?
    If yes, you can just use a layout to make stick together.
    It would appear as one widget but plus and minus would be next to the slider and not on top /overlapping it.

    Like this ?
    alt text

    To make:
    Place widget on Form
    place label with + and slider and label with - inside widget in right order.
    not important if they are aligned.
    Then right click a free area of the widget and select
    Layout menu -> Layout horizontally

  • @mrjj i think i'll just write the code :p will do tomorrow and see what i get, thanks!

Log in to reply