Using QtConcurrent to load a Pixmap and paint it

送分小仙女□ 提交于 2019-12-04 17:25:14

Only the main(also called GUI) thread can draw on the screen. The following line from the LoadTilePixmap() function, which you run in a separate thread, I believe, tries to paint the content of your pixmap item on the screen.

temp->tileItem->paint(painter,option,widget);

In the thread you should just load and prepare the image and when the thread is done, signal to the main thread that the image is ready and do the drawing from the main thread.

It's not clear from the code you posted, but are you initializing tileItem to be NULL in the constructor of Tile? If not, that would be a possible explanation for the crash you are seeing.

I solved the issue by avoiding paint and instead using the update function. From whatever documentation I've read update indirectly schedules a paint call. So in the end my code looks like this

void LoadTilePixmap(Tile *temp)
{
        QPixmap p("c:\\qt\\tile\\tile0-0.png");
        temp->tileItem=new QGraphicsPixmapItem;
        temp->tileItem->setPixmap(p);
        temp->update(0,0,511,511);
}

This will cause my overloaded paint function to get called for a second time but this time the if condition is false and it goes into else which paints. Not exactly an optimum solution but it works for now.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!