Results 1 to 2 of 2

Thread: color specified cell in a tableview

  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Question color specified cell in a tableview

    I am trying to print a report based on a dialog. On the dialog there is a table based on a QSqlRelationalModel and a QTable view. The user will be able to sort the table and delete rows in the displayed table. Then the user can print the report to reflect the changes. I am converting the information to htm to be able to print it. I am able to create the report with the exception three things. In the tableview, I need to highlight the cell containing the highest and lowest number in a particular column. I am not sure if this is the best way as I see there is a QModelIndexClass, but I am not sure how to implement it. I am able to get the column and row from the tableview of the cells that need to be highlighted through this code. I am not sure if this will actually work as I believe the model will contain ALL the records and not reflect which ones were deleted in the tableview.
    Qt Code:
    1. float fastTime=0;
    2. float slowTime=0;
    3. int rowFast=0;
    4. int rowSlow=0;
    5. int step;
    6.  
    7. while(printModel->canFetchMore())
    8. printModel->fetchMore();//to show all the rows
    9. int numRows = printModel->rowCount();
    10. QSqlRecord record;
    11.  
    12. //get fastest and slowest times and save row
    13.  
    14. for (step=0; step <= numRows; step++)
    15. {
    16. record = printModel->record(step);
    17. if (record.value(32).toFloat() < fastTime)
    18. {
    19. fastTime=record.value(32).toFloat();
    20. rowFast = step;
    21.  
    22. }
    23. if (record.value(32).toFloat() > slowTime)
    24. {
    25. slowTime=record.value(32).toFloat();
    26. rowSlow = step;
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 
    I have tried searching but I was unable to find how to highlight one cell based on a row,col criteria.
    So what I need is:
    1) a way to iterate through a tableview to get the data. (in QModelIndexClass this will list all items?)

    2) how to go to the specified cell and highlight the background color.

    3) How to identify the items displayed in the tableView to get the data to populate the html code (the table view can hide several columns and if I use QModelIndex I will have to iterate through each hidden column and check if it is hidden?)

    I have tried several different codes but none worked, so I am not sure which code to post as an example. I am sure it is probably because I did not implement them correctly. I have tried using QSortFilterProxyModel, creating a QItemDelegate with a paint function.

    So what would be the best methods to achieve this?

  2. #2
    Join Date
    Feb 2010
    Posts
    18
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: color specified cell in a tableview

    Quote Originally Posted by poporacer View Post
    1) a way to iterate through a tableview to get the data. (in QModelIndexClass this will list all items?)

    2) how to go to the specified cell and highlight the background color.

    3) How to identify the items displayed in the tableView to get the data to populate the html code (the table view can hide several columns and if I use QModelIndex I will have to iterate through each hidden column and check if it is hidden?)
    1. You can iterate with QAbstractItemModel.index(row, column) with row from 0 to QAbstractItemModel.rowCount() and column = column with the value to search for

    2. Look at http://doc.qt.nokia.com/4.7/modelview.html under 2.2

    3. When you only want to export the columns that are visible, then you have to check all columns if they are visible.

Similar Threads

  1. how to color a cell in a QTableView
    By JeanC in forum Qt Programming
    Replies: 13
    Last Post: 9th September 2015, 10:08
  2. Setting the background color of a header in TableView
    By sunilqt in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2013, 13:06
  3. Replies: 2
    Last Post: 16th February 2011, 09:15
  4. Replies: 1
    Last Post: 7th December 2009, 18:56
  5. Replies: 1
    Last Post: 23rd March 2009, 21:36

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.