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 add and access informations from dynamically created objects QT
Forum Updated to NodeBB v4.3 + New Features

How to add and access informations from dynamically created objects QT

Scheduled Pinned Locked Moved Solved General and Desktop
c++ qtqtcreatorc++object
37 Posts 5 Posters 6.9k Views 3 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.
  • F Offline
    F Offline
    frnklu20
    wrote on last edited by
    #18

    Ok! i'll do it

    Just one more question, how can i do to support this link between the label and the class barramento with drag and drop functions?

    diagrama.cpp

    void Diagrama::dragEnterEvent(QDragEnterEvent *event)
    {
        if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
            if (event->source() == this) {
                event->setDropAction(Qt::MoveAction);
                event->accept();
            } else {
                event->acceptProposedAction();
            }
        } else {
            event->ignore();
        }
    }
    
    void Diagrama::dragMoveEvent(QDragMoveEvent *event)
    {
        if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
            if (event->source() == this) {
                event->setDropAction(Qt::MoveAction);
                event->accept();
            } else {
                event->acceptProposedAction();
            }
        } else {
            event->ignore();
        }
    }
    
    void Diagrama::dropEvent(QDropEvent *event)
    {
        if (event->mimeData()->hasFormat("application/x-dnditemdata")) {
            QByteArray itemData = event->mimeData()->data("application/x-dnditemdata");
            QDataStream dataStream(&itemData, QIODevice::ReadOnly);
    
            QPixmap pixmap;
            QPoint offset;
            dataStream >> pixmap >> offset;
    
            CustomLabel *newIcon = new CustomLabel(this);
            newIcon->setPixmap(pixmap);
            newIcon->move(event->pos() - offset);
            newIcon->show();
            newIcon->setAttribute(Qt::WA_DeleteOnClose);
            connect(newIcon, &CustomLabel::sendMousePosition, this, &Diagrama::showMousePosition);
            connect(newIcon,&CustomLabel::opendialog, this, &Diagrama::justopendialog);
            if (event->source() == this) {
                event->setDropAction(Qt::MoveAction);
                event->accept();
            } else {
                event->acceptProposedAction();
            }
        } else {
            event->ignore();
        }
    }
    

    these are the functions responsible for that, and the problem is on
    void Diagrama::dropEvent(QDropEvent *event)

    in this function i create another label with the same pixmap that i'm moving and i delete(just guessing) the old one, so the new one has no barramento object linked to it

    what i thought was to get the property("datakey") of the old label (which is emitted by CustomLabel class) and do something like this:

    new_label->setProperty("datakey",property("datakey"))
    with this new line, the new_label overrides the informations of the old one, wich is not a problem beacuse it doesn't exist anymore.

    But how can i do it? since that i don't have the emitted signal of custom label in drag and drop?

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #19

      Hi
      Since you already pack some data
      (the dataStream >> pixmap >> offset; )
      could you not just include a QString with its datakey ID
      with the pixmap and offset and set it on the newly created label?

      1 Reply Last reply
      0
      • F Offline
        F Offline
        frnklu20
        wrote on last edited by
        #20

        ok, but how?

        let's say I do this:
        QString data;

        dataStream>>pixmap>>offset>>data;

        how the program will know that data=this->property("datakey")?

        mrjjM 1 Reply Last reply
        0
        • F frnklu20

          ok, but how?

          let's say I do this:
          QString data;

          dataStream>>pixmap>>offset>>data;

          how the program will know that data=this->property("datakey")?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #21

          @frnklu20
          hi
          you will put it in your self in startDrag or where you stuff the pixmap and point when drags starts.

          1 Reply Last reply
          1
          • F Offline
            F Offline
            frnklu20
            wrote on last edited by
            #22

            oohhh yeahh
            i got it!!!
            Thank you man <<<<<<33333333

            1 Reply Last reply
            1
            • F Offline
              F Offline
              frnklu20
              wrote on last edited by
              #23

              Hi,

              I have a problem with the same question.Can you help me please?

              mrjjM 1 Reply Last reply
              0
              • F frnklu20

                Hi,

                I have a problem with the same question.Can you help me please?

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #24

                @frnklu20
                Hi
                if it's not super related to this thread,
                then please make a new one.

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  frnklu20
                  wrote on last edited by
                  #25

                  It is!

                  is about how to add and access informations from dynamically created objects!

                  But now instead of using QLabels to portray my image, i'm using QGraphicsPixmapItems.

                  To update my program I started to use QGraphicItems, and like before I want to create a link between a specific graphic item that i added on the screen and a non-graphical object named Barramento.

                  diagramitem.cpp

                  DiagramItem::DiagramItem(DiagramType diagramType, QMenu *contextMenu,
                               QGraphicsItem *parent)
                      : QGraphicsPixmapItem(parent)
                  {
                      QPainter *painter=new QPainter;
                      myDiagramType = diagramType;
                      myContextMenu = contextMenu;
                  
                      QPainterPath path;
                      switch (myDiagramType) {
                          case StartEnd:{
                              QPixmap pixmap(url_trafo);
                              myPolygon = pixmap;
                              break;
                      }
                      case Gerador:{
                              QPixmap pixmap2(url_gerador);
                              myPolygon=pixmap2;
                              break;
                      }
                      case Carga:{
                              QPixmap pixmap3("C:/Users/Lukas/Desktop/simulight/images/carga");
                              myPolygon=pixmap3;
                              break;
                      }
                      default:{
                              QPixmap pixmap4(url_barramento);
                              myPolygon=pixmap4;
                              break;
                      }
                      }
                      setPixmap(myPolygon);
                      setFlag(QGraphicsItem::ItemIsMovable, true);
                      setFlag(QGraphicsItem::ItemIsSelectable, true);
                      setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
                  }
                  

                  that's the class responsible for the items

                  diagramscene.cpp
                  it handles the actions of adding objects on the screen dynamically

                  void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
                  {
                      if (mouseEvent->button() != Qt::LeftButton)
                          return;
                  
                      DiagramItem *item;
                      switch (myMode) {
                          case InsertItem:
                              
                              item = new DiagramItem(myItemType, myItemMenu);
                  
                              addItem(item);
                              item->setPos(mouseEvent->scenePos());
                              emit itemInserted(item);
                              if(myItemType==0)
                              {
                                  int i=0;
                                  i+=1;
                                  Barramento *barramento=new Barramento();
                                  
                                  BarramentoMap["barramento"+QString::number(i)]=barramento;//error1
                                  item->setProperty("datakey","barramento"+QString::number(i)); //error2
                              }
                              break;
                  

                  error1: no viable overloaded '='
                  error2: no member named 'setProperty' in 'DiagramItem'

                  And i'm asking myself if it is the same thing that we did beore, how can i fix it?Is there something that i have to do different whit QGraphicItems?

                  1 Reply Last reply
                  0
                  • mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on last edited by mrjj
                    #26

                    Hi
                    DiagramItem do not have setProperty, so just add a new member variable to hold the "key"
                    QString datakey;
                    so you can just do
                    item->datakey="barramento"+QString::number(i);
                    It would be prittier if you defined an set function
                    item->SetDataKey( "barramento"+QString::number(i) );

                    For error 2, i guess you have the wrong declaration of the map.
                    I can't see it.

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      frnklu20
                      wrote on last edited by
                      #27

                      Hi,

                      Let me explain what i did,

                      I have add and access the information, so i created a custom QGraphicsPixmapItem that emits a signal when the item is clicked.

                      customgraphicsitem.h

                      class CustomGraphicsItem: public QGraphicsPixmapItem
                      {
                      public:
                          CustomGraphicsItem(QGraphicsItem *parent=0);
                      
                           QString datakey;
                           void SetDataKey(QString new_datakey);
                            //i defined here the datakey variable and the function
                      signals:
                          void sendInfo(const QVariant datakey);
                      
                      protected:
                          void mousePressEvent(QMouseEvent *event);
                      };
                      

                      customgraphicsitem.cpp

                      CustomGraphicsItem::CustomGraphicsItem(QGraphicsItem *parent)
                      {
                      
                      }
                      
                      void CustomGraphicsItem::mousePressEvent(QMouseEvent *event)
                      {
                          QPoint mouse_pos=event->pos();
                      
                      
                          if(event->button()==Qt::RightButton){
                              
                      
                          if(mouse_pos.x()<=this->boundingRect().size().width() && mouse_pos.y()<=this->boundingRect().size().height()){
                      //i don't know if it is the best way, if you have a better ideia talk to me
                      
                          if(mouse_pos.x()>0 && mouse_pos.y()>0){
                                emit sendInfo(this->datakey);
                             }
                          }
                          }
                          else {
                              event->ignore();
                          }
                      }
                      void CustomGraphicsItem::SetDataKey(QString new_datakey)
                      {
                          datakey=new_datakey;
                      }
                      

                      Now in diagramscene i have a public slot named receiveinfo

                      diagramscene.cpp

                      void DiagramScene::receiveinfo(const QVariant &datakey)
                      {
                          QMessageBox msgBox;
                          msgBox.setText((datakey).toString());
                          msgBox.exec();
                      }
                      //for now i just want to show the datakey in a messagebox when i click the item
                      void DiagramScene::mousePressEvent(QGraphicsSceneMouseEvent *mouseEvent)
                      {if (mouseEvent->button() != Qt::LeftButton)
                              return;
                      
                          DiagramItem *item;
                          switch (myMode) {
                              case InsertItem:
                                  
                                  item = new DiagramItem(myItemType, myItemMenu);
                      
                                  addItem(item);
                                  item->setPos(mouseEvent->scenePos());
                                  emit itemInserted(item);
                                  if(myItemType==0)
                                  {
                                      int i=0;
                                      i+=1;
                                      Barramento *barramento=new Barramento();
                                      BarramentoMap["barramento"+QString::number(i)]=barramento;
                                      item->SetDataKey("barramento"+QString::number(i));
                                      connect(item, &CustomGraphicsItem::sendInfo, this, &DiagramScene::receiveinfo);
                                      //here i connect the item to the signal and the slots
                                  }
                      

                      What i did wrong? qt guves me 3 errors that i don't know what they mean

                      error1:
                      'staticMetaObject' is not a member of 'QtPivate::FunctionPrivate<void....

                      error2:
                      "qt_metacall" is not a member of 'CustomGraphicItem'

                      error3:
                      static assertion failed: No Q_OBJECT in the class with the signal

                      What i did wrong?There is a better way to know if the QGraphicsItem was clicked?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #28

                        Hi,

                        Not all Qt classes by far inherit QObject. If you want signals and slots in your custom QGraphicsPixmapItem, then you also have to inherit QObject first.

                        The errors are mostly related to that.

                        Error 3 is pretty self-explanatory: you are missing the Q_OBJECT macro in your class declaration.

                        You are likely looking for QGraphicsItem::mouseReleaseEvent.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        3
                        • F Offline
                          F Offline
                          frnklu20
                          wrote on last edited by
                          #29

                          Ok!

                          I fixed the error3

                          class CustomGraphicsItem: public QGraphicsPixmapItem
                          {
                              Q_OBJECT
                          
                          public:
                          

                          And now my mousePressEvent is defined in this way

                          protected:
                              void mousePressEvent(QGraphicsSceneMouseEvent *event);
                          

                          So it's fine i guess

                          but how can i inherit QObject in my custom QGraphicsPixmapItem?Because after i fixed those errors it only appears 1 error:
                          no matching function for call to 'QObject::connectImpl ....
                          that i think it must be from the signal that i'm emitting, so how can i fix this by inheriting qobject?

                          1 Reply Last reply
                          0
                          • mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on last edited by
                            #30

                            Hi

                            • how can i fix this by inheriting qobject?

                            class CustomGraphicsItem: public QObject, public QGraphicsPixmapItem {
                            ...

                            and in cpp, also call it

                            CustomGraphicsItem (QGraphicsItem *parent = 0): QObject(), QGraphicsPixmapItem(parent)
                            {

                            }

                            1 Reply Last reply
                            0
                            • F Offline
                              F Offline
                              frnklu20
                              wrote on last edited by
                              #31

                              I did it:

                              .h

                              class CustomGraphicsItem: public QObject, public QGraphicsPixmapItem
                              {
                                  Q_OBJECT
                              
                              public:
                                  CustomGraphicsItem(QGraphicsItem *parent=0);
                              

                              .cpp

                              CustomGraphicsItem::CustomGraphicsItem(QGraphicsItem *parent):
                                  QObject(), QGraphicsPixmapItem(parent)
                              {
                              

                              But now it gives to me a lot of errors
                              unidefined reference to 'vtable for CustomGraphicsItem
                              undefined reference to 'CustomGraphicsItem::sendInfo(QVariant)
                              undefined reference to 'CustomGraphicsItem::metaObject() const
                              undefined reference to 'CustomGraphicsItem::qt_metacast(char const*)
                              undefined reference to 'CustomGraphicsItem::qt_metacall(QMetaObject::Call,int,void**)

                              why is that happening?

                              mrjjM 1 Reply Last reply
                              0
                              • F frnklu20

                                I did it:

                                .h

                                class CustomGraphicsItem: public QObject, public QGraphicsPixmapItem
                                {
                                    Q_OBJECT
                                
                                public:
                                    CustomGraphicsItem(QGraphicsItem *parent=0);
                                

                                .cpp

                                CustomGraphicsItem::CustomGraphicsItem(QGraphicsItem *parent):
                                    QObject(), QGraphicsPixmapItem(parent)
                                {
                                

                                But now it gives to me a lot of errors
                                unidefined reference to 'vtable for CustomGraphicsItem
                                undefined reference to 'CustomGraphicsItem::sendInfo(QVariant)
                                undefined reference to 'CustomGraphicsItem::metaObject() const
                                undefined reference to 'CustomGraphicsItem::qt_metacast(char const*)
                                undefined reference to 'CustomGraphicsItem::qt_metacall(QMetaObject::Call,int,void**)

                                why is that happening?

                                mrjjM Offline
                                mrjjM Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on last edited by
                                #32

                                Hi
                                Do you actually have those in cpp?
                                The body of the functions, i mean.

                                Also, if yes, you do have them still, then try delete build folder
                                and run rebuild all. (from the menu)

                                1 Reply Last reply
                                1
                                • F Offline
                                  F Offline
                                  frnklu20
                                  wrote on last edited by
                                  #33

                                  Hi,

                                  Sorry for the delay to answer, i was in the period of exams in college.

                                  After delete the build folder and rebuild all the program executed :)
                                  But i don't know why my mousePressEvent of CustomGraphicItem is not working(not emitting any signal) and my drag and drop is not working either like it's causing inteference.

                                  what is happening?

                                  in customgraphicsitem.h the function is defined as:

                                  protected:
                                       void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
                                  

                                  in customgraphicsitem.cpp :

                                  void CustomGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
                                  {
                                      QPointF mouse_pos=event->scenePos();
                                  
                                          if(event->button()==Qt::RightButton){
                                  
                                  
                                          if(mouse_pos.x()<=this->boundingRect().size().width() && mouse_pos.y()<=this->boundingRect().size().height()){
                                  
                                  
                                          if(mouse_pos.x()>0 && mouse_pos.y()>0){
                                                emit sendInfo(this->datakey);
                                             }
                                          }
                                          }
                                          else {
                                              event->ignore();
                                          }
                                  }
                                  

                                  what is the problem with it?

                                  jsulmJ J.HilkJ 2 Replies Last reply
                                  0
                                  • F frnklu20

                                    Hi,

                                    Sorry for the delay to answer, i was in the period of exams in college.

                                    After delete the build folder and rebuild all the program executed :)
                                    But i don't know why my mousePressEvent of CustomGraphicItem is not working(not emitting any signal) and my drag and drop is not working either like it's causing inteference.

                                    what is happening?

                                    in customgraphicsitem.h the function is defined as:

                                    protected:
                                         void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
                                    

                                    in customgraphicsitem.cpp :

                                    void CustomGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
                                    {
                                        QPointF mouse_pos=event->scenePos();
                                    
                                            if(event->button()==Qt::RightButton){
                                    
                                    
                                            if(mouse_pos.x()<=this->boundingRect().size().width() && mouse_pos.y()<=this->boundingRect().size().height()){
                                    
                                    
                                            if(mouse_pos.x()>0 && mouse_pos.y()>0){
                                                  emit sendInfo(this->datakey);
                                               }
                                            }
                                            }
                                            else {
                                                event->ignore();
                                            }
                                    }
                                    

                                    what is the problem with it?

                                    jsulmJ Offline
                                    jsulmJ Offline
                                    jsulm
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #34

                                    @frnklu20 You have several conditions before emitting the signal: did you try to debug to see what happens? I guess one of your "if"s does not match.

                                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    0
                                    • F frnklu20

                                      Hi,

                                      Sorry for the delay to answer, i was in the period of exams in college.

                                      After delete the build folder and rebuild all the program executed :)
                                      But i don't know why my mousePressEvent of CustomGraphicItem is not working(not emitting any signal) and my drag and drop is not working either like it's causing inteference.

                                      what is happening?

                                      in customgraphicsitem.h the function is defined as:

                                      protected:
                                           void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
                                      

                                      in customgraphicsitem.cpp :

                                      void CustomGraphicsItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
                                      {
                                          QPointF mouse_pos=event->scenePos();
                                      
                                              if(event->button()==Qt::RightButton){
                                      
                                      
                                              if(mouse_pos.x()<=this->boundingRect().size().width() && mouse_pos.y()<=this->boundingRect().size().height()){
                                      
                                      
                                              if(mouse_pos.x()>0 && mouse_pos.y()>0){
                                                    emit sendInfo(this->datakey);
                                                 }
                                              }
                                              }
                                              else {
                                                  event->ignore();
                                              }
                                      }
                                      

                                      what is the problem with it?

                                      J.HilkJ Offline
                                      J.HilkJ Offline
                                      J.Hilk
                                      Moderators
                                      wrote on last edited by
                                      #35

                                      @frnklu20
                                      the boundingRect rectangle is most likely not mapped to the scenePos. You should check that


                                      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
                                      1
                                      • F Offline
                                        F Offline
                                        frnklu20
                                        wrote on last edited by
                                        #36

                                        Hi,

                                        I came up with a solution for not using a signal:

                                        mainwindow.cpp

                                         void MainWindow::infoItem()
                                        {
                                            foreach (QGraphicsItem *item, scene->selectedItems()){
                                               if (item->type() == DiagramItem::Type){
                                                   QMessageBox msgBox;
                                                   msgBox.setText((item->data(0)).toString());
                                                   msgBox.exec();
                                               }
                                        
                                            }
                                        }
                                        
                                        void MainWindow::createActions()
                                        {
                                        infoAction = new QAction(QIcon("C:/Users/Lukas/Desktop/simulight/images/info"),tr("&Informações"),this);
                                            infoAction->setShortcut(tr("Ctrl+I"));
                                            infoAction->setStatusTip(tr("Informations about the item"));
                                            connect(infoAction, SIGNAL(triggered()), this, SLOT(infoItem()));
                                        }
                                        

                                        I created this action that by clicking in the button, it appears a messagebox with the data stored in this object.
                                        The problem is, the foreach is defined like this:

                                        foreach (QGraphicsItem *item, scene->selectedItems())
                                        

                                        and i'm using the customgraphicsitem, ok i know that its parent is QGraphicItem but i want to use the functions used in customgraphicsitem to store the data
                                        I'm using right now the fucntion setData() and data() but i don't know if htey're good enough for this.
                                        How can i fix it?

                                        1 Reply Last reply
                                        0
                                        • mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on last edited by mrjj
                                          #37

                                          Hi
                                          You can use
                                          https://doc.qt.io/qt-5/qgraphicsitem.html#qgraphicsitem_cast
                                          and try cast to your child type if it really is, you can then use its members.
                                          so check for NULL and note the docs saying " reimplement the type() function"

                                          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