Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将数据保存到Qt中的文件_C++_Qt_Qml_Qtcore_Qfile - Fatal编程技术网

C++ 将数据保存到Qt中的文件

C++ 将数据保存到Qt中的文件,c++,qt,qml,qtcore,qfile,C++,Qt,Qml,Qtcore,Qfile,使用Qt在文件中保存和加载数据的最简单方法是什么 您可以提供一个如何执行此操作的示例吗?您可以创建一个用于写入的公共插槽,该插槽将在您的QML信号处理程序中调用 然后,您可以创建一个Q_INVOKABLE方法来读取返回的数据,或者创建一个槽来读取返回值为void的数据。在后一种情况下,如果是用例,您可以拥有一个映射到所需属性的数据属性 总的来说,正确的解决方案取决于更多尚未提供的上下文 不幸的是,该类需要包含Q_对象宏,并且需要继承QObject类,这是由于信号槽机制以及QML引擎在此时围绕QO

使用Qt在文件中保存和加载数据的最简单方法是什么


您可以提供一个如何执行此操作的示例吗?

您可以创建一个用于写入的公共
插槽,该插槽将在您的QML信号处理程序中调用

然后,您可以创建一个
Q_INVOKABLE
方法来读取返回的数据,或者创建一个槽来读取返回值为void的数据。在后一种情况下,如果是用例,您可以拥有一个映射到所需属性的数据属性

总的来说,正确的解决方案取决于更多尚未提供的上下文

不幸的是,该类需要包含
Q_对象
宏,并且需要继承
QObject
类,这是由于信号槽机制以及QML引擎在此时围绕
QObject
构建的事实。这可能会在未来发生变化,希望如此

myclass.h myclass.cpp 完成后,您可以通过以下方式向QML公开此功能:

main.cpp 然后您可以从QML调用这些函数,如下所示:

main.qml
免责声明:我写这段代码就像一篇原始文本,所以它可能无法编译,可能会吃掉婴儿,等等,但它应该展示背后的想法。最后,您将有两个用于读取和写入操作的按钮,以及一个显示读取文件内容的标签。

这是什么类型的数据?到目前为止,你尝试了什么?QT有一个序列化的可能性。在这里,拉斯洛·帕普,我很抱歉我没有回答你的评论,我已经找到了最好的答案,虽然它对我来说不起作用,难怪我是C++的NOOB:无论如何,你真的很感激所有的答案,谢谢。
class MyClass : public QObject
{
    Q_OBJECT
    // Leaving this behind comment since I would not start off with this.
    // Still, it might be interesting for your use case.
    // Q_PROPERTY(QByteArray data READ data WRITE setData NOTIFY dataChanged)
    public:
        MyClass() {} // Dummy for now because it is not the point
        ~MyClass() {} // Dummy for now because it is not the point

   Q_INVOKABLE QByteArray read();
   QByteArray data() const ;

   public slots:
       // void read(); // This would read into the internal variable  for instance
       void write(QByteArray data) const;

   // private:
       // QByteArray m_data;
};
/* 
void MyClass::read()
{
    QFile file("in.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    QTextStream in(&file);
    // You could also use the readAll() method in here.
    // Although be careful for huge files due to memory constraints
    while (!in.atEnd()) {
        QString line = in.readLine();
        m_data.append(line);
    }
}
*/

QByteArray MyClass::read()
{
    QByteArray data;
    QFile file("in.txt");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;

    QTextStream in(&file);
    // You could use readAll() here, too.
    while (!in.atEnd()) {
        QString line = in.readLine();
        data.append(line);
    }

    file.close();
    return data;
}

void MyClass::write(QByteArray data)
{
    QFile file("out.txt");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;

    QTextStream out(&file);
    out.write(data);
    file.close();
}
...
MyClass *myClass = new MyClass();
viewContext->setContextProperty("MyClass", myClass);
...
...
Page {
    Button {
        text: "Read"
        onClicked: readLabel.text = myClass.read()
    }

    Label {
        id: readLabel
    }

    Button {
        text: "Write"
        onClicked: myClass.write()
    }
}
...