Results 1 to 6 of 6

Thread: Problem when returning a QVector from a function

  1. #1
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Problem when returning a QVector from a function

    God day to you my fellow programmers.

    I have a little problem when using two different QVector objects. One is declared as
    Qt Code:
    1. QVector<QDSWireObject*> m_wireVector;
    To copy to clipboard, switch view to plain text mode 
    .
    The other one is declared as
    Qt Code:
    1. QVector< QVector<QDSWireObject*> > m_vectorVector;
    To copy to clipboard, switch view to plain text mode 
    .

    And now i have two different functions which is
    Qt Code:
    1. void QDSMainWindow::deleteWire(QDSWireObject *wire)
    2. {
    3. QVector<QDSWireObject*> objectVector = findObjectVector(wire);
    4.  
    5. if(!objectVector.isEmpty())
    6. {
    7. Q_FOREACH(QDSWireObject* item, objectVector)
    8. {
    9. if(item == wire)
    10. {
    11. int index = objectVector.indexOf(wire);
    12.  
    13. if(item->getConnectedBefore())
    14. objectVector.at(index - 1)->setConnectedAfter(0);
    15.  
    16. if(item->getConnectedAfter())
    17. objectVector.at(index +1 )->setConnectedBefore(0);
    18.  
    19. item->deleteLater();
    20. objectVector.remove(objectVector.indexOf(item));
    21. qDebug() << objectVector.size();
    22. }
    23. }
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 
    This function makes a call to this other function that I am using

    Qt Code:
    1. const QVector<QDSWireObject*>& QDSMainWindow::findObjectVector(QDSWireObject *wire)
    2. {
    3. Q_FOREACH(QVector<QDSWireObject*> item, m_vectorVector)
    4. {
    5. if(item.contains(wire))
    6. {
    7. int index = m_vectorVector.indexOf(item);
    8. return m_vectorVector.at(index);
    9. }
    10. }
    11.  
    12. return QVector<QDSWireObject*>();
    13. }
    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem when returning a QVector from a function

    Look at line 3 of your code, you are making a copy of the vector there. You should be getting a warning from the compiler about returning a reference to a local object as well. By the way, your find method is not very optimal.
    Last edited by wysota; 10th October 2011 at 13:49.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem when returning a QVector from a function

    Not getting any warnings regarding this. I tried to change the function without the the code on line 3.

    Qt Code:
    1. void QDSMainWindow::deleteWire(QDSWireObject *wire)
    2. {
    3. if(!findObjectVector(wire).isEmpty())
    4. {
    5. Q_FOREACH(QDSWireObject* item, findObjectVector(wire))
    6. {
    7. if(item == wire)
    8. {
    9. int index = findObjectVector(wire).indexOf(wire);
    10.  
    11. if(item->getConnectedBefore())
    12. findObjectVector(wire).at(index - 1)->setConnectedAfter(0);
    13.  
    14. if(item->getConnectedAfter())
    15. findObjectVector(wire).at(index +1 )->setConnectedBefore(0);
    16.  
    17. item->deleteLater();
    18. findObjectVector(wire).remove(findObjectVector(wire).indexOf(item));
    19. qDebug() << findObjectVector(wire).size();
    20. }
    21. }
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    and I get the error C:\Documents and Settings\KY\Mina dokument\Dropbox\Qt Creator\DigitalSim\qmake\..\src\src\QDSMainWindow. cpp:204: error: passing 'const QVector<QDSWireObject*>' as 'this' argument of 'void QVector<T>::remove(int) [with T = QDSWireObject*]' discards qualifiers.

    Why is this?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem when returning a QVector from a function

    Because you are passing a const object to a non-const method. By the way... think whether calling findObjectVector() so many times in your deleteWire() method is really a good idea. I know CPU power is cheap nowadays, but is it really THAT cheap to execute the same O(n^2) method with the same argument a couple of times?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2011
    Posts
    63
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Problem when returning a QVector from a function

    I know it is unnecessary to call it that many times but did this as a test to see if it would work instead of copying it.


    Added after 5 minutes:


    Quote Originally Posted by wysota View Post
    Look at line 3 of your code, you are making a copy of the vector there. You should be getting a warning from the compiler about returning a reference to a local object as well. By the way, your find method is not very optimal.
    How could I optimize my find method?
    Last edited by meazza; 10th October 2011 at 14:22.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Problem when returning a QVector from a function

    Your whole approach is incorrect. Instead of trying to fix what you already have, throw it away and rewrite from scratch properly. Return indexes or pointers instead of references.

    Here is what your findObjectVector() can look like:
    Qt Code:
    1. int QDSMainWinwod::findObjectVector(QDSWireObject *wire) {
    2. for(int i=0;i<m_vectorVector.size();++i) {
    3. int index = m_vectorVector[i].indexOf(wire);
    4. if(index>=0) return i;
    5. }
    6. return -1;
    7. }
    To copy to clipboard, switch view to plain text mode 

    It's still suboptimal because eventually you'll have to search through the vector again to find the index of the item, but it's a start... You can improve it like so:
    Qt Code:
    1. int QDSMainWinwod::findObjectVector(QDSWireObject *wire, int *indexInVector = 0) {
    2. for(int i=0;i<m_vectorVector.size();++i) {
    3. int index = m_vectorVector[i].indexOf(wire);
    4. if(index>=0) {
    5. if(indexInVector) *indexInVector = index;
    6. return i;
    7. }
    8. }
    9. return -1;
    10. }
    To copy to clipboard, switch view to plain text mode 

    and then:

    Qt Code:
    1. void QDSMainWindow::deleteWire(QDSWireObject *wire) {
    2. int idx = -1;
    3. int vec = findObjectVector(wire, &idx);
    4. if(vec==-1) return;
    5. m_vectorVector[vec].removeAt(idx);
    6. delete wire;
    7. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    meazza (10th October 2011)

Similar Threads

  1. Replies: 5
    Last Post: 2nd September 2011, 23:11
  2. QVector as function argument
    By stefan in forum Qt Programming
    Replies: 4
    Last Post: 12th May 2011, 12:40
  3. Function returning a QHBoxLayout
    By aomegax in forum Newbie
    Replies: 5
    Last Post: 16th August 2010, 18:19
  4. Big problem with QVector::apend() function
    By Zander87 in forum Qt Programming
    Replies: 2
    Last Post: 3rd April 2010, 21:23
  5. Replies: 12
    Last Post: 14th June 2006, 09:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.