C++程序设计之Qt标准容器类的序列化与反序列化

C++程序设计,Qt中使用QDataStream类实现对象序列化与反序列化。Boost准标准库提供对标准模板库的序列化与反序列化,Qt暂时不提供此功能,需要我们自己写代码实现。具体过程如下:

一、序列化

QDataStream &operator<<(QDataStream &stream, const Data &data)
{
    int count = data.vec.size();
    stream << count;
    for(int i=0; i<count; i++)
    {
        stream << data.vec.at(i);
    }
    return stream;
}

二、反序列化

QDataStream &operator>>(QDataStream &stream, Data &data)
{
    int count;
    stream >> count;
    data.vec.resize(count);
    QString str;
    for(int i=0; i<count; i++)
    {
        stream >> str;
        data.vec[i] = str;
    }
    return stream;
}

Continue reading

Windows程序设计之控制台程序的定时器使用方法

一、函数原型

VOID CALLBACK TimerProc(
  HWND hwnd,         // handle to window
  UINT uMsg,         // WM_TIMER message
  UINT_PTR idEvent,  // timer identifier
  DWORD dwTime       // current system time
);

UINT_PTR SetTimer(
  HWND hWnd,              // handle to window
  UINT_PTR nIDEvent,      // timer identifier
  UINT uElapse,           // time-out value
  TIMERPROC lpTimerFunc   // timer procedure
);

Continue reading

Windows程序设计之创建、打开、读写文件方法介绍

一、创建或打开文件

HANDLE CreateFile(
  LPCTSTR lpFileName,                         // file name
  DWORD dwDesiredAccess,                      // access mode
  DWORD dwShareMode,                          // share mode
  LPSECURITY_ATTRIBUTES lpSecurityAttributes, // SD
  DWORD dwCreationDisposition,                // how to create
  DWORD dwFlagsAndAttributes,                 // file attributes
  HANDLE hTemplateFile                        // handle to template file
);

Continue reading

C++程序设计之QEventLoop实现进入和退出事件循环

QEventLoop类为我们提供了一种进入和退出事件循环的方法。在任何时候,你都可以创建一个QEventLoop实例,并通过调用exec()来启动一个事件循环。在循环期间,主动调用exit()可以使exec()强制返回。

void Worker::run()
{
    QEventLoop loop;
    QTimer *timer = new QTimer;
    connect(timer, &QTimer::timeout, this, [&]()
    {
        static int count = 0;
        qDebug() << QDateTime::currentDateTime().toString();
        if(++count == 10) loop.quit();
    },
    Qt::DirectConnection);
    timer->start(1000);
    loop.exec();
}

C++程序设计之Boost序列化与反序列化自定义数据结构

Boost Serialization的主要概念是归档。归档的文件是相当于序列化的C++对象的一个字节流。对象可以通过序列化添加到归档文件,反过来也可从归档文件中加载。

一、自定义数据结构

class DateTime
{
public:
    DateTime(int y=0, int m=0, int d=0)
    {year = y; month = m; day = d;}
    
    void Display(void)
    {cout << year << "-" << month << "-" << day << endl;}
    
    template<class Archive>
    friend void serialize(Archive &ar, DateTime &dt, const unsigned int version);
    
private:
    int year;
    int month;
    int day;
};

Continue reading

C++程序设计之Qt自定义数据类型的序列化与反序列化

一、自定义数据类型

class Data_Record
{
public:
    Data_Record(int id = 0, QString name = "", int age = 0, QString addr = "");
    void display();
    friend QDataStream &operator<<(QDataStream &stream, const Data_Record &data); // 序列化
    friend QDataStream &operator>>(QDataStream &stream, Data_Record &data);       // 反序列化

private:
    int ID;
    QString name;
    int age;
    QString addr;
};

Data_Record::Data_Record(int ID, QString name, int age, QString addr)
{
    this->ID = ID;
    this->name = name;
    this->age = age;
    this->addr = addr;
}

void Data_Record::display()
{
    qDebug() << ID << name << age << addr;
}

QDataStream &operator<<(QDataStream &stream, const Data_Record &data)
{
    stream << data.ID << data.name << data.age << data.addr;
    return stream;
}

QDataStream &operator>>(QDataStream &stream, Data_Record &data)
{
    stream >> data.ID >> data.name >> data.age >> data.addr;

    return stream;
}

Continue reading

C++程序设计之Qt基本数据类型的序列化与反序列化

一、Qt基本数据类型的序列化

    QFile file1("QText.dat");
    if(!file1.open(QIODevice::WriteOnly | QIODevice::Text)) return;
    QDataStream out(&file1);

    QString str1 = "中华人民共和国";
    int iData1 = 10000;
    double dData1 = 3.14;
    QVector<int> vec1 = {0,1,2,3,4,5,6,7,8,9};

    out << str1 << iData1 << dData1 << vec1;
    file1.close();

Continue reading