Results 1 to 16 of 16

Thread: All specific column data of every row. QTableView

  1. #1
    Join Date
    Aug 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

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

    Qt Code:
    1. number = ui->tableView->model()->data(ui->tableView->model()->index(0,5)).toString();
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  3. #3
    Join Date
    Aug 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

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

    Qt Code:
    1. int rows = model->rowCount();
    2.  
    3. for (int i = 0; i < rows; i++)
    4. {
    5. number = ui->tableView->model()->data(ui->tableView->model()->index(i,5)).toString();
    6. number2 = ui->tableView->model()->data(ui->tableView->model()->index(i,6)).toString();
    7. }
    To copy to clipboard, switch view to plain text mode 

    Thanks
    Last edited by Jarrod; 14th August 2016 at 12:06.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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,
    _

  5. #5
    Join Date
    Aug 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default 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

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: All specific column data of every row. QTableView

    So, what value does "rows" have?

    Cheers,
    _

  7. #7
    Join Date
    Aug 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

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

  8. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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:
    Qt Code:
    1. int rows = model->rowCount();
    To copy to clipboard, switch view to plain text mode 
    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?

    Qt Code:
    1. int rows1 = model->rowCount();
    2. int rows2 = ui->tableView->model()->rowCount();
    To copy to clipboard, switch view to plain text mode 
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  9. #9
    Join Date
    Aug 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default 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

  10. #10
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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?
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  11. #11
    Join Date
    Aug 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default 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
    Last edited by Jarrod; 15th August 2016 at 19:20.

  12. #12
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: All specific column data of every row. QTableView

    Quote Originally Posted by Jarrod View Post
    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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  13. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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

    Qt Code:
    1. for (int i = 0; i < someValueGreaterThan1; ++i); // semicolon here makes the for have an empty body
    2. {
    3. // not actually the loop's body
    4. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  14. #14
    Join Date
    Aug 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    Windows

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

    model.jpg

    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.

  15. #15
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  16. #16
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: All specific column data of every row. QTableView

    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.

Similar Threads

  1. Replies: 4
    Last Post: 27th February 2014, 10:42
  2. Get data from QTableView sorted by column
    By qks1 in forum Qt Programming
    Replies: 0
    Last Post: 26th June 2013, 10:27
  3. How to get all column data of a QTableView
    By npascual in forum Qt Programming
    Replies: 1
    Last Post: 26th September 2012, 10:05
  4. Replies: 2
    Last Post: 5th September 2010, 15:06
  5. Replies: 1
    Last Post: 23rd January 2010, 23:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.