Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7643751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:31:24+00:00 2026-05-31T09:31:24+00:00

We know that for drawing on an image in qt, qpainter is used. Recently,

  • 0

We know that for drawing on an image in qt, qpainter is used. Recently, I used drawLine() function to draw whatever an user is scribbling. This was done by passing the lastPoint and currentPoint from the mouseMoveEvent to a custom function which implements drawLine(). I have passed the arguments for that custom function as given below:

void myPaint::mouseMoveEvent(QMouseEvent *event) {  
qDebug() << event->pos();
   if ((event->buttons() & Qt::LeftButton) && scribbling) {
    pixelList.append(event->pos());
    drawLineTo(event->pos());
    lastPoint = event->pos();
   }
}

Now with the help of qDebug() I noticed that some pixels are missed while drawing but the drawing is precise. I looked up the source of qt-painting where I saw that drawLine() was calling drawLines() which was making use of qpainterPath to have a shape drawn on the image.

My question is that, is there anyway to track these “missed” pixels or any approach to find all the pixels which have been drawn?

Thanks!

void myPaint::drawLineTo(const QPoint &endPoint) {    
QPainter painter(image); //image is initialized in the constructor of myPaint
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(QPen(Qt::blue, myPenWidth, Qt::SolidLine, Qt::RoundCap,Qt::RoundJoin));
painter.drawLine(lastPoint, endPoint);
modified = true;
lastPoint = endPoint; //at the mousePressEvent, the event->pos() will be stored as
                      // lastPoint 
update();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:31:26+00:00Added an answer on May 31, 2026 at 9:31 am

    For a start, don’t draw in a mouseEvent(). Actually handling a mouseevent should be done as quick as possible. Also, it is not a good idea to look at the Qt source, it can be confusing. Rather assume that what Qt gives you work, and first try to answer “What I am doing wrong?”. As I said drawing in a mouse event is definitely wrong.

    Your description is really subjective, maybe an image of your output is better. Are you trying to emulate a pen (like in windows paint)? In this case do the mouse button has to be down ? is that the purpose of your variable scribbling?

    There is more. following the documentation, QMouseEvent::buttons() always return a combination of all buttons for mouse move event. Which make sense : the mouse movements are independent of the buttons. It means

    if ((event->buttons() & Qt::LeftButton)

    will always be true.

    Let’s assume you want to draw the path of your mouse when the left button is pressed. Then you use something like :

    void myPaint::mousePressEvent(QMouseEvent *event){
         scribbling = true;
         pixelList.clear();
    }
    
    void myPaint::mouseReleaseEvent(QMouseEvent *event){
         scribbling = false;
    }
    
    void myPaint::mouseMoveEvent(QMouseEvent *event) {  
      if ( scribbling) {
        pixelList.append(event->pos());
      }
    }
    
    void myPaint::paintEvent(){
      QPainter painter(this)
      //some painting here
      if ( scribbling) {
         painter.setRenderHint(QPainter::Antialiasing);
         painter.setPen(QPen(Qt::blue, myPenWidth, Qt::SolidLine, Qt::RoundCap,Qt::RoundJoin));
        // here draw your path
        // for example if your path can be made of lines, 
        // or you just put the points if they are close from each other
      }
    
      //other painting here
    }
    

    If after all of this you don’t have a good rendering, try using float precision (slower), ie QMouseEvent::posF() instead of QMouseEvent::pos().

    EDIT :
    “I want to know whether there is any way to calculate all the sub-pixels between any two pixels that we send as arguments to drawLine”
    Yes there is. I don’t know why you need to do such thing but is really simple. A line can be characterized with the equation

       y = ax + b
    

    Both of the endpoints of the line p0 = (x0, y0) and p1 = (x1, y1) satisfy this equation so you can easily find a and b. Now all you need to do is increment from x0 to x1 by the amount of
    pixels you want (say 1), and to compute the corresponding y value, each time saving point(x,y).

    So will go over all of the points saved in pixelList and repeat this process for any two consecutive points.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that g.setXORMode(Color c) enables XOR mode drawing. But how to disable this
I'm drawing an Image on the canvas using the drawImage function. This is how
I know that the MsNLB can be configured to user mulitcast with IGMP. However,
I have a method here that is supposed to produce a System.Drawing.Image instance. Consider
How could I generate a System.Drawing.Image that contains the differences between the pixels of
I got this code that should help me save an image to the servers
I have a UIView that allows the user to draw a line (myLine) on
I recall that there was this one method that could piece together an image,
Ok we all know that nesting an image view inside of a scroll view
Does someone know what might go wrong, when you attempt to compare two System.Drawing.Image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.