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::drawLine() draws some weird dashes instead of zero-length lines
Forum Updated to NodeBB v4.3 + New Features

QPainter::drawLine() draws some weird dashes instead of zero-length lines

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 1.3k Views 3 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.
  • B Offline
    B Offline
    Brono
    wrote on last edited by
    #1

    Here is some code:

    widget.cpp

    #include "widget.h"
    
    #define SIZE 400
    #define LOGICAL_SIZE 30
    #define LINE_WIDTH 0.5
    
    Widget::Widget(QWidget *parent)
    	: QWidget(parent),
    	  img(SIZE, SIZE, QImage::Format_ARGB32)
    {
    	QPointF p1(1.23, 4.56);
    	QPointF p2(1.23, 4.56);
    	QPointF p3(6.54, 3.21);
    	QPointF p4(6.54, 3.21);
    	this->resize(SIZE, SIZE);
    	img.fill(Qt::white);
    	QPainter painter(&img);
    	painter.setWindow(-LOGICAL_SIZE/2.0,
    	                  -LOGICAL_SIZE/2.0,
    	                  LOGICAL_SIZE,
    	                  LOGICAL_SIZE);
    	painter.setPen(QPen(Qt::black, LINE_WIDTH));
    	painter.drawLine(p1, p2);
    	painter.setPen(QPen(Qt::red, LINE_WIDTH));
    	painter.drawLine(p3, p4);
    	img.save("result.png");
    }
    
    //...
    
    

    AFAIK this should generate blank image or two dots.
    However it outputs two dashes:

    0_1537861296159_result.png

    What am i doing wrong?

    raven-worxR 1 Reply Last reply
    0
    • A Offline
      A Offline
      Asperamanca
      wrote on last edited by
      #2

      Try setting the cap style of the pen to Qt::FlatCap

      B 1 Reply Last reply
      0
      • B Brono

        Here is some code:

        widget.cpp

        #include "widget.h"
        
        #define SIZE 400
        #define LOGICAL_SIZE 30
        #define LINE_WIDTH 0.5
        
        Widget::Widget(QWidget *parent)
        	: QWidget(parent),
        	  img(SIZE, SIZE, QImage::Format_ARGB32)
        {
        	QPointF p1(1.23, 4.56);
        	QPointF p2(1.23, 4.56);
        	QPointF p3(6.54, 3.21);
        	QPointF p4(6.54, 3.21);
        	this->resize(SIZE, SIZE);
        	img.fill(Qt::white);
        	QPainter painter(&img);
        	painter.setWindow(-LOGICAL_SIZE/2.0,
        	                  -LOGICAL_SIZE/2.0,
        	                  LOGICAL_SIZE,
        	                  LOGICAL_SIZE);
        	painter.setPen(QPen(Qt::black, LINE_WIDTH));
        	painter.drawLine(p1, p2);
        	painter.setPen(QPen(Qt::red, LINE_WIDTH));
        	painter.drawLine(p3, p4);
        	img.save("result.png");
        }
        
        //...
        
        

        AFAIK this should generate blank image or two dots.
        However it outputs two dashes:

        0_1537861296159_result.png

        What am i doing wrong?

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        @Brono
        setting a line-width of 0.5 doesn't make much sense. See QPen::setWidth(). Even a pen with a width of 0 draws a 1-pixel line.

        Either you want to draw or surround your drawing code with an if-condition and prevent drawing.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • B Offline
          B Offline
          Brono
          wrote on last edited by Brono
          #4

          @raven-worx :
          Help entry you linked to mentions "cosmetic pen", which is "independant to transformations".

          Here setting width of the pen does make quite a sense, because i'm setting a "transformation" (QPainter::setWindow()).

          P.S.: Image in the post is the real output image (the result.png one).

          raven-worxR 1 Reply Last reply
          0
          • B Brono

            @raven-worx :
            Help entry you linked to mentions "cosmetic pen", which is "independant to transformations".

            Here setting width of the pen does make quite a sense, because i'm setting a "transformation" (QPainter::setWindow()).

            P.S.: Image in the post is the real output image (the result.png one).

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #5

            @Brono
            but you are setting a pen width of a half pixel in a QWidget context where only full pixels can be drawn

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            B 1 Reply Last reply
            1
            • raven-worxR raven-worx

              @Brono
              but you are setting a pen width of a half pixel in a QWidget context where only full pixels can be drawn

              B Offline
              B Offline
              Brono
              wrote on last edited by Brono
              #6

              @raven-worx
              You can play around with the code (clickable link) and discover that setting pen width to .25 or .75 does indeed change the width of the line

              1 Reply Last reply
              0
              • A Asperamanca

                Try setting the cap style of the pen to Qt::FlatCap

                B Offline
                B Offline
                Brono
                wrote on last edited by Brono
                #7

                @Asperamanca
                Thanks, this helped.
                Seems like square capping rounds up non-integer width to highest int.
                (this does not apply to round caps)

                Is it a bug?

                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