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. Drawing a static grid on a pixmap/Qlabel
QtWS25 Last Chance

Drawing a static grid on a pixmap/Qlabel

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlabelqpixmapqpainterpaintevent
4 Posts 2 Posters 342 Views
  • 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.
  • G Offline
    G Offline
    gone_bush
    wrote on 7 Oct 2024, 07:51 last edited by
    #1

    I have an app that displays an image in a QLabel. I zoom and drag the image with the mouse - this works! (yea for me)

    What I want to do now is to draw a (Rule of Thirds) grid over the image.

    The problem is that the grid moves whenever I drag the image.

    I've tried using coordinates bases on width() and height() of the ImageLabel and of the pixmap returned via pixmap();

    Neither results in a static grid.

    HELP!!!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on 7 Oct 2024, 08:20 last edited by
      #2

      How are you drawing the grid and image?

      G 1 Reply Last reply 7 Oct 2024, 10:19
      0
      • C ChrisW67
        7 Oct 2024, 08:20

        How are you drawing the grid and image?

        G Offline
        G Offline
        gone_bush
        wrote on 7 Oct 2024, 10:19 last edited by
        #3

        @ChrisW67 image via

        setPixmap( QPixmap::fromImage( image ));

        zoom via

        wheelEvent

        dragging via

        mousePress/Move/Release

        grid via

        paintEvent

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gone_bush
          wrote on 10 Oct 2024, 02:01 last edited by gone_bush 10 Oct 2024, 02:03
          #4

          For the historical record, I finally got it working. My code:

          `

            QRect screenGeometry = QGuiApplication::primaryScreen( )->geometry( );
          
              int x13;
              int x23;
              int y13;
              int y23;
          
              if( screenGeometry.width( ) < width( ))
              {       x13 = m_image->getScrollX( ) +     screenGeometry.width( ) / 3.0;
                      x23 = m_image->getScrollX( ) + 2 * screenGeometry.width( ) / 3.0;
              }
              else
              {       x13 = m_image->getScrollX( ) +     width( ) / 3.0;
                      x23 = m_image->getScrollX( ) + 2 * width( ) / 3.0;
              }
          
              if( screenGeometry.height( ) < height( ))
              {       y13 = m_image->getScrollY( ) +     screenGeometry.height( ) / 3.0;
                      y23 = m_image->getScrollY( ) + 2 * screenGeometry.height( ) / 3.0;
              }
              else
              {       y13 = m_image->getScrollY( ) +     height( ) / 3.0;
                      y23 = m_image->getScrollY( ) + 2 * height( ) / 3.0;
              }
          
              QPainter painter( this );
              painter.setPen( QPen( Qt::red, 4, Qt::SolidLine, Qt::FlatCap ));
          
              painter.drawLine( 0, y13, width( ), y13 );
              painter.drawLine( 0, y23, width( ), y23 );
          
              painter.drawLine( x13, 0, x13, height( ));
              painter.drawLine( x23, 0, x23, height( ));
          

          }

          `

          1 Reply Last reply
          0

          4/4

          10 Oct 2024, 02:01

          • Login

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