How to get class name
-
Hi, all
Before qt5.15.2, I always used metaobject() - > classname() to get the class name, but now there is a warning.
How can I eliminate this warning, or am I using it incorrectly?Call to virtual method 'MyClass::metaObject' during construction bypasses virtual dispatch [clang-analyzer-optin.cplusplus.VirtualCall]
Best regards!
-
@tovax said in How to get class name:
Before qt5.15.2, I always used metaobject() - > classname() to get the class name
Why you need to do that?
BTW:
The warning is because you seem to call an overriden, virtual function inside your constructor, which shouldn't be done (ever, doesn't matter what Qt or C++ vesion). -
Where do you call these functions?
QMetaObject::className
is ok but shouldn't be used in the constructor of anyQObject
derived class. You could try theshowEvent
(only if you actually show yourMyClass
widget afterwards)This ensures that your widget was contructed completely
-
@tovax said in How to get class name:
I want to use the class name to create different working directories
What if the class itself provides the directory?
something likeMyClass::getWorkingDir()
-
@Pablo-J-Rogina Hi, thanks!
I don't know if I understand your meaning correctly. I defined the getWorkingDir () method in the class, but I shouldn't call it in the constructor.MyClass::MyClass(QWidget *parent) : QWidget(parent) { QDir dir(getWorkingDir()); if (!dir.exists()) { dir.mkpath(getWorkingDir()); } } QString MyClass::getWorkingDir() { // Warning: Call to virtual method 'MyClass::metaObject' during construction bypasses virtual dispatch return QStringLiteral("%1/%2").arg(QApplication::applicationDirPath(), this->metaObject()->className()); }