Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QQuickItem* object. "Expression must have class type" when using

QQuickItem* object. "Expression must have class type" when using

Scheduled Pinned Locked Moved Solved QML and Qt Quick
c++pythonqml
7 Posts 3 Posters 1.2k 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.
  • H Offline
    H Offline
    Hodenbrecher
    wrote on 24 May 2019, 08:18 last edited by Hodenbrecher
    #1

    hi,

    I'm trying to convert this python code in C++

    def setPresetBase( self ):
            object = self.view.rootObject()
            items = object.childItems()
            parameters = []
            for i in items:
                if type(i) == QtQuick.QQuickItem and i.objectName() != "" and i.objectName() != "EditPanel" and i.objectName() != "EditGrid":
                    parameters.append( [ i.objectName(), i.x(), i.y(), i.z(), i.width(), i.height(), i.isVisible() ] )
            self._preset_base = Preset( "base", parameters )
    

    i started to write this but i can't use w_object, the visual studio tells me that the expression must have a class type. So I can't call childItems().

    void PresetModel::setPresetBase(void)
    		{
    			QQuickItem* w_object = mp_view->rootObject();
    			QList<QQuickItem*> w_items = w_object.childItems();
    		}
    

    That's just a pointer so I don't need to allocate any QQuickItem, right ? What's wrong ?

    J 1 Reply Last reply 24 May 2019, 08:28
    0
    • H Hodenbrecher
      24 May 2019, 08:18

      hi,

      I'm trying to convert this python code in C++

      def setPresetBase( self ):
              object = self.view.rootObject()
              items = object.childItems()
              parameters = []
              for i in items:
                  if type(i) == QtQuick.QQuickItem and i.objectName() != "" and i.objectName() != "EditPanel" and i.objectName() != "EditGrid":
                      parameters.append( [ i.objectName(), i.x(), i.y(), i.z(), i.width(), i.height(), i.isVisible() ] )
              self._preset_base = Preset( "base", parameters )
      

      i started to write this but i can't use w_object, the visual studio tells me that the expression must have a class type. So I can't call childItems().

      void PresetModel::setPresetBase(void)
      		{
      			QQuickItem* w_object = mp_view->rootObject();
      			QList<QQuickItem*> w_items = w_object.childItems();
      		}
      

      That's just a pointer so I don't need to allocate any QQuickItem, right ? What's wrong ?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 24 May 2019, 08:28 last edited by J.Hilk
      #2

      @Hodenbrecher
      I'm not sure, what mp_view is, so I can't say for sure, but my guess is rootObject() returns and actual QObject pointer not a QQuickItem pointer.
      So you'll have to cast it:

      {
      			QQuickItem* w_object = qObject_cast<QQuickItem*> (mp_view->rootObject());
      			QList<QQuickItem*> w_items = w_object.childItems();
      		}
      

      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.

      H 1 Reply Last reply 24 May 2019, 08:52
      1
      • N Offline
        N Offline
        Nifiro
        wrote on 24 May 2019, 08:29 last edited by
        #3

        It's a pointer. Use -> instead of .

        w_object->childItems()

        1 Reply Last reply
        1
        • J J.Hilk
          24 May 2019, 08:28

          @Hodenbrecher
          I'm not sure, what mp_view is, so I can't say for sure, but my guess is rootObject() returns and actual QObject pointer not a QQuickItem pointer.
          So you'll have to cast it:

          {
          			QQuickItem* w_object = qObject_cast<QQuickItem*> (mp_view->rootObject());
          			QList<QQuickItem*> w_items = w_object.childItems();
          		}
          
          H Offline
          H Offline
          Hodenbrecher
          wrote on 24 May 2019, 08:52 last edited by Hodenbrecher
          #4

          @J.Hilk
          thanks for your answer.

          mp_view is a QQuickView*

          QQuickView * mp_view;
          PresetModel::PresetModel(QQuickView * aip_view, QObject * aip_parent/*=nullptr by default*/):mp_view(aip_view)
          {
          }
          

          The documentation says that rootObject returns a QQuickitem* : https://doc.qt.io/qt-5/qquickview.html#rootObject

          However I tried the qobject_cast and the IDE says
          no instance of overloaded function "qobject_cast" matches the argument list
          arguments are: (QQuickItem*)

          @Nifiro
          i tried, of course. But because it did not work I tried with the "."
          Anyway, visual studio underlines the error before any compilation. Just by checking the code.

          The error is weird as I can compile

          QQuickItem* w_object = mp_view->rootObject();
             		if (w_object == nullptr);
          

          But if I try to access to it's attribute... NO !!!

          [edit]
          I can't tell if the pointer is null, there's another error probably not linked.

          int argc = 0; char** argv=nullptr;
          	QGuiApplication(argc, argv);
          
          	QQuickView* view = new QQuickView();//in console : "QPixmap: Must construct a QGuiApplication before a QPixmap
          
          	view->setResizeMode(QQuickView::SizeRootObjectToView);
          	QQmlContext *ctxt = view->rootContext();
          	view->setSource(QString(QML_GENERIC_VIEW_PATH));
          	QQuickItem* w_object = view->rootObject();
          
          	if (w_object == nullptr)printf("\nthe pointer is null\n");
          	else printf("\nthe pointer is not null\n");
          	QList<QQuickItem*> w_items = w_object->childItems();//got the same error here. w_object underlined
          
          J 1 Reply Last reply 24 May 2019, 08:57
          1
          • H Hodenbrecher
            24 May 2019, 08:52

            @J.Hilk
            thanks for your answer.

            mp_view is a QQuickView*

            QQuickView * mp_view;
            PresetModel::PresetModel(QQuickView * aip_view, QObject * aip_parent/*=nullptr by default*/):mp_view(aip_view)
            {
            }
            

            The documentation says that rootObject returns a QQuickitem* : https://doc.qt.io/qt-5/qquickview.html#rootObject

            However I tried the qobject_cast and the IDE says
            no instance of overloaded function "qobject_cast" matches the argument list
            arguments are: (QQuickItem*)

            @Nifiro
            i tried, of course. But because it did not work I tried with the "."
            Anyway, visual studio underlines the error before any compilation. Just by checking the code.

            The error is weird as I can compile

            QQuickItem* w_object = mp_view->rootObject();
               		if (w_object == nullptr);
            

            But if I try to access to it's attribute... NO !!!

            [edit]
            I can't tell if the pointer is null, there's another error probably not linked.

            int argc = 0; char** argv=nullptr;
            	QGuiApplication(argc, argv);
            
            	QQuickView* view = new QQuickView();//in console : "QPixmap: Must construct a QGuiApplication before a QPixmap
            
            	view->setResizeMode(QQuickView::SizeRootObjectToView);
            	QQmlContext *ctxt = view->rootContext();
            	view->setSource(QString(QML_GENERIC_VIEW_PATH));
            	QQuickItem* w_object = view->rootObject();
            
            	if (w_object == nullptr)printf("\nthe pointer is null\n");
            	else printf("\nthe pointer is not null\n");
            	QList<QQuickItem*> w_items = w_object->childItems();//got the same error here. w_object underlined
            
            J Offline
            J Offline
            J.Hilk
            Moderators
            wrote on 24 May 2019, 08:57 last edited by
            #5

            @Hodenbrecher

            possible stupid question, but did you #include <QQuickItem> ?


            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.

            H 1 Reply Last reply 24 May 2019, 09:07
            3
            • J J.Hilk
              24 May 2019, 08:57

              @Hodenbrecher

              possible stupid question, but did you #include <QQuickItem> ?

              H Offline
              H Offline
              Hodenbrecher
              wrote on 24 May 2019, 09:07 last edited by
              #6

              @J.Hilk this is never a stupid question haha

              problem solved. thank you very much

              I didn't know that the program could know the type QQuickItem and compile a QQuickItem* object while missing the include...

              J 1 Reply Last reply 24 May 2019, 09:21
              3
              • H Hodenbrecher
                24 May 2019, 09:07

                @J.Hilk this is never a stupid question haha

                problem solved. thank you very much

                I didn't know that the program could know the type QQuickItem and compile a QQuickItem* object while missing the include...

                J Offline
                J Offline
                J.Hilk
                Moderators
                wrote on 24 May 2019, 09:21 last edited by
                #7

                @Hodenbrecher
                one of the quirks of c++

                QQuickItem is a forward-declared class
                https://en.cppreference.com/w/cpp/language/class#Forward_declaration


                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

                1/7

                24 May 2019, 08:18

                • Login

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