Results 1 to 16 of 16

Thread: How to display QPaint Images from other Threads

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to display QPaint Images from other Threads

    If you can use gui, you can be sure there is an event loop running.
    I can not emit from here because this peace of code is belong to MainGUI
    This piece of code does not belong to anything, it's just a pseudo-code, you've asked about advice and here it is - I suggest you to setup a timer to grab frames. Don't forget to call "exec()" in your thread's run() method as well.

  2. #2
    Join Date
    May 2011
    Posts
    16
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: How to display QPaint Images from other Threads

    Hi Stampede,
    I am talking about my code, not your pseudo-code. In comment#8 I pasted my code and that is from MainGUI.

    void thread::run() {
    while(1) {
    gPlayer->nextFrame(); // This function belong to MainGUI thread
    QImage img = gPlayer->displayFrame(); // This function belong to Main GUI thread
    emit sDisplayOnWidget(img); // from here I am calling SLOT function which is there in again MainGUI thread
    qDebug() << "Non GUI thread" << QThread::currentThread();
    msleep(400);
    }
    }

    Please let me know whether this is correct way of doing or not???

    If it is correct it's not uploading frame on the Label Widget...
    VijayQt

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to display QPaint Images from other Threads

    I don't get it:
    // This function belong to MainGUI thread
    // This function belong to Main GUI thread
    // from here I am calling SLOT function which is there in again MainGUI thread
    It looks like you don't need a capture thread at all, if everything you call is from Main thread
    Please let me know whether this is correct way of doing or not???
    The way it's done now, it's definitely not ok.
    Why do you want to stick with the "while(1) + sleep()" approach ? Why are you calling methods from gui thread in capture thread directly ?
    In my opinion, you need to *really* understand what's going on and how things are supposed to work before writing the code.
    If you really need separate thread, put the capture code in this thread (class) only, and make it emit the 'new frame' signal - make it the only way of communication between GUI and capture thread (plus, some signals for pause, stop etc.).
    Think about the interface first, how do you wish to use your player class ?
    I think it could be nice to have something like this:
    Qt Code:
    1. // gui thread:
    2. void MainGui::startVideo( const QString& file ){
    3. delete this->player;
    4. this->player = new VideoPlayer(file);
    5. connect( player, SIGNAL(frame(const QImage&)), this, SLOT(frameCallback(const QImage&)) );
    6. // few more connections, for stop, pause, reverse, fast forward, whatever...
    7. this->player->start();
    8. }
    9.  
    10. void MainGui::frameCallback( const QImage& img ){
    11. QPixmap p = QPixmap::fromImage(img);
    12. this->ui.display->setPixmap(img);
    13. }
    To copy to clipboard, switch view to plain text mode 
    As you can see, all video capture details are hidden from GUI, you dont need to worry how VideoPlayer grabs frames, it's enough to know that it emits a signal and connect to it.
    Try to do it this way - hide all video capture details in one class, separate it from gui. Then you can start thinking about how to run it in separate thread.

  4. #4
    Join Date
    May 2011
    Posts
    16
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: How to display QPaint Images from other Threads

    Hi stampede
    I changed my code little bit. I spawn one thread where I am doing decode frame and final Image will give it to Qt MainGUI thread. it's working fine.

    Now my problem is, If I call callback function and spawn decoder thread inside that, it's not working.

    Here is my decoder thread:
    void QVideoDecoder::run()
    {

    timer->setInterval(40);
    connect(timer, SIGNAL(timeout()), this, SLOT(vDecoderFrame()));
    timer->start();
    }

    void QVideoDecoder::vDecoderFrame()
    {
    seekNextFrame();
    emit vDEmitSignal(LastFrame);
    }
    Here is my normal working code:
    void videoPlayer:layAFile() {
    This SLOT belong to MainGUI thread
    loadVideo("/tmp/stream.bin");
    decoderThr->start();
    decoderThr->wait();
    }


    void videoPlayer::displayOnWidget(const QImage& img) {
    printf("We are in videoPlayer::displayOnWidget func()...\n");
    mLabelWidget->setPixmap(QPixmap::fromImage(img));
    }
    This part is working normal way. I am able to see the video on widget

    Problem code:
    void videoPlayer:auseAFile()
    {
    rtspInterface->startDesktopClient();
    This is my callback function (It calls only one time)
    }

    CALLBACK FUNCTION:
    void frameReturn(unsigned char* data, int dataSize, void* decoder) {
    loadVideo("/tmp/stream.bin");
    decoderThr->start();
    Here I am spawning decoder thread
    decoderThr->wait();
    }
    This part is not working...
    I put some debug logs here and It's going and hitting the decoder run function and not calling vDecoderFrame() SLOT from run function.

    Please can you let me know the what I am doing wrong here??
    VijayQt

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to display QPaint Images from other Threads

    Now my problem is, If I call callback function and spawn decoder thread inside that, it's not working.
    Again, I don't understand something - how are you supposed to capture frames ? With frame callback method launched by some kind of underlying video capture library, or manually, calling some getNextFrame() method yourself ?

Similar Threads

  1. How to display DDS images?
    By jamsession in forum Qt Programming
    Replies: 5
    Last Post: 12th June 2013, 22:18
  2. Display images and move them
    By AL in forum Qt Programming
    Replies: 5
    Last Post: 11th February 2010, 17:49
  3. How do I display a list of images?
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 23rd November 2007, 08:49
  4. Is there any way to synchronize the display of images
    By kiransu123 in forum General Programming
    Replies: 2
    Last Post: 17th March 2007, 20:50
  5. Display animated images
    By suresh in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2007, 21:53

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.