c++ - Fill function in basic graphics app in Qt -


i have been creating basic graphics program(like ms paint) simple gui. have 2 classes 1 mainwindow holds buttons, sliders etc , second class custom widget called drawingarea on user able draw. basicly i've implemented of functions, unfortunetly stucked @ filling function, should work 1 in ms paint. decided use called floodfill algorithm , after few hours of fighting(i beginner in qt) have managed make work. not @ all. problem able fill black-colored regions(shapes, lines, points etc) color choose. when comes filling different colors, puts 1 pixel in chosen color. in funcion fill(...) below passing 2 arguments - point(mouseclicked) , color of point. here:

void drawingarea::fill(const qpoint &point, qcolor act) { qpainter painter(&image); qpen mypen(actualcolor); mypen.setwidth(1); painter.setpen(mypen);  qqueue<qpoint> pixels; pixels.enqueue(point); while(pixels.isempty() == 0) {     qpoint newpoint = pixels.dequeue();     qcolor actual;     actual.fromrgb(image.pixel(newpoint));         if(actual == act)         {            painter.drawpoint(newpoint);            update();             qpoint left((newpoint.x()-1), newpoint.y());            if(left.x() >0 && left.x() < image.width() && image.pixel(left) == act.rgb())            {                pixels.enqueue(left);                painter.drawpoint(left);                update();            }             qpoint right((newpoint.x()+1), newpoint.y());            if(right.x() > 0 && right.x() < image.width() && image.pixel(right) == act.rgb())            {                pixels.enqueue(right);                painter.drawpoint(right);                update();            }             qpoint up((newpoint.x()), (newpoint.y()-1));            if(up.y() > 0 && up.y() < image.height() && image.pixel(up) == act.rgb())            {                pixels.enqueue(up);                painter.drawpoint(up);                update();            }             qpoint down((newpoint.x()), (newpoint.y()+1));            if(down.y() > 0 && down.y() < image.height() && image.pixel(down) == act.rgb())            {                pixels.enqueue(down);                painter.drawpoint(down);                update();            }         }   } } 

i call fill(...) function inside mousepressevent here:

void drawingarea::mousepressevent(qmouseevent *event)  {  if (event->button() == qt::leftbutton)    {      lastpoint = event->pos();      qcolor clr;      clr.fromrgb(image.pixel(lastpoint));      firstpoint = event->pos();      isdrawing = true;      if(iscolorcheck)   colorcheck(lastpoint);      if(isfill) fill(lastpoint,clr);    }  } 

the don't understand how working on black color, when passing argument qcolor of different colors. appreciate or ideas.

there fundamental problem code. don't drawing method called in class. drawing needs done in paint event callback define. qwidget::paintevent worth exploring.

void myclass::paintevent(qpaintevent *event) {      // drawing depending on context , may      // define context in other event handlers, etc. } 

other hard else wrong. btw, can trigger drawing event repaint() or update(). need consider how exact area of redraw defined in code.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -