Trying to use QMap to access dynamically added QGraphicObjects
-
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 objectsaddItem(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? -
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.
-
From your code you are not using a
QMap<Barramento, QGraphicsPixmapItem *>
map but aQMap<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.
-
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
-
How did you declare your map ?
-
Then why are you trying to store a pointer to a Barramento object ?
-
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! -
Barramento barramento=new Barramento(this);
This is wrong and can compile. You either want
Barramento barramento(this);
orBarramento *barramentoPtr = new Barramento(this);
To use
QMap<QString, Barramento>
, you need the first option, for the second you need to declareQMap<QString, Barramento *>
.