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. QPainter::setClipRect(...) is not working with QSvgGenerator
Forum Updated to NodeBB v4.3 + New Features

QPainter::setClipRect(...) is not working with QSvgGenerator

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 3.5k Views 2 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.
  • S Offline
    S Offline
    soma_yarram
    wrote on last edited by
    #1

    Hi,

    How can we clip a painter linked to a QSvgGenerator paint device? I have the following example:

            QSvgGenerator generator;
    	generator.setFileName("test_svg_file.svg");
    	generator.setSize(size());
    	generator.setViewBox(rect());
    
    	QPainter painter;
    	painter.begin(&generator);
    	painter.setRenderHints(QPainter::Antialiasing);
    
    	painter.setClipRect(QRect(100, 100, width() - 101, height() - 101));
    	painter.drawLine(0, 0, width() - 1, height() - 1);
    
    	painter.end();
    

    in which the painter is supposed to clip the line, but it is not. The output SVG file has line from "top-left" to "bottom-right".

    Comment: In a child widget, I'm drawing a painter path (QPainterPath) going up and down. Since clipping is ignored by QSvgGenerator (I'm assuming), the SVG file shows path outside the child widget too.

    What is the problem? How can I clip all painter drawings?

    Thanks in advance.

    kshegunovK 1 Reply Last reply
    0
    • S soma_yarram

      Hi,

      How can we clip a painter linked to a QSvgGenerator paint device? I have the following example:

              QSvgGenerator generator;
      	generator.setFileName("test_svg_file.svg");
      	generator.setSize(size());
      	generator.setViewBox(rect());
      
      	QPainter painter;
      	painter.begin(&generator);
      	painter.setRenderHints(QPainter::Antialiasing);
      
      	painter.setClipRect(QRect(100, 100, width() - 101, height() - 101));
      	painter.drawLine(0, 0, width() - 1, height() - 1);
      
      	painter.end();
      

      in which the painter is supposed to clip the line, but it is not. The output SVG file has line from "top-left" to "bottom-right".

      Comment: In a child widget, I'm drawing a painter path (QPainterPath) going up and down. Since clipping is ignored by QSvgGenerator (I'm assuming), the SVG file shows path outside the child widget too.

      What is the problem? How can I clip all painter drawings?

      Thanks in advance.

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @soma_yarram said in QPainter::setClipRect(...) is not working with QSvgGenerator:

      What is the problem?

      Are you absolutely sure the clipping rect is valid?
      What does:

      qDebug() << QRect(100, 100, width() - 101, height() - 101);
      

      print to the terminal/application output view?

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • S Offline
        S Offline
        soma_yarram
        wrote on last edited by
        #3

        Hi.....the entire code snippet is in a widget's slot. In my case, widget's width and height are above 500px, which means the clip rect is valid. Moreover the code snippet is just an example where I want to clip a line drawn from "top-left" to "bottom-right" leaving 100px border in all sides.

        The output SVG file has a line from "top-left" to "bottom-right" which is not clipped. But when I change paint device to QImage or QPixmap, it works as expected, the line is clipped. The problem comes with QSvgGenerator.

        kshegunovK 1 Reply Last reply
        0
        • S soma_yarram

          Hi.....the entire code snippet is in a widget's slot. In my case, widget's width and height are above 500px, which means the clip rect is valid. Moreover the code snippet is just an example where I want to clip a line drawn from "top-left" to "bottom-right" leaving 100px border in all sides.

          The output SVG file has a line from "top-left" to "bottom-right" which is not clipped. But when I change paint device to QImage or QPixmap, it works as expected, the line is clipped. The problem comes with QSvgGenerator.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          It may be a bug, have you checked the bugtracker to see if something's known?

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            soma_yarram
            wrote on last edited by
            #5

            I could not find if it is a bug or the feature is not at all implemented, but I found a reference in the page: https://wiki.python.org/moin/PyQt/Clipping SVG output, where it says that QSvgGenerator doesn't support clipping.

            If it is so, then I may have to solve clipping issue in another way.

            Do you have any ideas? or is there any other vector image format other than SVG supported by Qt?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              soma_yarram
              wrote on last edited by
              #6

              Yes....I found bugs related to SVG clipping already reported (https://bugreports.qt.io/browse/QTBUG-28636). Looks like there will not be a bug fix.

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

                Hi,

                The bug linked in the comment suggests to apply the clipping before rendering to the SVG in order to avoid creating files with data that would ultimately be unused. Would that be an acceptable workaround ?

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

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  soma_yarram
                  wrote on last edited by
                  #8

                  Hi,

                  In my case, I'm simply drawing a QPainterPath, which is easy to clip with a specified QRect (border).

                  But I have a problem with this approach: I'm using QPainterPath::intersected(const QPainterPath &p) const method which returns clipped path, which is a single connected unit. I want to have multiple disconnected units:

                  0_1498726162334_Untitled.png

                  How can I get such multiple disconnected units? Thanks in advance.

                  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