Results 1 to 6 of 6

Thread: Tableview after Model move section

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

    Question Tableview after Model move section

    I have a tableview that is populated via a QSFPM and QSqlRelationalTableModel and depending on different criteria I move columns using the code:
    Qt Code:
    1. QHeaderView *header= ui->printView->horizontalHeader();
    2. header->moveSection( 12, 0 );
    To copy to clipboard, switch view to plain text mode 
    The Tableview displays properly and then I want to print the table. I do so using a HTML and iterate through the tableview using:
    Qt Code:
    1. for (int i=0; i<viewColumns; i++)
    2. {
    3. if (!ui->printView->isColumnHidden(i))
    4. mainTable += "<td><font size=\"3\">" + ui->printView->model()->headerData(i,Qt::Horizontal).toString() + "</font></td>";
    5. }
    6. mainTable += "</tr>";
    7.  
    8. for (int r=0; r<viewRows; r++ )
    9. {
    10. if (ui->printView->isRowHidden(r))
    11. continue;
    12.  
    13. mainTable += "<tr>";
    14.  
    15. for (int c =0; c<viewColumns; c++)
    16. {
    17. if (!ui->printView->isColumnHidden(c))
    18. {
    19. if (ui->printView->model()->index(r,c).data(Qt::BackgroundRole)==Qt::yellow)
    20. mainTable +="<td bgcolor=\"yellow\"><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
    21. else if (ui->printView->model()->index(r,c).data(Qt::BackgroundRole)==Qt::red)
    22. mainTable +="<td bgcolor=\"red\"><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
    23. else
    24. mainTable +="<td><font size=\"3\">" + ui->printView->model()->index(r,c).data(Qt::DisplayRole).toString()+ "</font></td>" ;
    25. }
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 
    The HTML has the columns in the original order but it displays in the modified order. Why is this? I thought the tableView just displays the info from the model and it doesn't know about the underlying data structure. I might be able to correct this with some fancy coding by setting variables to keep track of the column changes, but think there must be an easier way. Any ideas?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Tableview after Model move section

    That is the expected behavior. QHeaderView::moveSection() swaps columns in the view not the model.

  3. #3
    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: Tableview after Model move section

    You should look at QHeaderView::logicalIndex() and QHeaderView::visualIndex() and the mapping they provide.

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

    Default Re: Tableview after Model move section

    I think I almost understand but not quite. I tried:
    Qt Code:
    1. QHeaderView *header= ui->printView->horizontalHeader();
    2. int column;
    3. QString mainTable = "<table border=1 width = \"100%\"> <tr>";
    4. int viewColumns =ui->printView->model()->columnCount();
    5. int viewRows =ui->printView->model()->rowCount();
    6.  
    7. for (int i=0; i<viewColumns; i++)
    8. {
    9. if (!ui->printView->isColumnHidden(i))
    10. column=header->logicalIndex(i);
    11. mainTable += "<td><font size=\"3\">" + ui->printView->model()->headerData(column,Qt::Horizontal).toString() + "</font></td>";
    12. }
    13. mainTable += "</tr>";
    14.  
    15. for (int r=0; r<viewRows; r++ )
    16. {
    17. if (ui->printView->isRowHidden(r))
    18. continue;
    19.  
    20. mainTable += "<tr>";
    21.  
    22. for (int c =0; c<viewColumns; c++)
    23. {
    24. column=header->logicalIndex(c);
    25. if (!ui->printView->isColumnHidden(c))
    26. {
    27. if (ui->printView->model()->index(r,column).data(Qt::BackgroundRole)==Qt::yellow)
    28. mainTable +="<td bgcolor=\"yellow\"><font size=\"3\">" + ui->printView->model()->index(r,column).data(Qt::DisplayRole).toString()+ "</font></td>" ;
    29. else if (ui->printView->model()->index(r,column).data(Qt::BackgroundRole)==Qt::red)
    30. mainTable +="<td bgcolor=\"red\"><font size=\"3\">" + ui->printView->model()->index(r,column).data(Qt::DisplayRole).toString()+ "</font></td>" ;
    31. else
    32. mainTable +="<td><font size=\"3\">" + ui->printView->model()->index(r,column).data(Qt::DisplayRole).toString()+ "</font></td>" ;
    33. }
    34.  
    35. }
    To copy to clipboard, switch view to plain text mode 
    But it didn't work...the table was all messed up! I am off to bed...gonna sleep on it and try again tomorrow. Am I close?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Tableview after Model move section

    logicalIndex() returns the original order, use visualIndex() to get the order displayed by the table.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    Default Re: Tableview after Model move section

    Thanks everyone....I found the problem. It wasnt with the
    Qt Code:
    1. column=header->logicalIndex()
    To copy to clipboard, switch view to plain text mode 
    This does return the index of the correct item in the tableview. The problem was with the test:
    Qt Code:
    1. if (!ui->printView->isColumnHidden(i))
    To copy to clipboard, switch view to plain text mode 
    It should be:
    Qt Code:
    1. column=header->logicalIndex(i);
    2. if (!header->isSectionHidden(column))
    To copy to clipboard, switch view to plain text mode 
    I appreciate the help!

Similar Threads

  1. Replies: 2
    Last Post: 10th August 2011, 07:16
  2. Replies: 2
    Last Post: 7th September 2010, 08:38
  3. tableview , model and owncolumn
    By maston in forum Qt Programming
    Replies: 6
    Last Post: 28th August 2010, 11:00
  4. TableView/Model Display Question
    By SixDegrees in forum Qt Programming
    Replies: 9
    Last Post: 27th August 2010, 09:06
  5. tableview model view problem
    By skuda in forum Qt Programming
    Replies: 5
    Last Post: 3rd December 2007, 14:02

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.