Results 1 to 3 of 3

Thread: Segmental fault when using QValueVector

  1. #1
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Question Segmental fault when using QValueVector

    I'm using Qt3.
    The compelling process is OK, but when I run the application I have a segment fault(or segmental fault, I can't remember what is exactly called).
    The code is below:

    oridata.h

    Qt Code:
    1. #ifndef ORIDATA_H
    2. #define ORIDATA_H
    3.  
    4. #include <qnamespace.h>
    5. #include <qstring.h>
    6. #include <qvaluevector.h>
    7.  
    8. class OriData;
    9. typedef QValueVector<OriData> OriDataVector;
    10.  
    11. class OriData
    12. {
    13.  
    14. public:
    15. OriData(double X=0.0, double Y=0.0) {m_X=X;
    16. m_Y=Y;}
    17. private:
    18. double m_X;
    19. double m_Y;
    20. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h

    Qt Code:
    1. #include "oridata.h"
    2.  
    3. class MainWindow:public QMainWindow
    4. {
    5. public:
    6. enum{MAX_ORIDATA=100};
    7. ...........................
    8. private:
    9. ...........................
    10. OriDataVector m_oriData;
    11. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. MainWindow::MainWindow(const QString &filename)
    2. :QMainWindow(0,0,WDestructiveClose)
    3. {
    4. ......................
    5. init();
    6. m_oriData.resize(MAX_ORIDATA);
    7. ...................
    8. }
    9.  
    10. void MainWindow::init()
    11. {
    12. ..................................
    13. ..................................
    14. textEdit = new QTextEdit(this,"textedit");
    15. textEdit->setTextFormat(Qt::PlainText);
    16. textEdit->setFocus();
    17. this->setCentralWidget(textEdit);
    18.  
    19. QString str;
    20. for(int i=0; i<MAX_ORIDATA; i++)
    21. {
    22. QString strX,strY;
    23. double x = m_oriData[i].getX();
    24. double y = m_oriData[i].getY();
    25. strX = strX.setNum(x,'f');
    26. strY = strY.setNum(y,'f');
    27. strX = 'X'+strX;
    28. strY = 'Y'+strY;
    29. str = strX+strY;
    30. textEdit->append(str);
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 

    I guess, the error maybe occurred in the MainWindow::init(), in the "for" circle, because when I comment out the "for" circle, the application runs OK without any error.

    I'm a newbie please help me. Thanks a lot!
    Last edited by jacek; 29th March 2007 at 13:19. Reason: wrapped too long lines

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Segmental fault when using QValueVector

    Swap these two lines:
    Qt Code:
    1. m_oriData.resize(MAX_ORIDATA); // <-- resize the vector before
    2. init(); // <-- the for loop which expects it to have size of MAX_ORIDATA
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Nov 2006
    Posts
    58
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: Segmental fault when using QValueVector

    Thanks jpn, that is correct.
    Now it is Ok.

Similar Threads

  1. Seg fault, but why?
    By Lykurg in forum Newbie
    Replies: 5
    Last Post: 8th March 2007, 22:49
  2. Retreive QValueVector value
    By Rayven in forum Qt Programming
    Replies: 1
    Last Post: 1st March 2007, 17:55
  3. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 16:30

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.