Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QTableWidget item->settext() Segfaulting QT 5.8
Forum Updated to NodeBB v4.3 + New Features

QTableWidget item->settext() Segfaulting QT 5.8

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dcbdbisVA
    wrote on last edited by
    #1

    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);
       }
    
    jsulmJ 2 Replies Last reply
    0
    • D dcbdbisVA

      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);
         }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • D dcbdbisVA

        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);
           }
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          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!

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          2
          • D Offline
            D Offline
            dcbdbisVA
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              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

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved