0

I have this piece of code:

    temp = ui->barsTable->item(i, 1)->text().toUInt();
    if (temp > ui->nodesTable->rowCount())
    {
        QTableWidgetItem* bla = ui->barsTable->item(i, 1);
        /*ui->barsTable->item(i,  1)*/bla->setText(QString::number(ui->nodesTable->rowCount()));
    }

And it crashes on setText(QT debugger points to the setText function in QTableWidgetItem class). But bla is not nullptr and temp get text from this element correctly. Why does the error happen?

Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49
dimas
  • 13
  • 7
  • Did you try to rebuild your project? – Simon Oct 22 '17 at 09:14
  • Deleted folder with debug-build now and tried. Still error there. – dimas Oct 22 '17 at 09:23
  • What value does the variable have and how many rows does BarsTable have? the function item returns a pointer of the item if there is a QTableWidgetItem associated with a row and column and zero otherwise, thinking that bla is 0 so I recommend you encapsulate the last line with an if: `if(bla){bla->setText(QString::number(ui->nodesTable->rowCount()));}` – eyllanesc Oct 22 '17 at 09:30
  • bla has not null adress, temp has correct value(which it must to have in this situation), barsTable has 2 rows. I can't encapsulate it because if my programm works right - there aren't null cells and this check - just ignoring the problem. – dimas Oct 22 '17 at 09:43
  • maybe ui->nodesTable is not initialized? – JLev Oct 22 '17 at 09:50
  • I think no, because ui was made in designer and initialize in MainWindow before all functions. – dimas Oct 22 '17 at 10:04
  • Check it with a debugger, and trying printing the value before setting the text – JLev Oct 22 '17 at 10:18
  • It is visible and working in program GUI. I think, table can't be uninitialized and visible in window. – dimas Oct 22 '17 at 10:28
  • And do you get the right value when printing with qDeubg? – JLev Oct 22 '17 at 11:59
  • Yes, all values are right. Even the value inside setText function is right. – dimas Oct 22 '17 at 12:16
  • Segmentation faults are tricky and can rise on a misleading part of the code. Is the object where this code is executed OK? – Tob Oct 22 '17 at 21:55

0 Answers0