Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get class name
Forum Updated to NodeBB v4.3 + New Features

How to get class name

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 3.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • tovaxT Offline
    tovaxT Offline
    tovax
    wrote on last edited by
    #1

    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!

    Pl45m4P 1 Reply Last reply
    0
    • tovaxT tovax

      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!

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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).


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      tovaxT 2 Replies Last reply
      2
      • Pl45m4P Pl45m4

        @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).

        tovaxT Offline
        tovaxT Offline
        tovax
        wrote on last edited by tovax
        #3

        @Pl45m4 Hi, thank you for your answer.
        I want to get the class name. In fact, these classes are independent applets. I want to use the class name to create different working directories.

        Pablo J. RoginaP 1 Reply Last reply
        0
        • tovaxT Offline
          tovaxT Offline
          tovax
          wrote on last edited by
          #4
          This post is deleted!
          Pl45m4P 1 Reply Last reply
          0
          • Pl45m4P Pl45m4

            @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).

            tovaxT Offline
            tovaxT Offline
            tovax
            wrote on last edited by
            #5

            @Pl45m4 Oh, I see. Thank you very much.
            Is there any other way to get the class name, please?

            1 Reply Last reply
            0
            • tovaxT tovax

              This post is deleted!

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by
              #6

              @tovax

              Where do you call these functions?
              QMetaObject::className is ok but shouldn't be used in the constructor of any QObject derived class. You could try the showEvent (only if you actually show your MyClass widget afterwards)

              This ensures that your widget was contructed completely

              • https://doc.qt.io/qt-5/qwidget.html#showEvent

              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              tovaxT 1 Reply Last reply
              1
              • Pl45m4P Pl45m4

                @tovax

                Where do you call these functions?
                QMetaObject::className is ok but shouldn't be used in the constructor of any QObject derived class. You could try the showEvent (only if you actually show your MyClass widget afterwards)

                This ensures that your widget was contructed completely

                • https://doc.qt.io/qt-5/qwidget.html#showEvent
                tovaxT Offline
                tovaxT Offline
                tovax
                wrote on last edited by
                #7

                @Pl45m4 I get it.
                Best regards!

                1 Reply Last reply
                0
                • tovaxT tovax

                  @Pl45m4 Hi, thank you for your answer.
                  I want to get the class name. In fact, these classes are independent applets. I want to use the class name to create different working directories.

                  Pablo J. RoginaP Offline
                  Pablo J. RoginaP Offline
                  Pablo J. Rogina
                  wrote on last edited by
                  #8

                  @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 like MyClass::getWorkingDir()

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  tovaxT 1 Reply Last reply
                  2
                  • Pablo J. RoginaP Pablo J. Rogina

                    @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 like MyClass::getWorkingDir()

                    tovaxT Offline
                    tovaxT Offline
                    tovax
                    wrote on last edited by
                    #9

                    @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());
                    }
                    
                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved