Important: Please read the Qt Code of Conduct -

Global variable question

  • I'm sure this is a rookie error for many but I have a QHash declared in one file and is being modified and populated by an initialization thread but when I try to access that contents of the QHash in a separate thread it appears as though it is empty. I can not seem to understand why.

    my map is declared like so
    static QHash<int32_t, Parameter*> dirtyParamsByIdx;

    and I've tried with and without an extern declaration in the C++ file that is referencing it from the triggering thread.

    extern QHash<int32_t, Parameter*> dirtyParamsByIdx;

    the class where i seem to be able to populate the contents on the qhash map is a QObject and the one that does not seem to reference the populated object is not a QObject class and is just a vanilla c++ class. do all classes need to be QObjects to manipulate static global variables?

    here is where we subscribe to parameters and i do see the size of the QHash collection growing:

    void Parameter::attributeUpdated(QOpcUa::NodeAttribute attr, const QVariant &value)
        qInfo() << "Parameter::attributeUpdated() " << pData->fieldName << " " << attr << " type " << value.type() << " updated to value=" << value.toInt();
        if (pData->type == PTYPE_INTEGER) {
            pData->ivalue = value.toInt();
        } else if (pData->type == PTYPE_FLOAT) {
            pData->fvalue = value.toFloat();
        } else if (pData->type == PTYPE_STRING) {
            // not tested
            strncpy(pData->svalue, value.toString().toLocal8Bit().data(), sizeof(pData->svalue)-1);
            pData->svalue[sizeof(pData->svalue)-1] = '\0';
        pData->dirty = 1;
        if (callUpdateAlgorithm != 1) {
            callUpdateAlgorithm = 1;
        dirtyParamsByIdx.insert(pData->idx, this);

    Any help is greatly appreciated.

  • Declaring a static global variable in the header file is just wrong.
    Every source file which includes that header will get a brand new global variable...

  • @Bonnie how would I properly declare a static global variable?

  • @James-Mark-Chan
    Why do you need a static global variable?
    Do you fully understand the three usages of static keyword?
    Unlike a class member, a global variable with static storage duration doesn't need to be declared as static.
    In your case, you just need to move QHash<int32_t, Parameter*> dirtyParamsByIdx; from dat_Params.h to a source file (without static keyword);
    And other source files should use it through insp_Main.h.

  • @Bonnie Probably a habit from being a java developer. in java all variables are scoped to the class they are declared in so most global variables are naturally static. Looks like I can use static or just plain old global variables for Qt but agree now that it does not make sense to do both.

  • Lifetime Qt Champion

    @James-Mark-Chan said in Global variable question:

    plain old global variables for Qt

    Avoid global variables in general (usually bad design) and more so when using Qt. Classes derived from QObject are not allowed to be initialised before QApplication instance is initialised!

Log in to reply