PDA

View Full Version : Obtaining data from QTableWidget



therealjag
14th February 2006, 18:42
Hey there i have created a table using QTableWidget and i was wondering if there is a way to obtain the data from the table when the user enters data into it so that i can perform a calculation on it and display it back to the user. Do i have to create a new function/method to perfrom the calculation in?? Ive tried looking at the class for QTableWidget and i cant really find enough that will help me with this task. any help would be again very appreciated thanks. Jag

wysota
14th February 2006, 21:22
Use QTableWidget::item(x,y) method to access a particular item and QTableWidgetItem::data(int role) to get the data stored in it (You need to access the DisplayRole) as QVariant. You can then use QVariant::toInt() (or other) to convert it to a type you need to perform your calculations. Of course for calculations themselves you need some place in your code (some method -- a slot maybe?).

therealjag
14th March 2006, 20:34
hey there i have one problem with my table now. when i want to get user data from it i have to enter the cell, enter the number, then leave the cell and then enter it again before the original value gets stored by my array?!! why does my table do this? heres my code to get the data from the table:



table[demands->currentRow()][demands->currentColumn()] = demands->item(demands->currentRow(), demands->currentColumn())->data(Qt::DisplayRole).toInt();


sorry about the lateness of the reply to the post but i forgot to ask before...

jpn
15th March 2006, 07:42
The problem sounds like that you havent' figured out the correct event when you should perform the calculation.

Here's at least 2 working ways:

1) Inherit QTableWidget and override protected slot:
virtual void dataChanged (http://doc.trolltech.com/4.1/qabstractitemview.html#dataChanged)( const QModelIndex & topLeft, const QModelIndex & bottomRight )
By this way you don't need to create any extra connections manually, since thanks to the underlying model/view architecture and a virtual slot you'll get the connection automatically. But you will have to inherit QTableWidget.

2) Create a custom slot of type:

void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
..and connect the table's model's dataChanged() signal signal to your custom slot:

connect(table->model(), SIGNAL(dataChanged(const QModelIndex&, const QModelIndex&)), this, SLOT(dataChanged(const QModelIndex&, const QModelIndex&)))
By this way you don't need to inherit QTableWidget. But you will have to create the connection manually.

Then, in both ways, you'll put your calculation code into the dataChanged() slot.
Basically the indexes topLeft and bottomRight are always the same, if you don't change the data programmatically. Meaning that the user is normally able to edit one cell at time..
I don't know if this is different depending on the selection mode and behaviour.

Anyway, the code in your slot could look something like this:

table[topleft.row()][topLeft.column()] = topLeft.data().toInt();