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. drawPrimitive does not draw over all primitive elements
Forum Updated to NodeBB v4.3 + New Features

drawPrimitive does not draw over all primitive elements

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 159 Views 1 Watching
  • 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.
  • A Offline
    A Offline
    ateek.ujjawal
    wrote on last edited by
    #1

    Hi,

    I have been trying to draw some primitive elements for a custom styled scrollbar. A snippet of my code:

    void CustomStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption* opt,
                                    QPainter* p, const QWidget* widget) const {
      if (pe == PE_IndicatorArrowUp || pe == PE_IndicatorArrowDown) {
        QImage img;
        if (pe == PE_IndicatorArrowUp) {
          img = QImage(":/resources/ui/upward_arrow.png");
        } else if (pe == PE_IndicatorArrowDown) {
          img = QImage(":/resources/ui/downward_arrow.png");
        }
    
        QRect topPortion = QRect(QPoint(-2, -2), QSize(1.7 * opt->rect.width(),
                                                       1.7 * opt->rect.height()));
    
        QPixmap pixmap = QPixmap::fromImage(img);
    
        QSize size = pixmap.size().scaled(topPortion.size(), Qt::KeepAspectRatio);
        p->drawPixmap(topPortion.x(), topPortion.y(), size.width(), size.height(),
                      pixmap);
      } else {
        QProxyStyle::drawPrimitive(pe, opt, p, widget);
      }
    }
    

    Although I wanted to draw to separate primitive elements, only the PE_IndicatorArrowUp is drawn. What is even more surprising, is that the image for the downward arrow is drawn in place of the PE_IndicatorArrowUp primitive element while the PE_IndicatorArrowDown is left blank.

    I have attached a screenshot of how it looks like.
    Screenshot 2025-02-05 110755.png

    I want to know why this behavior takes place and how to resolve it.

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Use a debugger and see what is drawn when on which place. Also make sure you added the correct images.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        From a quick look at your code, aren't you always painting the image at the same place ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        Christian EhrlicherC 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          From a quick look at your code, aren't you always painting the image at the same place ?

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          @SGaist said in drawPrimitive does not draw over all primitive elements:

          Hi,

          From a quick look at your code, aren't you always painting the image at the same place ?

          You're correct - opt.rect is not honored...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          A 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @SGaist said in drawPrimitive does not draw over all primitive elements:

            Hi,

            From a quick look at your code, aren't you always painting the image at the same place ?

            You're correct - opt.rect is not honored...

            A Offline
            A Offline
            ateek.ujjawal
            wrote on last edited by
            #5

            @Christian-Ehrlicher Thank you, I figured it out, using opt->rect's x and y attributes draws it in the correct place.

            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