PDA

View Full Version : All specific column data of every row. QTableView



Jarrod
14th August 2016, 10:25
Hi,

How can I get all the data from a specific column?
I have managed to pull the data from a specific cell, but I want all the data in the cells from a specific column.


number = ui->tableView->model()->data(ui->tableView->model()->index(0,5)).toString();

This obviously just gives me the data in that exact index. I want to retrieve all the data from column 5, with x amount of rows.

Thanks.

anda_skoa
14th August 2016, 10:57
You loop over the number of rows and use the loop variable as the row argument ot the index() function.

http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

Cheers,
_

Jarrod
14th August 2016, 11:06
Thanks for your reply, was just typing that I had figured it out.

Added after 5 minutes:

Well...I thought I had figured it out.
My code isn't give me all the rows, it only gives me the one row.


int rows = model->rowCount();

for (int i = 0; i < rows; i++)
{
number = ui->tableView->model()->data(ui->tableView->model()->index(i,5)).toString();
number2 = ui->tableView->model()->data(ui->tableView->model()->index(i,6)).toString();
}

Thanks

anda_skoa
14th August 2016, 12:26
Are you calling rowCount() on the same model as you are using for index() and data()?

Is the "rows" value what you expect?

Cheers,
_

Jarrod
14th August 2016, 17:28
Yes I am using it on the same model.
It calls correctly, but it only calls 1 row, not all of the rows. Seems the forloop ends after the first row has been iterated through(I have multiple rows and potentially thousands of rows).

Thanks

anda_skoa
14th August 2016, 21:27
So, what value does "rows" have?

Cheers,
_

Jarrod
14th August 2016, 23:10
I'm not sure if I'm understanding your question correctly.
Well each row has names and numbers. The index(i,5) and index(i,6) are 2 numbers which I am pulling out and adding together(As well as performing some other operations on them), then displaying the result in a different QTableModel. It seems to work just fine but for only the first row, maybe I have setup my forloop incorrectly?

I used a similar forloop to add data to the tableView from a txt file and that works with no problem. I can add a txt file full of data but I just can't perform operations on the data(Except for the first row of course).

Thanks.

jefftee
15th August 2016, 01:42
What @anda_skoa is pointing out is that you get the number of rows like this:


int rows = model->rowCount();

Yet the rest of your code gets the model from the view, i.e. ui->tableView->model(), so the question is do the following return the same values and if so, that is that value?



int rows1 = model->rowCount();
int rows2 = ui->tableView->model()->rowCount();

Jarrod
15th August 2016, 17:32
Thanks just tested.
I loaded a file with 2 rows and both rows1 and rows2 give an answer of 2. It loads all the rows its just only performs operations on the first one and not all of them.

Thanks

jefftee
15th August 2016, 17:41
So if rows = 2 then your loop *must* execute twice for i = 0 and i = 1 cases. Are you saying it executes only once for the i = 0 case?

Jarrod
15th August 2016, 18:21
I will rewrite the function, my logic is probably incorrect. I will get back with an update as soon as I have finished.

Thanks

jefftee
15th August 2016, 18:42
I will rewrite the function, my logic is probably incorrect. I will get back with an update as soon as I have finished.
Please do, it's not clear at all to me what problem you're having. The prior complaint was that the loop was only executing once, which now seems to not be the problem, so I am unclear regarding what "performs operations on the first one and not all of them" is in reference to.

anda_skoa
15th August 2016, 18:45
If the posted code is not your actual code make sure your actual code does not have a semicolon at the end of the for condition.

Code like this will only ever execute the "loop body" once



for (int i = 0; i < someValueGreaterThan1; ++i); // semicolon here makes the for have an empty body
{
// not actually the loop's body
}


Cheers,
_

Jarrod
15th August 2016, 19:34
Thanks.
There is no semicolon after the loop. The code I posted is the actual code. I have attached a screen shot to get you a better idea of what I was trying to accomplish.

12068

Operations are performed on "Assignment 1 Mark" and "Assignment 2 mark" as you can see from the screenshot, the marks are multiplied by certain amounts based on the mark, for example if(mark >50 && mark <= 60) then mark * 150. Based on the total number a "Current level" is assigned, based on what you can see if the total is greater than 15000 and less than 20000 then "Current Level" is set to "Rambler". But as I said it's only performing this operation on the first row and not the second.

Hope this gives a bit more insight into what I'm trying to achieve.

Thanks.

jefftee
15th August 2016, 21:59
Please show the code in your for loop. What you're describing isn't at all what you posted above for the for loop. Thanks.

ChrisW67
15th August 2016, 22:08
This

The code I posted is the actual code.
And this

Operations are performed on ...
Do not agree with each other. Your code does not perform any action changing the values in the model; it merely converts two column values to strings that it then does nothing with inside the loop.

Perhaps the logic that does the one row is outside the loop you have posted and is using the values extracted from the last pass of the loop.