2


Hi folks!
Recently I've upgraded my developing environment. Namely, I've moved from Qt 4.8.4 and MSVC 2010 to Qt 5.3.1 and MSVC 2013. The problem I've faced is that my application crashes on launch, and stack trace proves that the crash happens during the initialization of some static class fields.
See the following example:

// header file

class MyClass : QObject
Q_OBJECT
public:
...
private:
    static const QString CLASS_NAME;

// *.cpp file

const QString MyClass::CLASS_NAME = MyClass::tr("FOO"); // crash when calling tr()
const QString MyClass::CLASS_NAME = QObject::tr("FOO"); // but this works normally

During debugging into Qt I've found that the MyClass::tr() method eventually calls QMetaObject::tr() and it appears that all the fields of the QMetaObject instance are NULL. The crash then occurs when referencing some of them.

Notable fact is that the crash is not reproduced on another machine with Ubuntu 14.04 and Qt 5.2.1.

Surely, I could just replace the MyClass name to the QObject one, but my project consists of 63 libraries so I'm afraid of possible translation conficts.

Ivan
  • 588
  • 5
  • 20

1 Answers1

1

Well,

class QObject : 

   static QString tr ( const char * sourceText, const char * disambiguation = 0, int n = -1 )

tr is a static function, that means you cannot refer to the virtual methode table. (see C++ static virtual members?)

The problem is: you can overload the method, but the call to the base object is not called. Not sure how the macro Q_OBJECT is interfearing. But I think it will connect it later.

Did you verfiy if the resulting QString was translated using QObject::tr()?

Not quiet sure if this works. Need to test it.

Edit:

Checked it, indeed it only affect Qt 5.x, but please refer to http://qt-project.org/doc/qt-5/sourcebreaks.html

I remember they changed stuff in the translate api in Qt 5. Probably there could be the mess in some hidden code breaks.

Community
  • 1
  • 1
roalter
  • 259
  • 1
  • 2
  • 13
  • But I didn't overload the method. Its overloaded version is added automatically by the Q_OBJECT macro. Actually there is no difference between the Qt 4 implementation and the 5's one. And the call of tr() on behalf of a child class worked good in the fourth version. BTW, strings are translated by QObject::tr(). – Ivan Sep 19 '14 at 09:44