How to add and access informations from dynamically created objects QT
-
@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 ? -
@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()); -
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. -
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; };
-
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
-
@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. -
"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 classMy 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
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 -
@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.
-
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?
-
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? -
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 dynamicallyvoid 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?
-
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. -
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 signalWhat i did wrong?There is a better way to know if the QGraphicsItem was clicked?
-
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.