Solved Modify QtableWidget from a slots
-
Hi
Yes it possible. and it normally just worksui->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 :
-
@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.
-
Hi
You must assign items to the row/cols or else NULL is returned.
Try withauto 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 yourtable
has, before you try to dereference withtable->item(rowToChange, 0)
. You are using the sametable
instance as you create withQTableWidget* table = new QTableWidget(this);
aren't you, because that looks like a local variable.... ? If you have a
QMainWindow
member variable namedtable
, then statementQTableWidget* table = new QTableWidget(this);
is not setting the member one... -
@JonB
Ahhh good spotted. The dangling pointer classic :) -
Thanks for your answers.
First, I createtable
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 ? -
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 ! -