相关疑难解决方法(0)

正确的Qt线程方式

我有耗时的图像加载(图像很大),加载时也会对其进行一些操作.我不想阻止应用程序GUI.

我的想法是在另一个线程中加载图像,发出加载图像的信号,然后用这个图像重绘视图.

我的方法:

void Window::loadImage()
{ 
    ImageLoader* loaderThread = new ImageLoader();
    connect(loaderThread,SIGNAL(imageLoaded()),this,SLOT(imageLoadingFinished());
    loaderThread->loadImage(m_image, m_imagesContainer, m_path);
}
void Window::imageLoadingFinished()
{
    m_imagesContainer->addImage(m_image);
    redrawView();
}

class ImageLoader : public QThread
{
    Q_OBJECT
    public:
       ImageLoader(QObject *parent = 0) : m_image(NULL), m_container(NULL)

       void loadImage(Image* img, Container* cont, std::string path)
       {
            m_image = img;
            m_container = cont;
            ...
            start();
       }
    signals:
       void imageLoaded();
    protected:
       void run()
       {
           //loading image and operations on it
           emit imageLoaded();
       }
    protected:
       Image* m_image;
       Container* m_container;
}
Run Code Online (Sandbox Code Playgroud)

我基于quedcustomtypeQt编写此代码的示例.当在stackoverflow中搜索和搜索时,我也发现子类化QThread …

c++ qt multithreading qthread

6
推荐指数
2
解决办法
1万
查看次数

标签 统计

c++ ×1

multithreading ×1

qt ×1

qthread ×1