Important: Please read the Qt Code of Conduct -

Problem with synchronizing calls to grabToImage()

  • Hi guys!

    Please bear with me, I am pretty new at this stuff...

    I built a playing card editor in QML (QtQuick 2.7), and use Item.grabToImage() to save a card as a png image.
    The good news: This works fine!

    Now I want to expand the functionality by changing some of the card parameters (e.g. suit, rank, 52 cards in a deck) in a for loop,
    and save all all variants. Let's say card[0] is ace of spades, and card[51] is two of clubs with all the other cards in between.

    for (int i = 0; i<52; i++) {
       changeParameters(specific for each card);
       card.grabToImage(saveSingle, Qt.size(cWidth,cHeight));

    The only thing the callback function (saveSingle) does is the actual Image.saveToFile();

    The problem is that grabToImage() is asynchronous, and the for loop already finished before the first image gets a chance to be saved! Since the two of clubs is the last card in the loop, the callback function gets called 52 times with the same image...

    Any ideas on how to synchronize the for loop with the callback function, so that I get 52 different images?

    Thanks in advance!

  • Lifetime Qt Champion


    What about triggering the changeParameters in your callBack and then call again grabToImage ?

  • Hi SGaist,

    Thanx for your reply!

    I have been playing around a lot yesterday, and decided that I need to have some form of storage to keep all the parameter changes (I will serialize those later on), so I was thinking about letting the for-loop just fill a list model (1 list element has all parameters needed to recreate a card).

    After that I will call grabToImage() once to get things started, and the callback can then keep things going itself while emptying the list.

    So I guess that is basically the same as your suggestion, right?
    Thanks again!

  • Lifetime Qt Champion

    Yep, that's similar.

    You're welcome !

Log in to reply