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. Trying to use QMap to access dynamically added QGraphicObjects
Forum Updated to NodeBB v4.3 + New Features

Trying to use QMap to access dynamically added QGraphicObjects

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 878 Views 2 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
    #1

    Hi,

    I'm adapting the DiagramScene qt example to use pixmap instead of polygons and now i'm trying to use QMap to add an access data of the qgraphic objects that i place on the screen.

    The idea is:
    Besides creating the graphic object and place it on the screen, create another object(a non-graphic one) and use QMap to link this 2 objects

    addItem(item);
    item->setPos(mouseEvent->scenePos()); //creating the graphical object
    Barramento *barramento=new Barramento(this);
    //creating the non-graphical object
    BarramentoMap["barramento"+QString::number(i)]=barramento;
    item->setProperty("datakey","barramento"+QString::number(i));
    //creating a link between  this 2 objects
    

    But i'm having a problem

    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 &);
        void setang(float &);
    
    };
    

    barramento.cpp

    Barramento::Barramento(QWidget *parent)
    {
    }
    
    void Barramento::setnome(QString & new_nome)
    {
        nome=new_nome;
    }
    
    void Barramento::setnum(int & new_num)
    {
        num=new_num;
    }
    
    void Barramento::settensao(float &new_tensao)
    {
        tensao=new_tensao;
    }
    
    void Barramento::setang(float &new_ang)
    {
        ang=new_ang;
    }
    

    DiagramScene is the widget responsible to add graphical itens

    diagramscene.cpp

    .... 
    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)
                {
                    Barramento barramento=new Barramento(this);
                     //error here
                    BarramentoMap["barramento"+QString::number(i)]=barramento;
                    item->setProperty("datakey","barramento"+QString::number(i));
                }
    
                break;
    ....
    

    the error is "no matching constructor fot initialization of 'Barramento'"
    , how can I fix it?is my logic correct?

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

      Hi,

      Why are you passing that "parent" parameter to your Barramento class constructor ? You're not making any use of it neither in your constructor nor the rest of your class.

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

        ok, i removed the parent parameter

        but now it gives me another error:
        "no member named 'setProperty' in 'DiagramItem'" that is the class of the item

        so i cant use QMap with QGraphicsPixmapItem?

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

          From your code you are not using a QMap<Barramento, QGraphicsPixmapItem *> map but a QMap<QString, Barramento> which is not the same. And the setProperty call is unrelated to your QMap use.

          From the looks of it, you are trying to use DiagramItem as if it is a QObject based class, which is not. Not all classes of Qt inherits QObject.

          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 last edited by
            #5

            I didn't know that, thanks

            but now how am i going to link these to objects?
            i thought something like this:

             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]=item;
            }
            

            but it gives the no viable overload operator [] error

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

              How did you declare your map ?

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

                in the first time i defined as
                QMap<QString, Barramento>

                but after you answered me defined this way:

                QMap<Barramento, QGraphicsPixmapItem *> how can i do it?

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

                  Then why are you trying to store a pointer to a Barramento object ?

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

                    I'm trying to link the graphical object to the non-graphic object, and the baramento class represents the data of that specific graphical object on the screen.

                    In the older version of my program i represented the image on the screen in a label, and to make this link that i want to do now i did:

                     Barramento barramento=new Barramento(this);
                     BarramentoMap["barramento"+QString::number(i)]=barramento;
                     item->setProperty("datakey","barramento"+QString::number(i));
                    

                    i defined my Qmap as:

                    QMap<QString, Barramento>
                    

                    that's why i want your help, because that doesn't work anymore with QGraphicsPixmapItem as it did with QLabel
                    So how can i make it work?

                    after you answered me i define my QMap as:

                    QMap<Barramento, QGraphicsPixmapItem >
                    

                    it is not a pointer, i'm sorry.
                    I really need your help, thanks for the attention!

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

                      Barramento barramento=new Barramento(this);

                      This is wrong and can compile. You either want Barramento barramento(this); or Barramento *barramentoPtr = new Barramento(this);

                      To use QMap<QString, Barramento>, you need the first option, for the second you need to declare QMap<QString, Barramento *>.

                      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

                      • Login

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