Mohammad
8th January 2012, 21:52
Hi i am working on college task that involve a recursion drawing question and i am working with Qt.
my problem when the drawing function begin, the gui freezing until drawing finish,even though i used multi threading .
is there any way to avoid freezing while repainting main window ???
here is the code:
class draw:public QThread
{
public :
QPainter *d;
QWidget *t;
draw(){}
void run()
{
Drawing(10,40,0,3.1415926535897932384626433832795/4,200,200,d);
}
void Drawing(int n,double d,double q1,double q2,int x,int y,QPainter *dr)
{
if (n > 0)
{
int x1 = x - (sin(q1) * d);
int y1 = y - (cos(q1) * d);
dr->drawLine(x, y, x1, y1);
t->repaint();
int x2 = x1- (cos(q1) * d);
int y2 = y1 + (sin(q1) * d);
dr->drawLine(x1, y1, x2, y2);
t->repaint();
int x3 = x2 + (sin(q1) * d);
int y3 = y2 + (cos(q1) * d);
dr->drawLine(x2 , y2 , x3 , y3);
t->repaint();
dr->drawLine(x3 , y3 , x , y );
t->repaint();
double d1 = d / sqrt( 2.0 ); //d1 = d/?2
int x4 = x1 - ( cos(q2) * d1);
int y4 = y1 - ( sin(q2) * d1);
Drawing(n - 1, d1, q1 - 3.1415926535897932384626433832795/4, q2 + 3.1415926535897932384626433832795/4 , x1 , y1,dr);
Drawing(n - 1, d1, q1 + 3.1415926535897932384626433832795/4, q2 - 3.1415926535897932384626433832795/4 , x4 , y4,dr);
}
}
};
void MainWindow::on_pushButton_clicked()
{
image=new QPixmap(this->geometry().width(),this->geometry().height());
image->fill(this,0,0);
dr=new QPainter(this->image);
r=this->geometry();
draw d;
d.d=dr;
d.t=this;
d.run();
delete dr;
}
void MainWindow::paintEvent(QPaintEvent *e)
{
QPainter p(this);
p.drawPixmap(r,*this->image);
}
my problem when the drawing function begin, the gui freezing until drawing finish,even though i used multi threading .
is there any way to avoid freezing while repainting main window ???
here is the code:
class draw:public QThread
{
public :
QPainter *d;
QWidget *t;
draw(){}
void run()
{
Drawing(10,40,0,3.1415926535897932384626433832795/4,200,200,d);
}
void Drawing(int n,double d,double q1,double q2,int x,int y,QPainter *dr)
{
if (n > 0)
{
int x1 = x - (sin(q1) * d);
int y1 = y - (cos(q1) * d);
dr->drawLine(x, y, x1, y1);
t->repaint();
int x2 = x1- (cos(q1) * d);
int y2 = y1 + (sin(q1) * d);
dr->drawLine(x1, y1, x2, y2);
t->repaint();
int x3 = x2 + (sin(q1) * d);
int y3 = y2 + (cos(q1) * d);
dr->drawLine(x2 , y2 , x3 , y3);
t->repaint();
dr->drawLine(x3 , y3 , x , y );
t->repaint();
double d1 = d / sqrt( 2.0 ); //d1 = d/?2
int x4 = x1 - ( cos(q2) * d1);
int y4 = y1 - ( sin(q2) * d1);
Drawing(n - 1, d1, q1 - 3.1415926535897932384626433832795/4, q2 + 3.1415926535897932384626433832795/4 , x1 , y1,dr);
Drawing(n - 1, d1, q1 + 3.1415926535897932384626433832795/4, q2 - 3.1415926535897932384626433832795/4 , x4 , y4,dr);
}
}
};
void MainWindow::on_pushButton_clicked()
{
image=new QPixmap(this->geometry().width(),this->geometry().height());
image->fill(this,0,0);
dr=new QPainter(this->image);
r=this->geometry();
draw d;
d.d=dr;
d.t=this;
d.run();
delete dr;
}
void MainWindow::paintEvent(QPaintEvent *e)
{
QPainter p(this);
p.drawPixmap(r,*this->image);
}