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

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

问题描述

我想知道如何在qt中实现异步文件io?这是甚至可以实现在香草qt或有人需要使用另一个库(例如libuv)来实现这样的东西?我正在看着QDataStream,但即使它是一个流,它不是非阻塞。我想一个解决方案是使一个自定义的QIODevice内部使用libuv,然后可以使用QDataStream,但不知道从哪里开始。任何想法?

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。你可以连接适当的sig / slot来调用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&)));
   ....
 }

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

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