God day to you my fellow programmers.
I have a little problem when using two different QVector objects. One is declared as
QVector<QDSWireObject*> m_wireVector;
QVector<QDSWireObject*> m_wireVector;
To copy to clipboard, switch view to plain text mode
.
The other one is declared as
QVector< QVector<QDSWireObject*> > m_vectorVector;
QVector< QVector<QDSWireObject*> > m_vectorVector;
To copy to clipboard, switch view to plain text mode
.
And now i have two different functions which is
void QDSMainWindow::deleteWire(QDSWireObject *wire)
{
QVector<QDSWireObject*> objectVector = findObjectVector(wire);
if(!objectVector.isEmpty())
{
Q_FOREACH(QDSWireObject* item, objectVector)
{
if(item == wire)
{
int index = objectVector.indexOf(wire);
if(item->getConnectedBefore())
objectVector.at(index - 1)->setConnectedAfter(0);
if(item->getConnectedAfter())
objectVector.at(index +1 )->setConnectedBefore(0);
item->deleteLater();
objectVector.remove(objectVector.indexOf(item));
qDebug() << objectVector.size();
}
}
}
}
void QDSMainWindow::deleteWire(QDSWireObject *wire)
{
QVector<QDSWireObject*> objectVector = findObjectVector(wire);
if(!objectVector.isEmpty())
{
Q_FOREACH(QDSWireObject* item, objectVector)
{
if(item == wire)
{
int index = objectVector.indexOf(wire);
if(item->getConnectedBefore())
objectVector.at(index - 1)->setConnectedAfter(0);
if(item->getConnectedAfter())
objectVector.at(index +1 )->setConnectedBefore(0);
item->deleteLater();
objectVector.remove(objectVector.indexOf(item));
qDebug() << objectVector.size();
}
}
}
}
To copy to clipboard, switch view to plain text mode
This function makes a call to this other function that I am using
const QVector<QDSWireObject*>& QDSMainWindow::findObjectVector(QDSWireObject *wire)
{
Q_FOREACH(QVector<QDSWireObject*> item, m_vectorVector)
{
if(item.contains(wire))
{
int index = m_vectorVector.indexOf(item);
return m_vectorVector.at(index);
}
}
return QVector<QDSWireObject*>();
}
const QVector<QDSWireObject*>& QDSMainWindow::findObjectVector(QDSWireObject *wire)
{
Q_FOREACH(QVector<QDSWireObject*> item, m_vectorVector)
{
if(item.contains(wire))
{
int index = m_vectorVector.indexOf(item);
return m_vectorVector.at(index);
}
}
return QVector<QDSWireObject*>();
}
To copy to clipboard, switch view to plain text mode
And now problem here is that the QVector that the findObjectVector() function returns seems to be a copy of the one from the stored in m_vectorVector. Because when i remove an element it gets removed from the vector and the size changes but if I make several calls to the function I can tell that the vector size of the vector stored in m_vectorVector is not changing. Which I don't really understand why because I am returning a reference to the object.
Hopefully someone can help me out here and see what my problem is.
Bookmarks