Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Get text from table cell
Indrajeet last edited by
I have a QTableWidget & I am trying to get the content of the cell of this table.
@QString str = ui.tableWidget->item( row, col )->text();@
Through the above code I am able to get the content of required cell but if the cell is empty this line of code is crashing.
So how to handle for empty cell
you are querying first for an object (item) and access it without checking if the element exists. What you could do is using the model instead:
QAbstractItemModel* model = ui.tableWidget->model();
QModelIndex idx = model->index(row, col);
QString str = model->data(idx)->toString();
or, alternatively, you actually check the pointers:
QTableWidgetItem* itm = ui.tableWidget->item( row, col );
str = itm->text();
Indrajeet last edited by
But as we are doing 3 steps to get the data,wether it will affect the performance if we do for all items in table.
These steps should all be fast.
On the other side, you could use your version and always check the result of item to 0, like Andre suggested.
goetz last edited by
[quote author="Rajveer" date="1315815091"]Thanks Gerolf.
But as we are doing 3 steps to get the data,wether it will affect the performance if we do for all items in table.[/quote]
Think of performance issues after you are bitten by those, not before. Premature optimization leads to errors which are hard to find.
An BTW: the code André posted is basically what's done behind the scenes. Gerolf's solution is a bit more elegant regarding reading your own code though.
[quote author="Volker" date="1315897394"]An BTW: the code André posted is basically what's done behind the scenes. Gerolf's solution is a bit more elegant regarding reading your own code though.[/quote]
Actually, it is not. My solution uses the QTableWidget* API, not the QAIM API. Personally, I find the latter a bit of a hack to use in this case, as you are using the QTableWidget for a reason, I presume. Behind the scenes, Gerolfs solution obviously does not fall back to using the QTableWidget API. All it does is just make a direct call into the QTableWidget's model, which should by all rights be private anyway.
My method uses the QAIM API, that is right, and will also work with MVC concepts.
Yes I use the model of QTableWidget, but the model itself is not private. So why not use it?
I am not saying that you can not use it. You proved that you can. However, I think that the fact that QTableWidget has an accessible model() method is actually an oversight. It exposes an implementation detail of the class. It might have been better if the widget versions of the item views would have used encapsulation instead of inheritance.
Furthermore, in the general case, I am of the opinion that you should not use the QAIM API for other purposes than to provide an interface for views and delegates (and to create classes like QDataWidgetMapper). The API is not designed for that, and you should use application specific API to interact with the data store object that is underlying the model. The QAIM is here only an adaptor class between the data store and the Qt view classes to visualize data from that store.
In this case, the QTableWidget is the class that offers the API to manipulate the data, so it plays the role of both the data store and the view, and the actual model used is obscured from view. Following my reasoning, you should use the API of the data store to manipulate the data, that is, the QTableWidget and QTableWidgetItem API.