How to add and access informations from dynamically created objects QT
-
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.
-
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? -
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)
{}
-
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?
-
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?
-
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? -
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"