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 from a class ?
QtWS25 Last Chance

How to get class name from a class ?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 782 Views
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I want to test a object type in a function, the function is passed QAbstractButton *, in the function I can get the class type using:

    QString strType(pobjButton->metaObject()->className());
    

    strType is either clsQtRadioButton or clsQtPushButton these are my derived class names.

    Is there a way to get a string from the class itself without using an instance? Something like:

    QString strClassName(clsQtRadioButton::metaObject()->className());
    

    I know the above won't work as metaObject() can only be used on an instance.

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      What's wrong with qobject_cast<> (or dynamic_cast<> in plain c++) as already explained to you in your previous (basic c++) question?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      SPlattenS 1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        What's wrong with qobject_cast<> (or dynamic_cast<> in plain c++) as already explained to you in your previous (basic c++) question?

        SPlattenS Offline
        SPlattenS Offline
        SPlatten
        wrote on last edited by
        #3

        @Christian-Ehrlicher , this isn't a cast question, I intend to have a test for the class name then assign a pointer to get a common node pointer from the class.

        Kind Regards,
        Sy

        Christian EhrlicherC 1 Reply Last reply
        0
        • SPlattenS SPlatten

          @Christian-Ehrlicher , this isn't a cast question, I intend to have a test for the class name then assign a pointer to get a common node pointer from the class.

          Christian EhrlicherC Online
          Christian EhrlicherC Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @SPlatten said in How to get class name from a class ?:

          I intend to have a test for the class name then assign a pointer to get a common node pointer from the class.

          And why does dynamic_cast or qobject_cast does not work for this usecase?

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          SPlattenS 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @SPlatten said in How to get class name from a class ?:

            I intend to have a test for the class name then assign a pointer to get a common node pointer from the class.

            And why does dynamic_cast or qobject_cast does not work for this usecase?

            SPlattenS Offline
            SPlattenS Offline
            SPlatten
            wrote on last edited by
            #5

            @Christian-Ehrlicher , ok, that will work, just didn't think of it immediately.

            Kind Regards,
            Sy

            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