如何做到异步文件IO在QT? [英] How to do async file io in qt?

查看:1236
本文介绍了如何做到异步文件IO在QT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何实现异步文件IO在QT?这是即使在香草QT达到或会有人需要使用另一个库(libuv例如)来实现这样的事情?我一直在寻找和QDataStream但即使它是一个流这不是非阻塞。我想一个解决方案是使在内部使用libuv然后可以用和QDataStream使用,但不知道从哪里开始定制了QIODevice。任何想法?

I was wondering how to achieve async file io in qt? Is this even achievable in vanilla qt or would someone need to use another library (libuv for example) to achieve something like this? I was looking at QDataStream but even though it is a "stream" it isn't non blocking. I guess one solution would be to make a custom QIODevice that uses libuv internally which can then be used with QDataStream but not sure where to start. Any ideas?

感谢提供任何帮助。

推荐答案

我想实现一个线程将处理I / O。您可以将相应的信号/插槽从你的主线程的IO线程来激活的IO。可以传递给读/写作为参数信号中的数据。事情是这样的:

I would implement a thread that will handle the I/O. You can connect the appropriate sig/slots to "invoke" the IO from your main thread to the IO thread. You can pass the data to be read/written as a parameter to the signal. Something like this:

class FileIOThread : public QThread
{
public: 
    void run();
public slots: 
    void writeData(QByteArray &)
    void readData(QByteArray &)
};

class MyClass
{
private:
    FileIOThread m_writerThread;
signals: 
    void sendData(QByteArray &);
 ....
};

MyClass::MyClass()
{
    connect(this, SIGNAL(sendData(QByteArray&)),
                  &m_writerThread,SLOT(writeData(QByteArray&)));
   ....
 }

这篇关于如何做到异步文件IO在QT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆