2

I would like to have file in my Qt C++ project that holds all global variables.

Something like: mainvar.h

That I could include in the files where I need access to the globals.

They would hold constants where my settings file is, where I can find the stylesheet and other stuff.

At the moment I have a header file but get the Linker Error: LNK2005 for double definition.

Then, there is #define and extern ...

What's the most modern solution for this and how should I approach this ? The thing is, I dont know yet how many global variables I am going to have.

I come from PHP and things are slightley different over there. Looking forward to your help.

Edit1 :

OK, after the comments, here is some code: I have ejected the old code, so this is holding already the code from the first comment to create a Class:

QFile settingsFile(GlobalSettings::getSettingsFileStr());
if (!settingsFile.exists())
{
    //create the settings file
    settingsFile.open(QIODevice::ReadWrite);
    settingsFile.close();
    loadSettings("://src/settings_default.ini");
}
else
{
    loadSettings("");
}

In this Setting file I have saved weather a TrayIcon is shown or not when the user closes the application.

Later I want to use this setting in the MainWindow Class. Such as:

QSettings settings(GlobalSettings::getSettingsFileStr(), QSettings::IniFormat);

if ( settings.value("general/trayIcon").toBool() )
{
    event->ignore();
    showTrayIcon();
    this->hide();
}

I hope the edit adds clarity.

All the best, Richard

Richard
  • 1,173
  • 2
  • 20
  • 43
  • 2
    Could you create a class with all the constants in it? – Floris Aug 02 '13 at 00:03
  • 1
    Can you give code for the error? – Neil Kirk Aug 02 '13 at 00:25
  • "Then, there is #define and extern ..." ... and it was so, and God said it was good. Exactly where and how are these being used. `extern`, if used correctly, is how you link global variables (although I disagree with global variables). – Suedocode Aug 02 '13 at 00:27
  • I have edited the inital question. I have used a GlobalSetting Class for now. I guess, thats the best solution? @Aggieboy, also I do not want to use global variables floating around the project. – Richard Aug 02 '13 at 00:40
  • What symbol is the linker error giving? Is it `getSettingsFileStr` or something? – Suedocode Aug 02 '13 at 00:42
  • No, sorry, as said, this is the new code, there is no linker problem anymore, the linker problem was regaring my implementation of a single mainvar.h file that hold const QString settingFileStr. This file was included in two different places, and the Linker then complained about a double definition of the settingFileStr. But this is solved. – Richard Aug 02 '13 at 00:46
  • If this code is going to be around for any length of time at all, then my advice is to not use 'globals' or 'singletons'. Passing the settings around as a parameter may seem like a PITA, but doing so makes your program easier to change and most importantly easier to test. If you have some spare time, the answers here will do more justice than I can: http://stackoverflow.com/q/137975/11698 – Richard Corden Jan 27 '15 at 09:02

2 Answers2

3

I think you want is something like:

mainvar.cpp

QString settingsFileStr; //some initialization too probably

mainvar.h

extern Qstring settingsFileStr;

main.cpp

#include <iostream>
#include "mainvar.h"

using namespace std;

int main(){
  //do stuff with settingsFileStr
}

However encompassing the settings statically in another class does the same thing and is slightly more organized.

Suedocode
  • 2,414
  • 3
  • 18
  • 38
2

Since you are using C++ and QT I would propose following solution (I think it's more readable within sources and give some flexibility for extensions)..

global.h

#ifndef _GLOBALS_H
#define _GLOBALS_H

class Globals {
protected:
    static Globals * m_pInstance;

public:

    //Globals * instance();
    static Globals * instance();

    // your global setters/getter

    QString   getMyString();
    void setMyString(const QString & val);

protected:

    QString m_myString;

private:
    Globals();
};

#endif

globals.cpp

#include "globals.h"

Globals * Globals::m_pInstance  = NULL;
Globals * Globals::instance() {
      if (!m_pInstance)
          m_pInstance = new Globals();
      return m_pInstance;
}

Globals::Globals() {
   // whatever initialisation code you need to your global vars
}

QString Globals::getMyString() {
   return m_myString;
}

QString Globals::setMyString(const QString & str) {
    m_myStirng = str;
}

etc..

usage:

code.cpp

#include "globals.h"

...

QString getMyVar = Globals::instance()->getMyString();

...
evilruff
  • 3,849
  • 1
  • 13
  • 27