Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-123427

QStyle::subControlRect() returns wrong results for vertical slider groove

    XMLWordPrintable

Details

    • Bug
    • Resolution: Invalid
    • P3: Somewhat important
    • None
    • 5.15.3, 6.2.4
    • None
    • Ubuntu 22.04 LTS
    • Linux/X11

    Description

      Given is the following code:

       

      #include <QApplication>
      #include <QDebug>
      #include <QSlider>
      #include <QStyleOptionSlider>
      #include <QVBoxLayout>
      
      class PaintEventCenteredSlider : public QSlider
      {
      public:
          PaintEventCenteredSlider(QWidget *parent = nullptr) : QSlider(parent) {}
      
      protected:
          void paintEvent(QPaintEvent *event) override
          {
              QStyleOptionSlider option;
              option.initFrom(this);
              // Groove rect information
              const auto grooveRect = style()->subControlRect(QStyle::CC_Slider, &option, QStyle::SC_SliderGroove, this);
              qDebug() << "Orientation:" << orientation() << "Rect:" << grooveRect;
      
              QSlider::paintEvent(event);
          }
      };
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          // Create a horizontal and a vertical slider
          auto* const paintEventSliderHorizontal = new PaintEventCenteredSlider;
          paintEventSliderHorizontal->setOrientation(Qt::Horizontal);
          auto* const paintEventSliderVertical = new PaintEventCenteredSlider;
          paintEventSliderVertical->setOrientation(Qt::Vertical);
      
          // Apply to layout and then widget
          auto* const layout = new QVBoxLayout;
          layout->addWidget(paintEventSliderHorizontal);
          layout->addWidget(paintEventSliderVertical);
          auto* const widget = new QWidget;
          widget->setLayout(layout);
          widget->show();
      
          return a.exec();
      }
      

      CMakeLists.txt:

       

      cmake_minimum_required(VERSION 3.8)
      
      set(CMAKE_AUTOMOC ON)
      set(CMAKE_AUTORCC ON)
      
      project(ExampleProject LANGUAGES CXX)
      
      find_package(QT NAMES Qt6 COMPONENTS Widgets REQUIRED)
      find_package(Qt6 COMPONENTS Widgets REQUIRED)
      
      add_executable(ExampleProject
          ${CMAKE_CURRENT_LIST_DIR}/main.cpp
      )
      target_link_libraries(ExampleProject
          PRIVATE Qt::Widgets
      )
      

      The widget looks like the following:

      When running the program shown above, the following output is generated for Qt5:

      Orientation: Qt::Horizontal Rect: QRect(2,7 176x6)
      Orientation: Qt::Vertical Rect: QRect(2,7 16x6)

      And for Qt6:

      Orientation: Qt::Horizontal Rect: QRect(0,4 178x7)
      Orientation: Qt::Vertical Rect: QRect(0,38 15x7)

      For the horizontal slider, these values seem to make sense because the expected x value should be much higher compared to the y value. However, for the vertical slider, the printed y value seems to be way too low.
      Important note: The problem shown above does not apply to the handle rectangle (QStyle::SC_SliderHandle).

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            tpochep Timur Pocheptsov
            bakefish Back Fisch
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes