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
Yourfor
loop created/inserted just 1 row. Yourtable->item(rowToChange, 0)
has 0 <=rowToChange
< 5. IfrowToChange
> 0 that's not good.Also, your
table->insertRow(table->rowCount())
inserts row at end, but yourtable->setItem(0, 0, new QTableWidgetItem(QString::number(nb)));
is only ever setting row #0 to have anew 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 -
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
-
@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";
-
@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... -
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.