Results 1 to 2 of 2

Thread: how to draw using a while loop.

  1. #1
    Join Date
    Dec 2015
    Posts
    4
    Qt products
    Qt3
    Platforms
    MacOS X

    Default Re: how to draw using a while loop.

    Hey guys, if i'm calculating a matrix that is supposed to change every second using a while loop and i'm using a painter in it and the pen changes colors depending on each value in the matrix, the problem is that when i run the program it doesn't draw continuously, but only the final result altho i put the code inside the paint event, in the dialog.cpp.

    can anyone help?
    i want to see the drawing at every loop at the while not at the end of it.

    oh yes and one more thing, why is the code getting executed tons of times instead of just once? does the paint event loops? if yes how can i let it be executed just once?


    Added after 33 minutes:


    void Dialog::paintEvent(QPaintEvent *e)
    {
    QPainter painter(this);
    QPen framepen(QColor::fromRgb(0,0,255,255));
    QBrush brush(QColor::fromRgb(0,0,255,255));
    framepen.setWidth(1);
    float deltaX , deltaY, deltaT;
    float D , vX, vY;
    float m=9; //dim-1

    deltaX=0.3;
    deltaY=0.3;
    deltaT=1;
    vX=0.03;
    vY=0;
    D=0.01;
    float deltaX2=deltaX*deltaX;
    float deltaY2=deltaY*deltaY;
    float alfa = vX * deltaT / deltaX;
    float beta = vY * deltaT / deltaY;
    float gamma = D * deltaT / deltaX2;
    float etha = D * deltaT / deltaY2;

    cout<<gamma + etha + alfa/2 + beta/2 <<endl;
    //alfa 0.001 beta 0 gamma 0.0001 etha 0.0001
    // il faut <0.5

    Matricem C = Matricem(10,10);

    //pour k = 0 (cas initial avec poluant sur 2,0 et 3,0)
    float k =0;
    for (float i=1; i<m+1; i++){ //0 a 9
    for (float j=1; j<m+1; j++){
    C(i,j) = 0;
    }
    }
    C(3,1)=255;
    C(4,1)=255;
    //affiche(C);
    //cout<<C;
    //condition
    //cout<<C;

    // gamma + etha + 2*alfa*deltaT + 2*beta*deltaT < 0.25
    // D * deltaT / deltaX2; + D * deltaT / deltaY2; + 2*vX * (deltaT / deltaX)*delta t ,2*vY * deltaT / deltaY;*delta t <0.25;
    //prenons 100 incrementations de k
    k=1;
    // sans les bords
    while (k< 3){
    for (float i=2; i<m+1; i++){ // 1 a 8bb
    for (float j=2; j<m+1; j++){ // 1 a 8
    C(i,j) = C(i,j)*(1+alfa+beta-2*gamma-2*etha) + C(i+1,j)*(-alfa+gamma)
    +C(i,j+1)*(-beta+etha) + gamma*C(i-1,j) + etha*C(i,j-1);
    //C(i,j)=1;
    }
    }
    //C(2,1) = C(2,1)*(1+alfa+beta-2*gamma-2*etha) + C(2+1,1)*(-alfa+gamma+etha)
    //+C(2,1+1)*(-beta) + gamma*C(2-1,1) + etha*C(2,1-1);
    //affiche(C);
    // bord inferieur

    for (float i=5; i<m+1; i++){ // colonne 1 a 8
    C(i,1) = C(i,1)*(1+alfa-2*gamma-2*etha) + C(i+1,1)*(-alfa+gamma) //ligne 9
    + C(i-1,1)*(gamma) + 2*etha*C(i,2);
    //C(2,1) = C(2,1)*(1+alfa-2*gamma-2*etha) + C(2+1,1)*(-alfa+gamma) //ligne 9
    //+ C(2-1,1)*(gamma) + 2*etha*C(2,2);
    //cout<<C(5,1)<<endl;
    }
    //cout<<C(5,1)<<endl;
    //C(2,1) = C(2,1)*(1+alfa-2*gamma-2*etha) + C(2+1,1)*(-alfa+gamma) //ligne 9
    //+ C(2-1,1)*(gamma) + 2*etha*C(2,2);
    C(2,1) = C(2,1)*(1+alfa-2*gamma-2*etha) + C(2+1,1)*(-alfa+gamma) //ligne 9
    + C(2-1,1)*(gamma) + 2*etha*C(2,2);
    //alfa 0.001 beta 0 gamma 0.0001 etha 0.0001
    // bord superieur
    //C(2-1,1)*(gamma)
    //cout<<C(2,1)<<endl;
    for (float i=2; i<m+1; i++){ //colonne x1 a 8
    C(i,m+1) = C(i,m+1)*(1+alfa-2*gamma-2*etha) + C(i+1,m+1)*(-alfa+gamma) //ligne 9
    + C(i-1,m+1)*gamma + 2*etha*C(i,m);
    }
    for(float i=1;i<m+2;i++){
    C(1,i)=0;
    }
    for(float j=1;j<m+2;j++){
    C(m+1,j)=0;
    }

    QRect rect10(10,60,50,50); //2eme ligne 1ere colonne

    painter.drawRect(rect10);
    painter.fillRect(rect10,brush);


    QRect rect11(60,60,50,50); //2eme ligne 2eme colonne
    framepen.setColor(QColor::fromRgb(0,C(2,9),255-C(2,9),255));
    brush.setColor(QColor::fromRgb(0,C(2,9),255-C(2,9),255));
    painter.drawRect(rect11);
    painter.fillRect(rect11,brush);

    QRect rect12(110,60,50,50); //2eme ligne 3eme colonne
    framepen.setColor(QColor::fromRgb(0,C(3,9),255-C(3,9),255));
    brush.setColor(QColor::fromRgb(0,C(3,9),255-C(3,9),255));
    painter.drawRect(rect12);
    painter.fillRect(rect12,brush);

    QRect rect13(160,60,50,50);
    framepen.setColor(QColor::fromRgb(0,C(4,9),255-C(4,9),255));
    brush.setColor(QColor::fromRgb(0,C(4,9),255-C(4,9),255));
    painter.drawRect(rect13);
    painter.fillRect(rect13,brush);

    QRect rect14(210,60,50,50);
    framepen.setColor(QColor::fromRgb(0,C(5,9),255-C(5,9),255));
    brush.setColor(QColor::fromRgb(0,C(5,9),255-C(5,9),255));
    painter.drawRect(rect14);
    painter.fillRect(rect14,brush);

    QRect rect15(260,60,50,50);
    framepen.setColor(QColor::fromRgb(0,C(6,9),255-C(6,9),255));
    brush.setColor(QColor::fromRgb(0,C(6,9),255-C(6,9),255));
    painter.drawRect(rect15);
    painter.fillRect(rect15,brush);

    QRect rect16(310,60,50,50);
    framepen.setColor(QColor::fromRgb(0,C(7,9),255-C(7,9),255));
    brush.setColor(QColor::fromRgb(0,C(7,9),255-C(7,9),255));
    painter.drawRect(rect16);
    painter.fillRect(rect16,brush);

    QRect rect17(360,60,50,50);
    framepen.setColor(QColor::fromRgb(0,C(8,9),255-C(8,9),255));
    brush.setColor(QColor::fromRgb(0,C(8,9),255-C(8,9),255));
    painter.drawRect(rect17);
    painter.fillRect(rect17,brush);

    QRect rect18(410,60,50,50);
    framepen.setColor(QColor::fromRgb(0,C(9,9),255-C(9,9),255));
    brush.setColor(QColor::fromRgb(0,C(9,9),255-C(9,9),255));
    painter.drawRect(rect18);
    painter.fillRect(rect18,brush);

    QRect rect19(460,60,50,50);
    framepen.setColor(QColor::fromRgb(0,0,255,255));
    brush.setColor(QColor::fromRgb(0,0,255,255));
    painter.drawRect(rect19);
    painter.fillRect(rect19,brush);






    QRect rect20(10,110,50,50);
    framepen.setColor(QColor::fromRgb(0,0,255,255));
    brush.setColor(QColor::fromRgb(0,0,255,255));
    painter.drawRect(rect20);
    painter.fillRect(rect20,brush);

    QRect rect21(60,110,50,50);
    framepen.setColor(QColor::fromRgb(0,C(2,8),255-C(2,8),255));
    brush.setColor(QColor::fromRgb(0,C(2,8),255-C(2,8),255));
    painter.drawRect(rect21);
    painter.fillRect(rect21,brush);


    update();

    k++;
    }
    }

    i have 100 rectangle so i only posted 10 of them... i hope you understand what i'm trying to do and what my problem is...

    it's failing with rgb error out of range while the biggest value of my matrix is 255.. so i am not going past that.
    Last edited by Fouad Hanoun; 22nd January 2016 at 06:34.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: how to draw using a while loop.

    If you don't return from paintEvent, then there is no time for Qt to update the widget's content with what you have just drawn.

    So if "drawing in a loop" means you are drawing different things in each interation, then you don't use an actual loop, but return after each "iteration" and schedule another paint by calling QWidget::update().
    The latter potentially delayed with a timer, there is a limit of what a human eye can still see regarding frame rate.

    Cheers,
    _

    P.S.: when you post code, wrap it in [code][/code] tags

Similar Threads

  1. For loop and While loop handling.
    By kiboi in forum Newbie
    Replies: 7
    Last Post: 2nd January 2013, 13:43
  2. Replies: 4
    Last Post: 6th August 2011, 01:40
  3. Replies: 10
    Last Post: 10th February 2011, 23:31
  4. Main loop thread loop communication
    By mcsahin in forum Qt Programming
    Replies: 7
    Last Post: 25th January 2011, 16:31
  5. Replies: 12
    Last Post: 25th December 2009, 15:07

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.