Results 1 to 3 of 3

Thread: Segmentation fault while passing IO/file handles

  1. #1
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Segmentation fault while passing IO/file handles

    I'm not quite sure what can be done with file/IO handles - can they be copied, passed as an argument, etc. I have to be honest, these are still not completely clear to me despite my efforts to understand it.

    I've attached a sample program to get a better idea what I'm trying to do.

    In my program I want to select a csv file (FileSelect) and pass a handle to another class (MyDialog). The file might have headers it its first row (name,surname,eid) or not - that info is provided by a user by (un)ticking a checkbox.

    Bits in MyDialog constructor (init headers) work as I want. However, I cannot pass 'stop 2' in setHeaders as I get "Segmentation fault".

    Any help would be appreciated. Thanks.

    Qt Code:
    1. #include "mydialog.h"
    2.  
    3. MyDialog::MyDialog(QWidget *parent, QIODevice *handle) : QDialog(parent)
    4. {
    5. m_handle = handle;
    6.  
    7. // get the number of columns
    8. QTextStream in(m_handle);
    9. m_nColumns = in.readLine().split(',').length();
    10. qDebug() << "Number of Columns" << m_nColumns;
    11.  
    12. QCheckBox *checkbox = new QCheckBox("&Header row", this);
    13. checkbox->setCheckState(Qt::Checked);
    14.  
    15. // init headers - works fine
    16. m_tableWidget = new QTableWidget(0, m_nColumns);
    17. in.seek(0);
    18. QStringList headers = in.readLine().split(',');
    19. m_tableWidget->setHorizontalHeaderLabels(headers);
    20.  
    21. QVBoxLayout *mainLayout = new QVBoxLayout;
    22. mainLayout->addWidget(checkbox);
    23. mainLayout->addWidget(m_tableWidget);
    24. setLayout(mainLayout);
    25.  
    26. connect (checkbox, SIGNAL(stateChanged(int)), this, SLOT(headerRowStateChanged(int)));
    27. }
    28.  
    29.  
    30. void MyDialog::headerRowStateChanged(int nState)
    31. {
    32. qDebug() << "Checkbox state changed" << nState;
    33.  
    34. if (nState == Qt::Unchecked)
    35. setHeaders(false);
    36. else if (nState == Qt::Checked)
    37. setHeaders(true);
    38.  
    39. return;
    40. }
    41.  
    42. void MyDialog::setHeaders(bool bHeaderRow)
    43. {
    44. QStringList headers;
    45.  
    46. qDebug() << "stop 1";
    47. if (bHeaderRow) {
    48. qDebug() << "stop 2";
    49. QTextStream in(m_handle);
    50. qDebug() << "stop 3";
    51. QStringList headers = in.readLine().split(',');
    52. qDebug() << "stop 4";
    53. } else {
    54. for (int i = 0; i < m_nColumns; i++)
    55. headers << QString::number(i+1);
    56. }
    57. qDebug() << "stop 5";
    58.  
    59. m_tableWidget->setHorizontalHeaderLabels(headers);
    60. qDebug() << "stop 6" << headers;
    61.  
    62. return;
    63. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Segmentation fault while passing IO/file handles

    the problem was in that you created QFile in the stack, try this
    Qt Code:
    1. void FileSelect::processFile()
    2. {
    3. QString strFilename = QFileDialog::getOpenFileName(this, "Select CSV File", ".", "Comma Separated Values (*.csv)");
    4. qDebug() << "got filename" << strFilename;
    5.  
    6. QFile *file = new QFile(strFilename, this);
    7. if (!file->open(QIODevice::ReadOnly)) {
    8. qDebug() << "cannot read" << strFilename;
    9. return;
    10. }
    11.  
    12. MyDialog *a = new MyDialog(this, file);
    13. a->show();
    14.  
    15. return;
    16. }
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. The following user says thank you to spirit for this useful post:

    xfurrier (19th August 2009)

  4. #3
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Segmentation fault while passing IO/file handles

    You're absolutely right. Thanks.

Similar Threads

  1. Replies: 2
    Last Post: 16th June 2010, 23:58
  2. segmentation fault for no apparent reason
    By rishiraj in forum Newbie
    Replies: 1
    Last Post: 12th February 2009, 11:13
  3. segmentation fault
    By uchennaanyanwu in forum Newbie
    Replies: 3
    Last Post: 31st July 2008, 16:52
  4. Segmentation fault running any QT4 executables
    By jellis in forum Installation and Deployment
    Replies: 7
    Last Post: 19th May 2007, 16:35
  5. Icons missing => segmentation fault
    By antonio.r.tome in forum Qt Programming
    Replies: 4
    Last Post: 8th March 2006, 16:30

Tags for this Thread

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.