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

Qtablewidget Throwing Segmentation Fault



  • I am using QtableWidget to populate data collected from ROS messages:

    ui->stat->setColumnCount(3);
    ui->stat->setRowCount(5);
    count_row=0;// keep track of table rows
    ui->stat->setColumnWidth(0,145);
    ui->stat->setColumnWidth(1,50);
    ui->stat->setColumnWidth(2,300);
    QStringlist labels; labels.push_back(" Time");
    labels.push_back("Type"); labels.push_back("Message ");
    ui->stat->setHorizontalHeaderLabels(labels);
    QTableWidgetItem* text = new QTableWidgetItem();
    QTableWidgetItem* msgType = new QTableWidgetItem();
    QTableWidgetItem* time = new QTableWidgetItem();
    count_row=count_row%5;
    ui->stat->setItem(count_row,0,time);
    ui->stat->setItem(count_row,1,msgType); ui->st
    at->setItem(count_row,2,text);
    count_row++;
    for(int i=4;i>=0;i--)
    ui->stat->showRow(i);

    On running the program, I am getting segmentation fault error. Please help!!


  • Moderators

    Hi,

    1. Do you want to display any text in QTableWidget ? As i can see you have not added any text to your QTableWidgetItem.

    2. You are adding 3 items in 1st row's 3 columns and you are iterating the whole QTableWidget. And here no need to do showRow as the items will be displayed by default when you set text to it.

    3. I modified your code as follows and i see no crashes:
      @ui->stat->setColumnCount(3);
      ui->stat->setRowCount(5);
      int count_row=0;// keep track of table rows

      ui->stat->setColumnWidth(0,145);
      ui->stat->setColumnWidth(1,50);
      ui->stat->setColumnWidth(2,300);

      QStringList labels;
      labels.push_back("Time");
      labels.push_back("Type");
      labels.push_back("Message");

      ui->stat->setHorizontalHeaderLabels(labels);

      QTableWidgetItem* text = new QTableWidgetItem();
      text->setText("Sometext");
      QTableWidgetItem* msgType = new QTableWidgetItem();
      msgType->setText("SomeMessage");
      QTableWidgetItem* time = new QTableWidgetItem();
      time->setText("SomeTime");

      count_row=count_row%5;
      qDebug() << count_row;
      ui->stat->setItem(count_row,0,time);
      ui->stat->setItem(count_row,1,msgType);
      ui->stat->setItem(count_row,2,text);
      count_row++;

      for(int i=4;i>=0;i--)
      ui->stat->showRow(i);@

    4. Please include your code in Code tags as it is difficult to understand to others.


Log in to reply