QPair和QMap以及结构体struct的连用

在头文件里面:

定义一个QPair和一个struct结构体

#ifndef TESTCLASS_H
#define TESTCLASS_H

#include 
#include 

namespace Ui {
class TestClass;
}
typedef  QPair OnlyKey;

class TestClass: public QDialog
{
    Q_OBJECT

public:
    explicit TestClass(QWidget *parent = nullptr);
    ~TestClass();

    struct IndexMsg
    {
        int nDataId;
        QString sDataName;
        int nDataSources;
        QString sField;

        void clear()
        {
            nDataId = -1;
        }
    };
private slots:
    void on_pushButton_check_clicked();

    void on_pushButton_cancel_clicked();

private:
    Ui::TestClass *ui;

    QMap> m_mapOnlyToDate;

};

#endif // KQDLGINDEXCHECK_H

在cpp文件里面写:

void TestClass::on_pushButton_check_clicked()
{
    ...此处省略
    QString sIndex = "A-01";
    int nYear = 2019;
    sql = QString("select * from Table");
    query.exec(sql);

    QList listIndexMsg;
    listIndexMsg.clear();
    while(query.next())
    {
        IndexMsg indexToMsg;
        indexToMsg.clear();
        indexToMsg.nDataId = query.value("ID").toInt();
        indexToMsg.sDataName = query.value("NAME").toString();
        indexToMsg.nDataSources = query.value("TYPE").toInt();
        indexToMsg.sField = query.value("FIELD").toString();
        listIndexMsg.append(indexToMsg);
    }

    OnlyKey key(sIndex,nYear);    //当sIndex和nYear组合为一个完全不重复的QPair,就可以把这两个值变成一个类似主键的东西.
    m_mapOnlyToDate[key] = listIndexMsg;

    //取值时
    for(auto it=m_mapZBYearToDate.begin();it!=m_mapZBYearToDate.end();it++)
    {
        in++;
        OnlyKey zb = it.key();
        QList listIndexMsg = it.value();
        QString sIndex = zb.first;
        int nY = zb.second;
    }
    
    for(int i=0;i

将结构体传入到一个类里面:

void TestClass::CheckMsg(TestClass::IndexMsg itMsg)
{
    QString id = itMsg.nDataId;
    QString name = itMsg.sDataName;
    QString sources = itMsg.nDataSources;
    QString field = itMsg.sField;
}

 

QPair和QMap以及结构体struct的连用_第1张图片 海鸟跟鱼相爱 只是一场意外  《珊瑚海》

 

你可能感兴趣的:(♪,--,Qt,♬,--,C\C++)