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. qobject_cast doesn't work with QWT type
Forum Updated to NodeBB v4.3 + New Features

qobject_cast doesn't work with QWT type

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 818 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.
  • Please_Help_me_DP Offline
    Please_Help_me_DP Offline
    Please_Help_me_D
    wrote on last edited by
    #1

    Hi,

    I use QWT library to plot data.
    After I plotted N-curves I'm trying to get one of them with name "MyCurve"

    QwtPlotCurve curve = plotForm->canvas()->findChild<QwtPlotCurve>(QString("MyCurve"), Qt::FindDirectChildrenOnly);
    

    But the program can't compile this line of code with the errors:

    • C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: error: C2039: 'staticMetaObject': is not a member of 'QwtPlotCurve'
    • C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: ошибка: C2440: 'static_cast': cannot convert from 'QObject *' to 'T' with [T=QwtPlotCurve]

    But if I write:

    QObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly);
    

    it works. But then the qobject_cast<QwtPlotCurve>() throws the error :)
    I googled this errors but didn't understand how to solve the problem

    KroMignonK J.HilkJ 2 Replies Last reply
    0
    • Please_Help_me_DP Please_Help_me_D

      Hi,

      I use QWT library to plot data.
      After I plotted N-curves I'm trying to get one of them with name "MyCurve"

      QwtPlotCurve curve = plotForm->canvas()->findChild<QwtPlotCurve>(QString("MyCurve"), Qt::FindDirectChildrenOnly);
      

      But the program can't compile this line of code with the errors:

      • C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: error: C2039: 'staticMetaObject': is not a member of 'QwtPlotCurve'
      • C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: ошибка: C2440: 'static_cast': cannot convert from 'QObject *' to 'T' with [T=QwtPlotCurve]

      But if I write:

      QObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly);
      

      it works. But then the qobject_cast<QwtPlotCurve>() throws the error :)
      I googled this errors but didn't understand how to solve the problem

      KroMignonK Offline
      KroMignonK Offline
      KroMignon
      wrote on last edited by
      #2

      @Please_Help_me_D As far as I can see, QwtPlotCurve is not a QObject
      d5508f95-d3ce-4bb0-adca-b5846217f165-image.png
      So qobject_cast<QwtPlotCurve>() can not work

      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

      Please_Help_me_DP 1 Reply Last reply
      4
      • Please_Help_me_DP Please_Help_me_D

        Hi,

        I use QWT library to plot data.
        After I plotted N-curves I'm trying to get one of them with name "MyCurve"

        QwtPlotCurve curve = plotForm->canvas()->findChild<QwtPlotCurve>(QString("MyCurve"), Qt::FindDirectChildrenOnly);
        

        But the program can't compile this line of code with the errors:

        • C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: error: C2039: 'staticMetaObject': is not a member of 'QwtPlotCurve'
        • C:\Qt\5.14.1\msvc2017_64\include\QtCore\qobject.h:176: ошибка: C2440: 'static_cast': cannot convert from 'QObject *' to 'T' with [T=QwtPlotCurve]

        But if I write:

        QObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly);
        

        it works. But then the qobject_cast<QwtPlotCurve>() throws the error :)
        I googled this errors but didn't understand how to solve the problem

        J.HilkJ Online
        J.HilkJ Online
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @Please_Help_me_D
        QwtPlotCurve is not a QObject based class. -> qobject_cast won't work.

        use static_cast<QwtPlotCurve>();

        that said, find child shouldn't work either. Object is probably null or invalid -> object_cast fails


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • KroMignonK KroMignon

          @Please_Help_me_D As far as I can see, QwtPlotCurve is not a QObject
          d5508f95-d3ce-4bb0-adca-b5846217f165-image.png
          So qobject_cast<QwtPlotCurve>() can not work

          Please_Help_me_DP Offline
          Please_Help_me_DP Offline
          Please_Help_me_D
          wrote on last edited by
          #4

          @KroMignon @J-Hilk thank you
          I understand that this is more QWT question but still, don't you know how to get plotted item?
          In debug mode QObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly); shows me:
          Безымянный.png

          KroMignonK Pl45m4P 2 Replies Last reply
          0
          • Please_Help_me_DP Please_Help_me_D

            @KroMignon @J-Hilk thank you
            I understand that this is more QWT question but still, don't you know how to get plotted item?
            In debug mode QObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly); shows me:
            Безымянный.png

            KroMignonK Offline
            KroMignonK Offline
            KroMignon
            wrote on last edited by
            #5

            @Please_Help_me_D said in qobject_cast doesn't work with QWT type:

            I understand that this is more QWT question but still, don't you know how to get plotted item?

            Sorry I think I can not help you, I never used QWT and I don't understand what you want to extract from QwtPlot.
            Perhaps reading documentation could help you => https://qwt.sourceforge.io/class_qwt_plot.html

            It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

            1 Reply Last reply
            2
            • Please_Help_me_DP Please_Help_me_D

              @KroMignon @J-Hilk thank you
              I understand that this is more QWT question but still, don't you know how to get plotted item?
              In debug mode QObject curve = plotForm->canvas()->findChild<QObject>(QString("MyCurve"), Qt::FindDirectChildrenOnly); shows me:
              Безымянный.png

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

              @Please_Help_me_D said in qobject_cast doesn't work with QWT type:

              how to get plotted item?

              You already have the itemList. You can get a list with QwtPlotCurve-items only by passing Rtti_PlotCurve (plotForm->itemList( Rtti_PlotCurve ); ). Then you can iterate through the list and get your QwtPlotCurve(s).

              https://qwt.sourceforge.io/class_qwt_plot_dict.html#a49d60b01e53a33423987aa80559449f9

              There is also infoToItem()
              https://qwt.sourceforge.io/class_qwt_plot.html#aa951d433d8eba6a47bfd8f0537090c82


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

              ~E. W. Dijkstra

              Please_Help_me_DP 1 Reply Last reply
              1
              • A Offline
                A Offline
                Astrinus
                wrote on last edited by
                #7

                QwtPlot inherits from QwtPlotDict, that has an itemList() method. Have you tried to iterate over it, comparing the item title() and maybe the rtti() value? Just a guess from five minutes into Qwt documentation, never used.
                Furthermore, I think findChild should be used with a pointer type, even if here it is useless...

                1 Reply Last reply
                1
                • Pl45m4P Pl45m4

                  @Please_Help_me_D said in qobject_cast doesn't work with QWT type:

                  how to get plotted item?

                  You already have the itemList. You can get a list with QwtPlotCurve-items only by passing Rtti_PlotCurve (plotForm->itemList( Rtti_PlotCurve ); ). Then you can iterate through the list and get your QwtPlotCurve(s).

                  https://qwt.sourceforge.io/class_qwt_plot_dict.html#a49d60b01e53a33423987aa80559449f9

                  There is also infoToItem()
                  https://qwt.sourceforge.io/class_qwt_plot.html#aa951d433d8eba6a47bfd8f0537090c82

                  Please_Help_me_DP Offline
                  Please_Help_me_DP Offline
                  Please_Help_me_D
                  wrote on last edited by
                  #8

                  @Pl45m4 @Pl45m4 thank you very much!
                  I also read the documentation but sometimes it is difficult to understand the idea of some function. Only now I understood how I could use infoToItem().
                  The last thing now. I find all curves QwtPlotItemList itemList = plotForm->itemList( QwtPlotItem::Rtti_PlotCurve ); but they are stored in QwtPlotItemList type. How to convert QwtPlotItemList[1] to QwtPlotCurve?

                  Pl45m4P 1 Reply Last reply
                  0
                  • Please_Help_me_DP Please_Help_me_D

                    @Pl45m4 @Pl45m4 thank you very much!
                    I also read the documentation but sometimes it is difficult to understand the idea of some function. Only now I understood how I could use infoToItem().
                    The last thing now. I find all curves QwtPlotItemList itemList = plotForm->itemList( QwtPlotItem::Rtti_PlotCurve ); but they are stored in QwtPlotItemList type. How to convert QwtPlotItemList[1] to QwtPlotCurve?

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

                    @Please_Help_me_D said in qobject_cast doesn't work with QWT type:

                    How to convert QwtPlotItemList[1] to QwtPlotCurve?

                    QwtPlotItemIterator it;
                    for ( it = itemList.begin(); it != itemList.end(); ++it )
                    { 
                      QwtPlotCurve* curve = static_cast<QwtPlotCurve *>( *it );
                    }
                    

                    Or if you have just one item and you know that there aren't more, you can do something like this:

                     if(itemList.size() > 0 && itemList[0] != nullptr)
                              QwtPlotCurve* curve = static_cast<QwtPlotCurve *>( itemList[0] );
                    

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

                    ~E. W. Dijkstra

                    Please_Help_me_DP 1 Reply Last reply
                    3
                    • Pl45m4P Pl45m4

                      @Please_Help_me_D said in qobject_cast doesn't work with QWT type:

                      How to convert QwtPlotItemList[1] to QwtPlotCurve?

                      QwtPlotItemIterator it;
                      for ( it = itemList.begin(); it != itemList.end(); ++it )
                      { 
                        QwtPlotCurve* curve = static_cast<QwtPlotCurve *>( *it );
                      }
                      

                      Or if you have just one item and you know that there aren't more, you can do something like this:

                       if(itemList.size() > 0 && itemList[0] != nullptr)
                                QwtPlotCurve* curve = static_cast<QwtPlotCurve *>( itemList[0] );
                      
                      Please_Help_me_DP Offline
                      Please_Help_me_DP Offline
                      Please_Help_me_D
                      wrote on last edited by
                      #10

                      @Pl45m4 thank you a lot!

                      1 Reply Last reply
                      1

                      • Login

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