Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableWidget item->settext() Segfaulting QT 5.8



  • Good evening. I am having a segfault and I cannot find out why. According to the web searches that I have done things are OK in the code.

    I am reading in a file, adding a row to the tableWidget, then attempting to parse and put data in the same. It segfaults on the first "setText" line.

    "Cell[]" is a QString array. "row" is an int. "line" is a QString.

    
    fp = fopen("Procedures.txt","r");
       if(fp != NULL)
       {
       QTextStream file(fp,QIODevice::ReadOnly);
    
          while(file.readLineInto(&line))
          {
          Cell[0] = line.mid(0,line.indexOf("\t"));
          line.remove(0,(line.indexOf("\t")+1));
          Cell[1] = line;
          ui->tableWidget->insertRow(row);
          ui->tableWidget->item(row,0)->setText(Cell[0]);
          ui->tableWidget->item(row,1)->setText(Cell[1]);
          row++;
          }
    
       fclose(fp);
       }
    

  • Qt Champions 2019

    @dcbdbisVA You did not set any items in the new row, so I guess ui->tableWidget->item(row,0) and ui->tableWidget->item(row,1) both return null pointer. See http://doc.qt.io/qt-5/qtablewidget.html#insertRow : "Inserts an empty row into the table at row."
    You need to set items in both columns using http://doc.qt.io/qt-5/qtablewidget.html#setItem


  • Qt Champions 2019

    @dcbdbisVA One note: if you use Qt you can use QFile to access files.



  • ui->tableWidget->item(row,0) and ui->tableWidget->item(row,1) are null pointers.

    QAbstractItemModel* model=ui->tableWidget->model();
    QFile fp("Procedures.txt");
    if(fp.open(QIODevice::ReadOnly | QIODevice::Text)){
    QTextStream file(&fp);
    for(;file.readLineInto(&line);++row){
    const QStringList cells=line.split("\t");
    model->insertRow(row);
    model->setData(model->index(row,0),cells.value(0));
    model->setData(model->index(row,1),cells.value(1));
    }
    fp.close();
    }
    

    P.S.
    Yes, I hate the QStandardItem interface!



  • VRonin and jsulm, THANK YOU.

    I appreciated the code example VRonin....As you can tell by my style, I'm older. Think ENIAC, 300 baud acoustically coupled modems, and everything written in assembler.

    I modified my code accordingly and it works perfectly.

    To assist myself in the future when I read the docs, I would like to ask this question:

    Where in the QT docs would I have gotten the model interface? When I was reading the class documentation, nowhere did it tell me about the model interface. I ASSumed that the class documentation had all I needed there. Contrasting QT to wxWidgets, and the VLC (from the Borland days) all fully documented their classes on how to interact with them. Frankly, I would not have gotten your solution from the QT docs themselves. So I am very appreciative for your example.

    Is there some doc that I need to read elsewhere to brush-up on the "model" interface, and when to use/deploy it?

    Thank You!

    Dave



  • QTableWidget is an unsusual animal, it's just a QTableView with a QStandardItemModel as a member.

    In one of the see also in the documentation it links to http://doc.qt.io/qt-5/model-view-programming.html that explains that models must respect the QAbstractItemModel interface so you can use that one to manipulate models


Log in to reply