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

Modify QtableWidget from a slots



  • Hello !
    I have a problem that I can't solve :
    I have a QTableWidget, and I wan't to modify randomly the text in the rows.
    So I have made this :

    mainwinow.cpp

        QTableWidget* table = new QTableWidget(this);
        table->setColumnCount(1);
        table->setEditTriggers(QAbstractItemView::NoEditTriggers); 
    
        for (int i=0; i<1; i++) 
        {
            nb = rand() % 500 + 10;
            table->insertRow(table->rowCount());
            table->setItem(0, 0, new QTableWidgetItem(QString::number(nb)));
        }
    

    and I have a slot in mainwondow.cpp that I call when I press a Button

    void MainWindow::changeNbRandom()
    {
        int rowToChange = 0;
        rowToChange = rand() % 5;
        int newNb = 5;
        newNb = rand() % 500 + 10;
    
        table->item(rowToChange, 0)->setText(QString::number(newNb));
    

    But when I press the Button, the application crash.
    The debug say that the error is on the last line of the slots.
    But if I put the line in dirrectly in mainwindow it works...

    Please, help me, I don't understand



  • @Yoyotl
    Your for loop created/inserted just 1 row. Your table->item(rowToChange, 0) has 0 <= rowToChange < 5. If rowToChange > 0 that's not good.

    Also, your table->insertRow(table->rowCount()) inserts row at end, but your table->setItem(0, 0, new QTableWidgetItem(QString::number(nb))); is only ever setting row #0 to have a new QTableWidgetItem, you keep overwriting just that one item.



  • Alright, I change that, it was a mistake :

    for (int i=0; i<5; i++) /
        {
            nb = rand() % 500 + 10;
            table->insertRow(table->rowCount());
            table->setItem(i, 0, new QTableWidgetItem(QString::number(nb)));
        }
    

    So I have my 5 rows.
    I've tested differents sings with the debuggers, and it's seems like I just can't make any action on my QTableWidget table in my slot



  • It is possible ? Or how can I do otherwise ?


  • Lifetime Qt Champion

    Hi
    Yes it possible. and it normally just works

    ui->tableWidget_2->item(0,0)->setText("Test");

    changes the first row/col of the table widget



  • Yeah, I was thinking that too, but it doesn't work...
    Debugger sort that :
    ab27d849-3198-48fb-9695-e19075a22293-image.png


  • Lifetime Qt Champion

    @mrjj said in Modify QtableWidget from a slots:

    ui->tableWidget_2->item(0,0)

    I would guess this returns a nullptr since there is no QTableWidgetItem set yet - you have to set one before accessing it.


  • Lifetime Qt Champion

    Hi
    You must assign items to the row/cols or else NULL is returned.
    Try with

    auto item = ui->tableWidget_2->item(row, 0);
    if (item)
        item->setText("Test");
    else
        qDebug() << "item is null";


  • Itwork's if I put that in mainwindow, but if I put it in my slot like I want to do, the program crash again...



  • @Yoyotl
    In your slot, put in debugging code to see how many rows/columns/what items your table has, before you try to dereference with table->item(rowToChange, 0). You are using the same table instance as you create with

    QTableWidget* table = new QTableWidget(this);
    

    aren't you, because that looks like a local variable.... ? If you have a QMainWindow member variable named table, then statement QTableWidget* table = new QTableWidget(this); is not setting the member one...


  • Lifetime Qt Champion

    @JonB
    Ahhh good spotted. The dangling pointer classic :)



  • Thanks for your answers.
    First, I create table in my mainwindow.cpp, and I declare it in mainwindow.h in public.
    My slot is in the file mainwindow.cpp
    And I can't even test how many rows/columns/item I have in it in my slot, because program crash on a segmentation fault every time that I try to access to table on my slot
    Do I need to declare table in antoher place/file ?


  • Lifetime Qt Champion

    @Yoyotl

    QTableWidget* table = new QTableWidget(this); // this would be wrong as its a local variable

    table = new QTableWidget(this); // this would allocate the one you have in the class. (in the .h )

    so make sure to remove the first one so there is ONLY the one in the class that you access in the slot

    Im 99% sure that is why you crash.



  • IT WAS THAT !!
    THANK YOU SO MUCH @mrjj !

    So yes it was a problem in my declaration of table
    That fix all my problems, thank you all !


  • Lifetime Qt Champion

    @Yoyotl
    Actually it was @JonB that spotted it but
    super its fixed :)


Log in to reply