Important: Please read the Qt Code of Conduct -

QTableWidget problem

  • Hi, I have a problem and I don't know why.

    So, I have a QTable widget, (10 rows, 8 columns) in my case. With a loop I create in three column a QTableWidgetItem.
    I use table->setItem(currentRow,2,new QTableWidgetItem); and the same for column 3 and 4.
    I am sure that these lines are called and currentRow index take all values from 0 to 9.

    The problem is, when I use cell, my software crash.
    I use table->item(i,2)->setBackgroundColor(Qt::darkGray); 0<=i<=9 it's sure, I print "i" index;
    The setBackgroundColor works but I use the line directly after setText(text) or text() and now the software crash.
    The QTableWiodgetItem exists, and it's works for one method and not for setText and text.

    Someone have an idea ?

    Sorry for my english. If you need more information, tell me.

    Thanks a lot.

  • Hi Paco,
    you should give some code sample. That would help to understand your problem.

  • when I initialize my table:
    for(int currentRow=0; currentRow<nbRow; ++currentRow) {
    if(nbSystem>1) {

    table->setItem(currentRow,2,new QTableWidgetItem);
    table->setItem(currentRow,3,new QTableWidgetItem);
    table->setItem(currentRow,4,new QTableWidgetItem);

    And when I use it:
    for(int currentRow=0; currentRow<nbRow; ++currentRow) {
    QString posX =;
    QString posY =;
    QString posZ =;



    When the setText() is called, my software crash

    Sorry I can't copying my real code because it is on an another computer and I can't put USB key.

    I think perhaps the problem is the setSpan but it's not sure

    [edit: code highlighted / Denis Kormalev]

  • Sory now the setBackgoundColor doesn't work, I don't know why, but the object is created and the address in the cell is not NULL.

  • Please don't forget to highlight code with @ tags. I've done it in your last comment for you, but in future please do it yourself

  • Ho it's ok, I find it, sorry for all the desagreement. For the next time I will know that I need to use @ tag.

    My problem is I print the address before I modify it in an other function. And the new address it was NULL.

    Thank's for all

  • You set row spans in your first method. Be sure to not access any of the cells that are spanned over in the second method.

Log in to reply