Program crashes while accessing QTableWidgetItem



  • Hello everyone, this is my first post so I hope I am doing it correct. Please feel free to advice me for future post.

    I have a problem concerning the access of a QTableWidgetItem. I created a GUI using QT Creator on a Windows machine and added a QTableWidget to my GUI. The TableWidget consisted of two columns and 16 rows (0 - 15). Everything worked great and I could access the items without an error. Yesterday, I switched to my personal MacBook (not sure if this is of any importance) and I added another row with the designer in QT Creator, so that I had 17 in total. I noticed that my program crashes right after the start and found that accessing the 17th row caused the error. I used the following code, which gave me the output for all rows except the last one.

    @ QTableWidgetItem* item = ui->tableWidget_2->item(16,0);
    if(item)
    {
    std::cout << "Item is accessible" << std::endl;
    }@

    How can I solve this problem?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Did you run your program through a debugger ?



  • I must admit that I have never used the debugger in Qt Creator before.



  • I did the following assignment manually that seems to work now, but is there a way it might work without doing it manually? I'm not sure how many rows I would like to add to the table and rather not add every item by hand.

    @ QTableWidgetItem *newItem = new QTableWidgetItem();
    ui->tableWidget_2->setItem(16,0,newItem);
    if(ui->tableWidget_2->item(16,0))
    {
    std::cout << "Item is available" << std::endl;
    }
    @


  • Lifetime Qt Champion

    When you added the row, did you put something in it ?



  • I left the new rows blank initially. I put content in the other rows when I first created them, but deleted the content later in the Qt Designer for they are filled with a routine. Do I have to put content in the cells in order to create the QTableWidgetItems corresponding to them?


  • Lifetime Qt Champion

    No, you don't need to do that. If you fill the items in a routine, why not also set the number of rows in there ? So you won't have that kind of problem ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.