How to paint on a different thread?!



  • hi, i tried many things like making class inside painting event but it gives me error;
    i also tried to make Qpainter pointer inside the class and taking a painter in constructor, still didnt work,

    how can i paint my stuff on a different thread?!

    heres whats i want to paint! @ for(int i = 0;i<=255;i++){
    for(int j = 0;j<=255;j++){
    painter.setPen(QPen(QColor(j,0,i),1.0));
    painter.drawPoint(j,i);
    }
    }@


  • Lifetime Qt Champion

    Hi,

    If you are trying to paint on a widget from another thread, the answer is you can't.

    Please describe what you are trying to achieve.



  • [quote author="SGaist" date="1404650530"]Hi,

    If you are trying to paint on a widget from another thread, the answer is you can't.

    Please describe what you are trying to achieve.[/quote]

    i want to put the painting on a different thread so it doesn't slow down the GUI, or ect,



  • [quote author="SGaist" date="1404650530"]Hi,

    If you are trying to paint on a widget from another thread, the answer is you can't.

    Please describe what you are trying to achieve.[/quote]

    btw i mistakenly wrote image, i ment a simple painter drawing, not an image, anyway to do that, that painting is heavy ! 255x255 times changing pen and drawing a point !



  • You can't do painting on widget in different thread. You can try with QImage to draw in different thread and use it.



  • [quote author="Dheerendra" date="1404665573"]You can't do painting on widget in different thread. You can try with QImage to draw in different thread and use it.[/quote]
    ok im looking at it, that drawing requires lots of CPU power, even thought it looks simple ! ,



  • hmm, i read stuff about QImage on qt refrence but didn't find out how to do that with QImage, can you tell me how!?


  • Lifetime Qt Champion

    Search Mandelbrot Example



  • [quote author="SGaist" date="1404682074"]Search Mandelbrot Example[/quote]
    Thanks :) i will search that tomorrow it's 2:00am !! Thanks for your help!



  • [quote author="SGaist" date="1404682074"]Search Mandelbrot Example[/quote]

    damn it it have been 3 days im looking at that example i can't understand it:(
    i feel like im dying !!!


  • Lifetime Qt Champion

    What are you not understanding ?



  • [quote author="SGaist" date="1404853020"]What are you not understanding ?[/quote]

    I sobt understabd the wavelength function and how the core algorithm works,, + i didnt get how he put stuff into the qimage, i understood all the other parts!! He emits the image via signal and paint it there !!


  • Lifetime Qt Champion

    In this case, the pixels are accessed directly through scanline



  • [quote author="SGaist" date="1404854909"]In this case, the pixels are accessed directly through scanline[/quote]

    Oh so, we first make a qimage and spicify the number of pixels we want to store(like 500,500) and than scanline(num) is like the exact pixel? Also the scan line wasnt a 2 arg func, so it doesn't have width and height? I will paste the exact parts here, currently im on phone and trying to sleep :/ @2:15am....

    I will post here what exactly i ment here !!! Qlso tye core algorithm, my main prob is thqt, how app calcs each pixel :/ it makes me mad (because i cant understand it) than i can't understand anything else, !!!!


  • Lifetime Qt Champion

    No, a scanline represent a line of data however you can access each pixel using QImage::setPixel or use a QPainter. Painting on a QImage will work, but don't try to paint on the widget directly that won't work.



  • [quote author="SGaist" date="1404856491"]No, a scanline represent a line of data however you can access each pixel using QImage::setPixel or use a QPainter. Painting on a QImage will work, but don't try to paint on the widget directly that won't work.[/quote]

    Oh thanks! I will use painter ti make the image, i will ask next Question tomarrow :)



  • im making my app now with QImage :)



  • i have another problem:

    this is thread's Run()
    @ QImage image = QImage(QSize(500,500), QImage::Format_RGB32);
    QPainter p;
    //p.setRenderHint(QPainter::Antialiasing);

    p.begin(&image);
    for(int i = 0;i<=50;i++){
        for(int j = 0;j<50;j++){
            p.setBrush(QColor(rand()%5,rand()%5,rand()%5));
            p.drawRect(10*i,10*j,i+1 * 10,j+1 * 10);
        }
    }
    p.end();
    emit renderedImage(image);@
    

    restart only waits and restarts the thread !

    this is my Window Constructor @ thread.start();
    connect(&thread,SIGNAL(renderedImage(QImage)),this,SLOT(DrawImage(QImage)));
    thread.wait();@

    this is draw Image:

    @ image = QPixmap::fromImage(img);
    qDebug()<<"GotCHA!";@

    this is paint event:
    @ QPainter painter(this);
    //painter.setRenderHint(QPainter::Antialiasing);

    painter.drawPixmap(0,0,image);
    thread.Restart();
    qDebug()<<"painted";@
    

    the problem is everytime i repaint, it gaves same image, seems the random is not working, and giving same number, always !

    altought it doesn't change pic, but GUI is much smoother now ! also my restart function only does one thing: start(); , if i add wait() behind it it will lag like when i didn't use a thread, so is there anyway i can interrupt the run in middle of it or prevent it from executing and instead making new one when i call restart?!

    i made this for now:
    @ if(isRunning())
    restart = true;@

    greatly reduces CPU usage but still i dont think its a good way, any other ideas:?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.