Results 1 to 16 of 16

Thread: All specific column data of every row. QTableView

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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 11: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!

Similar Threads

  1. Replies: 4
    Last Post: 27th February 2014, 09:42
  2. Get data from QTableView sorted by column
    By qks1 in forum Qt Programming
    Replies: 0
    Last Post: 26th June 2013, 09:27
  3. How to get all column data of a QTableView
    By npascual in forum Qt Programming
    Replies: 1
    Last Post: 26th September 2012, 09:05
  4. Replies: 2
    Last Post: 5th September 2010, 14:06
  5. Replies: 1
    Last Post: 23rd January 2010, 22: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.