Get text from table cell
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
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.
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.
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.An
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.
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.