我有耗时的图像加载(图像很大),加载时也会对其进行一些操作.我不想阻止应用程序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 …