Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Unsolved QSlider with Text at tick marks

    General and Desktop
    2
    5
    3458
    Loading More Posts
    • 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
      Smeeth last edited by

      I have some horizontal QSliders in my Qt application with 4-5 ticks marks. How can I add QLabels above the slider ticks to reflect the value at each of the ticks?

      I suspect the best way to do this is to create a subclass of QSlider and override the paintEvent() method like this answer describes. But how would I add QLabels based on the position of the ticks?

      1 Reply Last reply Reply Quote 0
      • S
        Smeeth last edited by Smeeth

        I am going to answer my own question. The solution is to override the paintEvent method and use drawText() to write the text on screen.

        Code taken from this answer

        void MySlider::painEvent(...) {
            QSlider::paintEvent(ev);
        
            auto painter = new QPainter(this);
            painter->setPen(QPen(Qt::black));
        
            auto rect = this->geometry();
        
            int numTicks = (maximum() - minimum())/tickInterval();
        
            QFontMetrics fontMetrics = QFontMetrics(this->font());
        
            if (this->orientation() == Qt::Horizontal) {
        
                int fontHeight = fontMetrics.height();
        
                for (int i=0; i<=numTicks; i++){
        
                    int tickNum = minimum() + (tickInterval() * i);
        
                    auto tickX = ((rect.width()/numTicks) * i) - (fontMetrics.width(QString::number(tickNum))/2);
                    auto tickY = rect.height() - fontHeight;
        
                    painter->drawText(QPoint(tickX, tickY),
                                      QString::number(tickNum));
                }
        
            } else if (this->orientation() == Qt::Vertical) {
        
                //do something else for vertical here, I haven't implemented it because I don't need it
            } else {
                return;
            }
        
            painter->drawRect(rect);
        }
        
        1 Reply Last reply Reply Quote 2
        • Kent-Dorfman
          Kent-Dorfman last edited by

          you are correct...that's pretty much as I have done. My approach though is to do a tickless slider and a completely custom background in painEvent(). As you've guessed, the challenge is correctly locating and scaling the background elements based on a widget that can change sizes.

          S 1 Reply Last reply Reply Quote 0
          • S
            Smeeth @Kent-Dorfman last edited by Smeeth

            @Kent-Dorfman

            Thanks,

            I've implemented a custom paint method like the one I described above. The only problem I have is that when the font is large enough, and the text would extend outside the geometry of the QSlider, it gets cut off (as you would expect, since it is outside the geometry of the Slider widget.)

            0_1554306376880_3ef42f5c-6632-41ae-9d4a-f203a46cf0f8-image.png

            How can I rectify this? I tried setting the padding on the QSlider to 100px but that did not help. Is this something I can fix with a stylesheet option?

            1 Reply Last reply Reply Quote 0
            • Kent-Dorfman
              Kent-Dorfman last edited by Kent-Dorfman

              Change QSlider vertical size policy and have the parent control the space available to QSlider? Maybe subclass QWidget and add the QSlider as a member object instead of a direct parent class. I assume you turned off AutoFillBackground as expected?

              1 Reply Last reply Reply Quote 0
              • First post
                Last post