All specific column data of every row. QTableView
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.
Code:
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.
Re: All specific column data of every row. QTableView
You loop over the number of rows and use the loop variable as the row argument ot the index() function.
http://www.tutorialspoint.com/cplusp...p_for_loop.htm
Cheers,
_
Re: All specific column data of every row. QTableView
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.
Code:
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
Re: All specific column data of every row. QTableView
Are you calling rowCount() on the same model as you are using for index() and data()?
Is the "rows" value what you expect?
Cheers,
_
Re: All specific column data of every row. QTableView
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
Re: All specific column data of every row. QTableView
So, what value does "rows" have?
Cheers,
_
Re: All specific column data of every row. QTableView
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.
Re: All specific column data of every row. QTableView
What @anda_skoa is pointing out is that you get the number of rows like this:
Code:
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?
Code:
int rows1 = model->rowCount();
int rows2 = ui->tableView->model()->rowCount();
Re: All specific column data of every row. QTableView
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
Re: All specific column data of every row. QTableView
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?
Re: All specific column data of every row. QTableView
I will rewrite the function, my logic is probably incorrect. I will get back with an update as soon as I have finished.
Thanks
Re: All specific column data of every row. QTableView
Quote:
Originally Posted by
Jarrod
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.
Re: All specific column data of every row. QTableView
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
Code:
for (int i = 0; i < someValueGreaterThan1; ++i); // semicolon here makes the for have an empty body
{
// not actually the loop's body
}
Cheers,
_
1 Attachment(s)
Re: All specific column data of every row. QTableView
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.
Attachment 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.
Re: All specific column data of every row. QTableView
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.
Re: All specific column data of every row. QTableView
This
Quote:
The code I posted is the actual code.
And this
Quote:
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.