不同文件的类成员变量的访问

来源:互联网 发布:qq头像源码大全 编辑:程序博客网 时间:2024/06/05 01:07

不在一个文件里的两个类,其中一个类的成员函数访问另一个类的成员变量。

主要体现在读取配置文件,我们希望程序模块化、低耦合,为解析配置文件的函数单独建一个文件,方便以后复用。

如读取配置文件的头文件xmlparse.h

#ifndef XMLPARSE_H
#define XMLPARSE_H
#include <QString>
class XMLParse
{
public:
    XMLParse();
    ~XMLParse();
    bool xmlParse(QString);
public:
    QString m_serverIP;
    QString m_dbNmae;
    QString m_userName;
    QString m_password;
};
#endif // XMLPARSE_H

xmlparse.cpp

#include "xmlparse.h"
#include <QDomElement>
#include <QFile>
#include <QDebug>
XMLParse::XMLParse()
{
//    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("GB2312"));
    QFile *file;
    QString  filename = "Config.xml";
    if(file->exists("Config.xml"))
    {
        xmlParse(filename);
    }
}
XMLParse::~XMLParse()
{
    //normal, do nothing
}
bool XMLParse::xmlParse(QString fileName)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QFile::Text))
        qDebug() << "open for read error..." ;
    QString errorStr;
    int errorLine;
    int errorColumn;
    QDomDocument doc;
    if (!doc.setContent(&file, false, &errorStr, &errorLine, &errorColumn))
    {
        qDebug() << "setcontent error..." ;
        file.close();
    }
    file.close();
    QDomElement root = doc.documentElement();
    if (root.tagName() != "serverConfig")
    {
        qDebug()<<"root.tagname != serverConfig..." ;
    }
    QDomNode ipNode = root.firstChildElement("db_serverIP");
    m_serverIP = ipNode.toElement().text();
    QDomNode nameNode = root.firstChildElement("db_Name");
    m_dbNmae = nameNode.toElement().text();
    QDomNode userNode = root.firstChildElement("db_userName");
    m_userName = userNode.toElement().text();
    QDomNode passwordNode = root.firstChildElement("db_password");
    m_password = passwordNode.toElement().text();
    return true;
}
Config.xml

<?xml version='1.0' encoding='GB2312'?>
<serverConfig>
<db_serverIP>192.168.2.11</db_serverIP>
<db_Name>talkback</db_Name>
<db_userName>root</db_userName>
<db_password>root</db_password>
</serverConfig>

如果在另一个文件的类里要使用xmlparse读取的配置数据,直接在其成员函数里声明xmlparse类对象是无法访问xmlparse成员变量的。

可在要使用配置数据的类中声明全局xmlparse类对象,这样在其成员函数中就可正常调用xmlparse成员变量。

如另一个类.h

class Database
{
public:
    Database();
    ~Database();
public:
    static bool openDatabase(); //打开数据库
    static void closeDatabase();// 关闭数据库
public:
    static QSqlDatabase m_db;
};
实现文件.cpp

QSqlDatabase Database::m_db = QSqlDatabase::addDatabase("QMYSQL");
XMLParse xmlparse;

bool Database::openDatabase()
{
    QStringList driverList = QSqlDatabase::drivers();
//    qDebug() <<"SUPPORT" <<driverList;
    m_db.setHostName(xmlparse.m_serverIP);
    m_db.setDatabaseName(xmlparse.m_dbNmae);
    m_db.setUserName(xmlparse.m_userName);
    m_db.setPassword(xmlparse.m_password);
    bool ok = m_db.open();
    if(!ok)
    {
        QMessageBox::critical(0, QObject::tr(" 连接数据库失败!!! "), m_db.lastError().text());
        return false;
    }
    else
    {
        qDebug() << "open database successful";
        return true;
    }
}
这样在使用xmlparse成员变量之前先声明全局类对象方可正常访问xmlparse类读取的配置数据。

0 0
原创粉丝点击