September 12, 2011

Adi Adi
Lab Rat
206 posts

Get text from table cell

 

Hi All

I have a QTableWidget & I am trying to get the content of the cell of this table.

  1. 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

8 replies

September 12, 2011

Gerolf Gerolf
Robot Herder
3235 posts

Hi Ravjerr,

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:

  1.     QAbstractItemModel* model = ui.tableWidget->model();
  2.     QModelIndex idx = model->index(row, col);
  3.     QString str = model->data(idx)->toString();

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

September 12, 2011

Andre Andre
Robot Herder
6295 posts

or, alternatively, you actually check the pointers:

  1. QString str;
  2. QTableWidgetItem* itm = ui.tableWidget->item( row, col );
  3. if (itm)
  4.     str = itm->text();

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

September 12, 2011

Adi Adi
Lab Rat
206 posts

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.

September 12, 2011

Gerolf Gerolf
Robot Herder
3235 posts

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.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

September 13, 2011

Volker Volker
Ant Farmer
5428 posts

Rajveer wrote:
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.

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.

September 13, 2011

Andre Andre
Robot Herder
6295 posts

Volker wrote:
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.

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

September 13, 2011

Gerolf Gerolf
Robot Herder
3235 posts

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?

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

September 13, 2011

Andre Andre
Robot Herder
6295 posts

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.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ rubberband zooming      unregistered timer problem ››

You must log in to post a reply. Not a member yet? Register here!