integer as a QTableWidgetItem ?
I'd like to load QTableWidget with integers and then display selected integers. It seems that QTableWidget can accept integers as shown here:
Code:
myTable->setItem(0, 0, newItem); //set object
But how do you visualize a selected entry from this table (entry 0,0)? The below code evidently does not work because the returned type is wrong.
Code:
int a = myTable->item(0,0);
I'd have to do this
But how can I get my integer out of this? This won't return integer.
Re: integer as a QTableWidgetItem ?
Code:
int val = -1;
if (a)
val = a->text().toInt();
You may also want have a look at QTableWidgetItem::data() and setData woth UserRole...
Re: integer as a QTableWidgetItem ?
This worked great thanks!
How do you empty the QTableWidget memory after you are done using the table and want to get rid of it?
Re: integer as a QTableWidgetItem ?
QTableWidget::clear() or QTableWidget::clearContents(). Also frees the memory:
Code:
void QTableModel::clearContents()
{
for (int i = 0; i < tableItems.count(); ++i) {
if (tableItems.at(i)) {
tableItems.at(i)->view = 0;
delete tableItems.at(i); // <--
tableItems[i] = 0;
}
}
reset();
}
Re: integer as a QTableWidgetItem ?
Thanks again!
But can I just delete the QTableWidgetItem object? This should free the memory, too. How woud you do that?
Re: integer as a QTableWidgetItem ?
Quote:
Originally Posted by
tommy
But can I just delete the QTableWidgetItem object? This should free the memory, too. How woud you do that?
If you only want to delete a single item which is allready assigned to the table use
but be aware that you must delete the returned item yourself!
Example:
Re: integer as a QTableWidgetItem ?
Hi Lykurg,
Unfortunately it didn't work. The below code for some reason always assigns 0 to val. Why doesn't it assign 3 to val?
Thanks!
Code:
myTable->setItem(3, 3, newItem);
int val = a->text().toInt();
Re: integer as a QTableWidgetItem ?
Quote:
Originally Posted by
tommy
Quote:
Originally Posted by
tommy
Why doesn't it assign 3 to val?
because the above statement is wrong. You have to use: