7

In Visual Studio 2012 (C++) it is enough to declare variable at the beginning for it to have global scope and at the same time set the value for the variable. How to create global variable and initialize in Qt 5.3?

I tried to declare it in header file, but I have a problem: "only static const integral data members can be be initialized within a class".

Thanks in advance!

Pavlo Zvarych
  • 615
  • 2
  • 9
  • 13
  • 1
    (1) Post text, not images. (2) It appears that you are dealing with ordinary class member variables (not global variables), so the question doesn't make sense. The simplest fix would be to move the member variable initialization to the class constructor. (3) This question doesn't appear to have anything to do with Qt or OpenCV. – nobody May 28 '14 at 21:48
  • 1
    Those are members, not globals, and the initialisation shouldn't work in VS either. Also, how is `oVideoWriter` invisible? Are you trying to use it outside of a member function? – molbdnilo May 28 '14 at 21:55
  • global vars are evil. use class variables instead. – berak May 29 '14 at 06:07
  • @AndrewMedico OK, I did it.(2)How the question make sense? I tried to initialize member variables (dialog.cpp) and still get message: "unused variable". (3)Maybe the question doesn't appear to have anything to do with OpenCV... but it's about QT and how to create global variables here and initialize them. – Pavlo Zvarych May 29 '14 at 13:23
  • @molbdnilo I meant, that I got message "undeclared identifier" in dialog.cpp, though I declare this variable in dialog.h. Sorry, I don't understand your last question. – Pavlo Zvarych May 29 '14 at 13:27

1 Answers1

17

Global Variables

To create a "global" variable, you need to make it available to everyone and you need to make it declared once, and only once.

globals.h

#ifndef GLOBALS_H
#define GLOBALS_H

#include <qtglobal.h>

// ALL THE GLOBAL DECLARATIONS

// don't use #include <QString> here, instead do this:

QT_BEGIN_NAMESPACE
class QString;
QT_END_NAMESPACE

// that way you aren't compiling QString into every header file you put this in...
// aka faster build times.

#define MAGIC_NUM 42

extern qreal g_some_double; // Note the important use of extern!
extern QString g_some_string;

#endif // GLOBALS_H

globals.cpp

#include "globals.h"
#include <QString>

// ALL THE GLOBAL DEFINITIONS

qreal g_some_double = 0.5;
QString g_some_string = "Hello Globals";

Now at the top of any file you want access to these dangerous global variables is:

#include "globals.h"

// ...

// short example of usage

qDebug() << g_some_string << MAGIC_NUM;

g_some_double += 0.1;

In summary, globals.h has all the prototypes for your global functions and variables, and then they are described in globals.cpp.

public static member variables and methods

For these they are similar to the above example, but they are included in your class.

myclass.h

class MyClass
{
    public:
    static int s_count; // declaration
}

myclass.cpp

int MyClass::s_count = 0; // initial definition

Then from any part of your program you can put:

qDebug() << MyClass::s_count;

or

MyClass::s_count++;// etc

DISCLAIMER:

In general globals and public static members are kind of dangerous/frowned upon, especially if you aren't sure what you are doing. All the OOP goodness of Objects and Methods and Private and Protected kind of go out the window, and readability goes down, too. And maintainability can get messy. See the more in depth SO answer below:

Are global variables bad?

QSettings

For some global settings, I've used QSettings with great success.

http://qt-project.org/doc/qt-5/QSettings.html#details

https://stackoverflow.com/a/17554182/999943

Hope that helps.

phyatt
  • 18,472
  • 5
  • 61
  • 80
  • Why `don't use don't use #include ` in global.h? Why you capsulated `Class QString;` in `QT_BEGIN_NAMESPACE` and `QT_END_NAMESPACE`? – S.M.Mousavi Mar 18 '16 at 09:54
  • Your Disclaimer is very helpful. All readers, please read it carefully. – S.M.Mousavi Mar 18 '16 at 09:57
  • I described why in the code's comments. It speeds up compile time. – phyatt Mar 18 '16 at 14:23
  • The forward declaration `class QString;` gets wrapped with the Qt namespace because `QString` is part of the Qt namespace. And in several examples, I've seen that practice. A better/longer explanation is here: http://wiki.qt.io/Qt-In-Namespace – phyatt Mar 18 '16 at 16:59
  • Whe you say *any part*, is it include inside `void` definitions? – Sigur Jul 26 '18 at 02:00
  • Void indicates it is a function without a return. Yes, globals work inside functions. – phyatt Jul 26 '18 at 02:15