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

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.5k 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.
  • F Offline
    F Offline
    frnklu20
    wrote on 26 Apr 2019, 17:14 last edited by
    #3

    YES,exactaly !!!!!

    to start i would add manually some informations in the created object, and when i click it, it would appear a messagebox with the informations of that object (just an ideia i don't know)

    but how can relate the label and the instance?thats the first step!

    M 1 Reply Last reply 26 Apr 2019, 17:19
    0
    • F frnklu20
      26 Apr 2019, 17:14

      YES,exactaly !!!!!

      to start i would add manually some informations in the created object, and when i click it, it would appear a messagebox with the informations of that object (just an ideia i don't know)

      but how can relate the label and the instance?thats the first step!

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 26 Apr 2019, 17:19 last edited by mrjj
      #4

      @frnklu20
      Well there various ways to do that.
      You can add a
      Barramento *MyBarramento;
      to CustomLabel and assign its Barramento when you create label.
      Then it points directly to its data.
      (it can also be a non pointer if u wish)

      Alternatively, you can have the Barramento in a map and use an id to allow label to look it up.
      std::map<QString, Barramento *> BarramentoMap;
      BarramentoMap["Car"] = new Barramento;
      and you give "Car" to label as its data ID.
      This works well if it is not really the CustomLabel that needs the data but something outside of it. So when you click on a label, you can look up data.

      F 1 Reply Last reply 26 Apr 2019, 18:21
      0
      • F Offline
        F Offline
        frnklu20
        wrote on 26 Apr 2019, 18:06 last edited by
        #5

        I think that using a map is the better option, but i've never heard Qmap for example.
        I'm kinf of new in Qt... how Qmap works?

        1 Reply Last reply
        0
        • M mrjj
          26 Apr 2019, 17:19

          @frnklu20
          Well there various ways to do that.
          You can add a
          Barramento *MyBarramento;
          to CustomLabel and assign its Barramento when you create label.
          Then it points directly to its data.
          (it can also be a non pointer if u wish)

          Alternatively, you can have the Barramento in a map and use an id to allow label to look it up.
          std::map<QString, Barramento *> BarramentoMap;
          BarramentoMap["Car"] = new Barramento;
          and you give "Car" to label as its data ID.
          This works well if it is not really the CustomLabel that needs the data but something outside of it. So when you click on a label, you can look up data.

          F Offline
          F Offline
          frnklu20
          wrote on 26 Apr 2019, 18:21 last edited by
          #6

          @mrjj
          I saw some videos about Qmap and map in c++ and it is like a dictionary in python i guess
          The problem is that the labels that i add on the screen don't have a specific name , all of them are named as 'child' as you can see in the code.
          Is that a way that i can use it?

          M 1 Reply Last reply 26 Apr 2019, 22:07
          0
          • F frnklu20
            26 Apr 2019, 18:21

            @mrjj
            I saw some videos about Qmap and map in c++ and it is like a dictionary in python i guess
            The problem is that the labels that i add on the screen don't have a specific name , all of them are named as 'child' as you can see in the code.
            Is that a way that i can use it?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 26 Apr 2019, 22:07 last edited by
            #7

            @frnklu20
            Hi
            Yes map is like a dictionary. You insert a key and its data.
            you can then use key to get data back.

            You dont need to use labels name. you can use a dynamic property that just has to match
            the id you use for its Barramento;
            say
            std::map<QString (this is key type) , Barramento * ( this is data) > BarramentoMap;
            BarramentoMap["Car"] = new Barramento;
            than key Car stores some Barramento

            When you create the CustomLabel
            you can set property
            CustomLabel *lab = new CustomLabel();
            lab->setProperty("datakey", "Car");
            (or simply add a QString datakey to class CustomLabel to hold it )

            in either case, when you click on a Label, you can use its property("datakey") or its
            variable to look the actual data up in the list.

            You could also add new clicked signal to the CustomLabel that includes the data ID so its send to the slot.
            and you can directly look data up.

            1 Reply Last reply
            4
            • F Offline
              F Offline
              frnklu20
              wrote on 29 Apr 2019, 19:12 last edited by
              #8

              Okay, please be patient with me, i really need your help!

              In my mousePressEvent of diagrama.cpp i have:

              void Diagrama::mousePressEvent(QMouseEvent *event)
              {...
              
                          CustomLabel *child = new CustomLabel(this);
                          child->setPixmap(QPixmap(url_barram));
                          child->move(event->x(),event->y());
                          connect(child,&CustomLabel::opendialog, this, &Diagrama::justopendialog);
                          child->show();
                          Barramento *barramento=new Barramento(this);##create a new barramento
                          BarramentoMap["barramento"]=barramento;      
                          child->setProperty("datakey","barramento");##link the new label with the new barramento
              

              And my customlabel class is:

              void CustomLabel::mousePressEvent(QMouseEvent *event){
              
              
                  QPoint mouse_pos=event->pos();
                  diagrama=new Diagrama();
              
                  if(event->button()==Qt::RightButton &&diagrama->modo=="linha"){
              
                  if(mouse_pos.x()<=this->size().width() && mouse_pos.y()<=this->size().height()){
              
                  if(mouse_pos.x()>0 && mouse_pos.y()>0){
                        emit sendMousePosition(this->pos(),this->property("datakey"));
                     }
              

              Now in my signal I emit the position of the label that i clicked and his property("datakey"), can I do it?

              Is that it?Or there is something wrong or missing?

              S 1 Reply Last reply 29 Apr 2019, 19:16
              0
              • F frnklu20
                29 Apr 2019, 19:12

                Okay, please be patient with me, i really need your help!

                In my mousePressEvent of diagrama.cpp i have:

                void Diagrama::mousePressEvent(QMouseEvent *event)
                {...
                
                            CustomLabel *child = new CustomLabel(this);
                            child->setPixmap(QPixmap(url_barram));
                            child->move(event->x(),event->y());
                            connect(child,&CustomLabel::opendialog, this, &Diagrama::justopendialog);
                            child->show();
                            Barramento *barramento=new Barramento(this);##create a new barramento
                            BarramentoMap["barramento"]=barramento;      
                            child->setProperty("datakey","barramento");##link the new label with the new barramento
                

                And my customlabel class is:

                void CustomLabel::mousePressEvent(QMouseEvent *event){
                
                
                    QPoint mouse_pos=event->pos();
                    diagrama=new Diagrama();
                
                    if(event->button()==Qt::RightButton &&diagrama->modo=="linha"){
                
                    if(mouse_pos.x()<=this->size().width() && mouse_pos.y()<=this->size().height()){
                
                    if(mouse_pos.x()>0 && mouse_pos.y()>0){
                          emit sendMousePosition(this->pos(),this->property("datakey"));
                       }
                

                Now in my signal I emit the position of the label that i clicked and his property("datakey"), can I do it?

                Is that it?Or there is something wrong or missing?

                S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 29 Apr 2019, 19:16 last edited by
                #9

                @frnklu20 said in How to add and access informations from dynamically created objects QT:

                QPoint mouse_pos=event->pos();
                diagrama=new Diagrama();
                
                if(event->button()==Qt::RightButton &&diagrama->modo=="linha"){
                

                You just created diagrama , does this test really make sense ?

                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
                1
                • F Offline
                  F Offline
                  frnklu20
                  wrote on 29 Apr 2019, 19:22 last edited by
                  #10

                  yes, it does.
                  In my diagrama class i have some modes and this clicked signal is only emitted when the mode "linha" from diagram is on!

                  Relax, this is not the problem!Do you know if what i did works?

                  M 1 Reply Last reply 29 Apr 2019, 19:29
                  0
                  • F frnklu20
                    29 Apr 2019, 19:22

                    yes, it does.
                    In my diagrama class i have some modes and this clicked signal is only emitted when the mode "linha" from diagram is on!

                    Relax, this is not the problem!Do you know if what i did works?

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 29 Apr 2019, 19:29 last edited by
                    #11

                    @frnklu20
                    Hi
                    Seems fine if you also changed the signal sendMousePosition to also have a QVariant in it parameter list.
                    You could make it a QString and do
                    emit sendMousePosition(this->pos(),this->property("datakey").toString());

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      frnklu20
                      wrote on 29 Apr 2019, 19:43 last edited by frnklu20
                      #12

                      Yes, I did it!
                      This is the slot that handles the signal:

                      void Diagrama::showMousePosition(const QPoint &pos, const QVariant property)
                      {
                          QPoint p=pos;
                          pontos.append(p);##this is to draw a line between the object
                          update();
                          QMessageBox::about(this,"info",property.toString());##to start i want to just open a message box and show the information, it is temporary before i do something nice
                      }
                      

                      There is only one problem, the class barramento is like this:

                      barramento.h

                      class Barramento
                      {
                      
                      public:
                          explicit Barramento(QWidget* parent =0 );
                          Barramento();
                          QString nome="padrão";
                          int num;
                          float tensao;
                          float ang;
                          QPoint pos;
                      };
                      

                      barramento.cpp

                      #include "barramento.h"
                      
                      Barramento::Barramento()
                      {
                      
                      }
                      

                      This class is like this because i don't need any function, I just need to store informations on those variables.

                      And when i run the program it gives me an error:
                      undefined reference to 'Barramento::Barramento(QWidget*)' it happens in the line when i create a new Barramento.

                      M 1 Reply Last reply 29 Apr 2019, 20:19
                      0
                      • F frnklu20
                        29 Apr 2019, 19:43

                        Yes, I did it!
                        This is the slot that handles the signal:

                        void Diagrama::showMousePosition(const QPoint &pos, const QVariant property)
                        {
                            QPoint p=pos;
                            pontos.append(p);##this is to draw a line between the object
                            update();
                            QMessageBox::about(this,"info",property.toString());##to start i want to just open a message box and show the information, it is temporary before i do something nice
                        }
                        

                        There is only one problem, the class barramento is like this:

                        barramento.h

                        class Barramento
                        {
                        
                        public:
                            explicit Barramento(QWidget* parent =0 );
                            Barramento();
                            QString nome="padrão";
                            int num;
                            float tensao;
                            float ang;
                            QPoint pos;
                        };
                        

                        barramento.cpp

                        #include "barramento.h"
                        
                        Barramento::Barramento()
                        {
                        
                        }
                        

                        This class is like this because i don't need any function, I just need to store informations on those variables.

                        And when i run the program it gives me an error:
                        undefined reference to 'Barramento::Barramento(QWidget*)' it happens in the line when i create a new Barramento.

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 29 Apr 2019, 20:19 last edited by
                        #13

                        Hi
                        you need (also)
                        Barramento::Barramento( QWidget* parent )
                        {

                        }

                        however, you dont really need it as its not a widget
                        so you can just delete it completely. (also in .h)

                        class Barramento
                        {
                        public:
                            QString nome="padrão";
                            int num;
                            float tensao;
                            float ang;
                            QPoint pos;
                        };
                        
                        1 Reply Last reply
                        3
                        • F Offline
                          F Offline
                          frnklu20
                          wrote on 30 Apr 2019, 18:28 last edited by
                          #14

                          It worked!!

                          THANK YOU <3

                          You´re an angel man, for real.

                          I only changed a detail:
                          Each barramento represents a diferent object with different informations and etc
                          So i did this:

                          void Diagrama::mousePressEvent(QMouseEvent *event)
                          {...
                          if(modo=="barramento")
                              {
                                  if(event->button()==Qt::RightButton)
                                  {
                                      Barramento *barra=new Barramento(this);
                                      int i=0;
                                      i+=1;
                                      BarramentoMap["barramento"+QString::number(i)]=barramento;
                                      child->setProperty("datakey","barramento"+QString::number(i));
                          }
                          ...
                          

                          every time i click on the screen with the mode barramento on, I add 1 to this "i" variable and now when i click on the label it send me a signal in the form "barramento"+i.toString()

                          dealing with the signal:

                          void Diagrama::showMousePosition(const QPoint &pos, const QVariant property)
                          {
                              QPoint p=pos;
                              pontos.append(p);
                              update();
                              QMessageBox::about(this,"info",BarramentoMap[property.toString()]->nome);
                          }
                          

                          And i can access each object separately!
                          I don't know if it is the best way, but i'm quite happy now that it is working!

                          Thank you again SZ

                          M 1 Reply Last reply 30 Apr 2019, 18:47
                          1
                          • F frnklu20
                            30 Apr 2019, 18:28

                            It worked!!

                            THANK YOU <3

                            You´re an angel man, for real.

                            I only changed a detail:
                            Each barramento represents a diferent object with different informations and etc
                            So i did this:

                            void Diagrama::mousePressEvent(QMouseEvent *event)
                            {...
                            if(modo=="barramento")
                                {
                                    if(event->button()==Qt::RightButton)
                                    {
                                        Barramento *barra=new Barramento(this);
                                        int i=0;
                                        i+=1;
                                        BarramentoMap["barramento"+QString::number(i)]=barramento;
                                        child->setProperty("datakey","barramento"+QString::number(i));
                            }
                            ...
                            

                            every time i click on the screen with the mode barramento on, I add 1 to this "i" variable and now when i click on the label it send me a signal in the form "barramento"+i.toString()

                            dealing with the signal:

                            void Diagrama::showMousePosition(const QPoint &pos, const QVariant property)
                            {
                                QPoint p=pos;
                                pontos.append(p);
                                update();
                                QMessageBox::about(this,"info",BarramentoMap[property.toString()]->nome);
                            }
                            

                            And i can access each object separately!
                            I don't know if it is the best way, but i'm quite happy now that it is working!

                            Thank you again SZ

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 30 Apr 2019, 18:47 last edited by
                            #15

                            @frnklu20
                            Hi
                            Super. :)
                            Looks fine.
                            One note:
                            You say
                            if(event->button()==Qt::RightButton)
                            {
                            Barramento *barra=new Barramento(this);
                            int i=0;
                            i+=1;
                            BarramentoMap["barramento"+QString::number(i)]=barramento;
                            child->setProperty("datakey","barramento"+QString::number(i));

                            so if you would call this code more than once, it would still get "barramento1" as key
                            and override any barramento1 already put there. Might not be an issue but i did wonder.

                            Just a note2, about clean up.
                            I can see you assign
                            Barramento *barra=new Barramento(this); >> the this
                            as parent.
                            Do you have
                            class Barramento : public QObject <<<
                            {

                            so it becomes part of the parent/owner system ?

                            Else, we are currently leaking the Barramento in the list as nothing will delete them on close down.
                            Not a big issues as the OS will clean it up when you close but im just old school and like it to be certain.

                            1 Reply Last reply
                            1
                            • F Offline
                              F Offline
                              frnklu20
                              wrote on 3 May 2019, 13:03 last edited by
                              #16

                              "so if you would call this code more than once, it would still get "barramento1" as key
                              and override any barramento1 already put there. Might not be an issue but i did wonder."

                              That's the point! the user change the informations of the barramento
                              that he wants

                              "so it becomes part of the parent/owner system ?"
                              yes, it becomes part of Diagrama class

                              My point here is when i click on the screen add a label that represents a barramento object created at the same time.We're ok with that now

                              The next step is to add and change informations about it.You already saw the variables in class barramento,so here is what i thought:

                              When i create the label on the screen, i execute a Dialog from the class BarramDialog

                              0_1556887384690_f9d3a643-ddbc-41a8-97de-4edc7bf8e63a-image.png

                              And to get those values, i did:

                              if(modo=="barramento")
                                  {
                                      if(event->button()==Qt::RightButton)
                                      {
                                         int i=0;
                                          i+=1;
                                          CustomLabel *child = new CustomLabel(this);
                                          BarramDialog *barram=new BarramDialog;
                                          barram->setModal(true);
                              
                                          child->setPixmap(QPixmap(url_barram));
                                          child->move(event->x(),event->y());
                                          connect(child, &CustomLabel::sendMousePosition, this, &Diagrama::showMousePosition);
                                          connect(child,&CustomLabel::opendialog, this, &Diagrama::justopendialog);
                                          child->show();
                                          barram->exec();##executing the dialog right after i create the label
                              
                                          Barramento *barramento=new Barramento(this);
                                          barramento->setnum(barram->num);
                                          barramento->setang(barram->ang);
                                          barramento->settensao(barram->tensao);##getting those input values on the BarramDialog
                                          barramento->setnome(barram->nome);
                                          BarramentoMap["barramento"+QString::number(i)]=barramento;
                                          child->setProperty("datakey","barramento"+QString::number(i));
                                      }
                                  }
                              

                              barramento.h

                              class Barramento
                              {
                              
                              public:
                                  explicit Barramento(QWidget* parent =0 );
                                  Barramento();
                                  QString nome="padrão";
                                  int num;
                                  float tensao;
                                  float ang;
                              
                                  void setnome(QString &);
                                  void setnum(int &);
                                  void settensao(float &);#these functions are responsible to change the variable values that i want 
                                  void setang(float &);
                              
                              };
                              

                              "Else, we are currently leaking the Barramento in the list as nothing will delete them on close down."
                              So how can i delete them?
                              " im just old school and like it to be certain." i get it hahaha

                              1 Reply Last reply
                              0
                              • M Offline
                                M Offline
                                mrjj
                                Lifetime Qt Champion
                                wrote on 3 May 2019, 13:08 last edited by mrjj 5 Mar 2019, 13:11
                                #17

                                @frnklu20 said in How to add and access informations from dynamically created objects QT:

                                ok. super its works as you want, no reason for alarm about the id.

                                So how can i delete them?

                                Well if you don't use the parent parameter in
                                explicit Barramento(QWidget* parent =0 );

                                then you can simply loop the and and call delete on each.
                                disclaimer*: fast typed. might have errors.

                                for (auto const& x : BarramentoMap)
                                {
                                delete x.second;
                                }
                                

                                a good place to do it , would be the destructor of the main window. or where the BarramentoMap lives.

                                1 Reply Last reply
                                2
                                • F Offline
                                  F Offline
                                  frnklu20
                                  wrote on 3 May 2019, 17:21 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
                                  • M Offline
                                    M Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 3 May 2019, 20:15 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 3 May 2019, 20:31 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")?

                                      M 1 Reply Last reply 3 May 2019, 20:32
                                      0
                                      • F frnklu20
                                        3 May 2019, 20:31

                                        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")?

                                        M Offline
                                        M Offline
                                        mrjj
                                        Lifetime Qt Champion
                                        wrote on 3 May 2019, 20:32 last edited by mrjj 5 Mar 2019, 20:33
                                        #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 3 May 2019, 20:39 last edited by
                                          #22

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

                                          1 Reply Last reply
                                          1

                                          12/37

                                          29 Apr 2019, 19:43

                                          25 unread
                                          • Login

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