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. Set Dashed Line's Dash And Space Lengths In Pixels Instead Of In Line Width Units
Qt 6.11 is out! See what's new in the release blog

Set Dashed Line's Dash And Space Lengths In Pixels Instead Of In Line Width Units

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 2.8k 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.
  • F Offline
    F Offline
    Frogs-Mulligan
    wrote on last edited by Frogs-Mulligan
    #1

    As far as I can tell there is no way in Qt to set a dashed line's dash and space lengths in any unit other than multiples of line widths. That is a show stopper for me as I need to set them in pixels in order to draw a dashed line with dashes shorter than the line is wide. Is there any way to do that?

    I did try to set the dash and space lengths as fractions of the line width, but it did not work.

    Thanks.

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

      Hi and welcome to devnet,

      You should explain what you are using to draw these lines and on what you are drawing them.

      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
      • F Offline
        F Offline
        Frogs-Mulligan
        wrote on last edited by
        #3

        Thanks for the reminder.

        It' just QPainter and QPen in a QPaintEvent on a simple MainWindow. I did this as a test. I have very little Qt experience so I wanted to see if what I want to do is even possible.

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

          Can you show an example of drawing you want to achieve ?

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

          F 1 Reply Last reply
          0
          • F Offline
            F Offline
            Frogs-Mulligan
            wrote on last edited by Frogs-Mulligan
            #5

            WideSpacedLine.png

            Sorry for the delay in getting back.

            The example is a single 130 pixel wide spaced line drawn using the Cairo graphics library, where cr is a cairo context. This works with any Cairo line, not just straight lines.

            I was hoping that Qt had a similar capabilty.

             double dashes[2] = {7.5, 30.0};
             cairo_set_source_rgba(cr, 1.0, 0.0, 0.0, 0.5);     
             cairo_set_line_width(cr, 130);                      
             cairo_set_antialias(cr,  CAIRO_ANTIALIAS_FAST);
             cairo_set_dash(cr, dashes, 2, 5);
             cairo_move_to(cr, 200, 200);
             cairo_line_to(cr, 1000, 200);
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Doesn't QPen's width and dash pattern do what you want ?

              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
              • SGaistS SGaist

                Can you show an example of drawing you want to achieve ?

                F Offline
                F Offline
                Frogs-Mulligan
                wrote on last edited by
                #7

                @SGaist

                No, I'm afraid not. From the documentation: The dash pattern is specified in units of the pens width; e.g. a dash of length 5 in width 10 is 50 pixels long.

                And fractional line width units simply don't work. I tried that.

                Frankly I am surprised that the developers did this. I can see line width units for dashes and spaces as a convenience, but not at the expense of not being able to specify them in pixels.

                Unless I can find a way to use a Cairo context in Qt, I am afraid I am not going to be able to use Qt. So far, despite searching, I have found no information on that possibility.

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

                  Based on this file from the Cairo sources I would say you should be good to go.

                  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
                  1

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved