我将在二进制文件中保存一个结构并稍后加载它.我发现一种方法是使用QVariant.这是我创建的简化的Qt Widget应用程序示例.但是当我运行它时,二进制文件仍然是空的.你能帮我解决吗?另外,有没有更好的方法来做这样的事情?
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QFile>
#include <QFileDialog>
#include <QDataStream>
#include <QString>
#include "mystruct.h"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* create an object of the struct */
myStruct * aStruct = new myStruct;
aStruct->myStringVar = QString("aaaaa");
/* put the object in QVariant */
QVariant aVariant;
aVariant.setValue(aStruct);
/* save the QVariant to binary file */
QFile myFile("myFile");
QDataStream save(&myFile);
save.setVersion(QDataStream::Qt_4_6);
save << aVariant;
myFile.close();
/* load the QVariant from binary file */
QDataStream load(&myFile);
load.setVersion(QDataStream::Qt_4_6);
QVariant theVariant;
load >> theVariant;
QString theVar = theVariant.value<myStruct*>()->myStringVar;
myFile.close();
}
MainWindow::~MainWindow()
{
delete ui;
}
mystruct.h:
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
#include <QMetaType>
#include <QString>
struct myStruct
{
QString myStringVar;
};
Q_DECLARE_METATYPE(myStruct*)
#endif // MYSTRUCT_H
评论:作为参考,this link显示了类似的问题,但我无法正确理解/实施建议.例如,我试图定义一个新的运算符,如here所述,但它不起作用.
解决方法:
首先你不应该保存指针,因为这只是变量的内存地址,应该保存的是值.另一方面,如果要使用QDataStream,则必须覆盖流操作符,最后如果要保存为QVariant,则必须使用qRegisterMetaTypeStreamOperators.除上述内容外,您的代码还有一些错误,例如无法打开QFile.
mystruct.h
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
#include <QString>
#include <QMetaType>
struct myStruct
{
QString myStringVar;
bool operator==(const myStruct & o){
return o.myStringVar == this->myStringVar;
}
friend QDataStream &operator<<(QDataStream &out, const myStruct &rhs){
out << rhs.myStringVar;
return out;
}
friend QDataStream &operator>>(QDataStream &in, myStruct &rhs){
in >> rhs.myStringVar;
return in;
}
};
Q_DECLARE_METATYPE(myStruct)
#endif // MYSTRUCT_H
main.cpp中
#include "mystruct.h"
#include <QDataStream>
#include <QFile>
#include <QVariant>
int main(int argc, char *argv[])
{
qRegisterMetaTypeStreamOperators<myStruct>("myStruct");
// create struct
myStruct aStructIn{"aaa"};
// myStruct to QVariant
QVariant aVariant;
aVariant.setValue(aStructIn);
//open file
QFile myFile("myFile");
if(!myFile.open(QIODevice::WriteOnly))
return -1;
// save QVariant
QDataStream save(&myFile);
save.setVersion(QDataStream::Qt_4_6);
save << aVariant;
myFile.close();
//open file
if(!myFile.open(QIODevice::ReadOnly))
return -1;
// read QVariant
QDataStream load(&myFile);
load.setVersion(QDataStream::Qt_4_6);
QVariant theVariant;
load >> theVariant;
myFile.close();
// QVariant to myStruct
myStruct aStructOut = theVariant.value<myStruct>();
Q_ASSERT(aStructOut == aStructIn);
return 0;
}