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. C++ QWidget class has no member (n)
Forum Updated to NodeBB v4.3 + New Features

C++ QWidget class has no member (n)

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 571 Views 1 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.
  • P Offline
    P Offline
    Pekoyo
    wrote on last edited by Pekoyo
    #1

    Good day!

    I'm having the following problem:

    I have a custom class named WidgetStyle, inheriting from a QWidget.
    WidgetStyle has a std::string variable called MY_Style, which stores custom information

    I add a lot of these items to a layout and have to iterate through them and get the variable MY_Style stored in them.

    for (int i = 0; i < MyLayout->count(); i++) {
             MyLayout->itemAt(i)->widget()->MY_Style
    }
    

    C++ returns the error : "class QWidget has no member MY_Style"

    , which if they were normal qwidgets they wouldn't have that member, but how do I indicate to c++/QT that the items in the layout are not QWidgets but instead my inherited WidgetStyles that do have that property/member?

    Thank you!

    JonBJ P 2 Replies Last reply
    0
    • P Pekoyo

      Good day!

      I'm having the following problem:

      I have a custom class named WidgetStyle, inheriting from a QWidget.
      WidgetStyle has a std::string variable called MY_Style, which stores custom information

      I add a lot of these items to a layout and have to iterate through them and get the variable MY_Style stored in them.

      for (int i = 0; i < MyLayout->count(); i++) {
               MyLayout->itemAt(i)->widget()->MY_Style
      }
      

      C++ returns the error : "class QWidget has no member MY_Style"

      , which if they were normal qwidgets they wouldn't have that member, but how do I indicate to c++/QT that the items in the layout are not QWidgets but instead my inherited WidgetStyles that do have that property/member?

      Thank you!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #3

      @Pekoyo
      As @Kent-Dorfman says. To that end have a look at qobject_cast<>().

      1 Reply Last reply
      1
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote on last edited by
        #2

        you need to research and learn c++ casts...and related, the use of virtuals.

        widget() is probly returning a qwidget* as its type, so it wont' understand an implicit cast to the subclass.

        1 Reply Last reply
        5
        • P Pekoyo

          Good day!

          I'm having the following problem:

          I have a custom class named WidgetStyle, inheriting from a QWidget.
          WidgetStyle has a std::string variable called MY_Style, which stores custom information

          I add a lot of these items to a layout and have to iterate through them and get the variable MY_Style stored in them.

          for (int i = 0; i < MyLayout->count(); i++) {
                   MyLayout->itemAt(i)->widget()->MY_Style
          }
          

          C++ returns the error : "class QWidget has no member MY_Style"

          , which if they were normal qwidgets they wouldn't have that member, but how do I indicate to c++/QT that the items in the layout are not QWidgets but instead my inherited WidgetStyles that do have that property/member?

          Thank you!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #3

          @Pekoyo
          As @Kent-Dorfman says. To that end have a look at qobject_cast<>().

          1 Reply Last reply
          1
          • P Pekoyo has marked this topic as solved on
          • P Pekoyo

            Good day!

            I'm having the following problem:

            I have a custom class named WidgetStyle, inheriting from a QWidget.
            WidgetStyle has a std::string variable called MY_Style, which stores custom information

            I add a lot of these items to a layout and have to iterate through them and get the variable MY_Style stored in them.

            for (int i = 0; i < MyLayout->count(); i++) {
                     MyLayout->itemAt(i)->widget()->MY_Style
            }
            

            C++ returns the error : "class QWidget has no member MY_Style"

            , which if they were normal qwidgets they wouldn't have that member, but how do I indicate to c++/QT that the items in the layout are not QWidgets but instead my inherited WidgetStyles that do have that property/member?

            Thank you!

            P Offline
            P Offline
            Pekoyo
            wrote on last edited by
            #4

            @Pekoyo Thanks for all the answers: here is my solution if it helps anyone

            in my class WidgetStyle simply add

            
            struct WidgetStyle: QWidget {
                Q_OBJECT
            public:
                WidgetStyle();
            };
            

            qobject_cast<WidgetStyle* >(MYLayout->itemAt(i)->widget())->MY_Style

            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