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

[solved]segfault: QTableWidgetItem->setText



  • I've read that segfaults are usually caused by trying to use inaccessible memory. So, if I'm getting a segfault from trying to manipulate a table widget item, it's probably because the item I'm trying to access doesn't exist.

    I'm using a qtablewidget with one row to simulate a Turing machine's input tape.
    @
    void MainWindow::goForth()
    {

    //code that isn't part of the problem
    //I fetch the widget's current cell to get the symbol that's in it

            move=shifts.indexOf(triplet.at(2)) - 1;   //this decideswether I'll move on the "tape" left, right, or not at all.
            index=ui->tableWidget_2->currentIndex();
            tapeCell=index.column();   
            tapeCell+=move;      //this helps me get the index of the next cell I should move to
    
            if(tapeCell >  ui->tableWidget_2->columnCount())  //this is the problematic one
            {
                ui->tableWidget_2->insertColumn(ui->tableWidget_2->columnCount()-1);
                QTableWidgetItem *item = new QTableWidgetItem();
                item->setTextAlignment(Qt::AlignCenter);
                QFont *font = new QFont();
                font->setPointSize(30);         //if I should move to a cell that's beyond the right edge of the tape, 
                item->setFont(*font);             //I oughtta create more tape, and that's what I'm trying to do here. 
                item->setText(triplet.at(1));    //insert a column, put a new item there and set it.
                ui->tableWidget_2->setItem(0, tapeCell, item);
                ui->tableWidget_2->setCurrentCell(0, tapeCell);
            }else
                if(tapeCell<0)
                {
                    ui->tableWidget_2->insertColumn(0);
                    QTableWidgetItem *item = new QTableWidgetItem();
                    item->setTextAlignment(Qt::AlignCenter);
                    QFont *font = new QFont();
                    font->setPointSize(30);                              //now, with this, I don't have a problem. It's exactly the
                    item->setFont(*font);                                //same as the problem code, except here I'm setting the 
                    item->setText(triplet.at(1));                    // cell using 0, not an int variable.
                    ui->tableWidget_2->setItem(0, 0, item);
                    ui->tableWidget_2->item(0, 0)->setText(triplet.at(1));
                    ui->tableWidget_2->setCurrentCell(0,0);
                }else
                {
            ui->tableWidget_2->item(0, tapeCell)->setText(triplet.at(1));  //when it throws the segfault, this line is 
            ui->tableWidget_2->setCurrentCell(0, tapeCell);    //where it points me to, even though this line shouldn't 
    

    } //be executed because tapeCell is greater than columncount

            stepCount++;
            quintuplet=QString("%1. takt: %2, %3 --&gt; %4").arg(stepCount).arg(cState).arg(cSymbol).arg(triplet.join(", "));
            ui->listWidget->addItem(quintuplet);
        }
    }
    

    }
    @

    I checked the brackets, they're all fine, the if clause is how it should be, I'm setting items left and right... what's wrong?

    EDIT: nevermind, figured it out after a nap XD I messed up the setting of the current index that occurs whenever I'm near the edge of the tape.


Log in to reply