Results 1 to 1 of 1

Thread: [Solved] Synchronizing column widths between QTableViews

  1. #1
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default [Solved] Synchronizing column widths between QTableViews

    I have two tables displayed one above the other. The second table is a sorted/filtered/processed version of the first table, with exactly the same columns. I would like to have the columns of the second table line up exactly with the first table.

    To accomplish this, I have trivially sub-classed QTableView and added a slot called MyTableView::updatedSectionWidth(int,int,int) that is linked to the standard implementation's QHeaderView::sectionResized(int,int,int) signal of the opposing table's horizontal header. The slot then synchronizes the receiving table's header view with the sending table's column width. When a user uses his mouse to resize the column in one table, the column in the other table resizes in sync, as expected.

    I would like to resize the columns in both tables programmatically using QTableView::resizeColumnToContents. Unfortunately, that method does not appear to emit the sectionResized signal like I expected.

    How can I accomplish the functionality I intend? Since the contents in the two tables might occupy a different amount of space, I cannot simply call resizeColumnToContents on both tables. I want them to match the width of the larger of the two tables. The following code does not appear to have the results I would expect:
    Qt Code:
    1. ui->viewMain->resizeColumnToContents(columnid);
    2. ui->viewSummary->resizeColumnToContents(columnid);
    3. int mainsize = ui->viewMain->horizontalHeader()->sectionSize(columnid);
    4. int summsize = ui->viewSummary->horizontalHeader()->sectionSize(columnid);
    5. ui->viewMain->horizontalHeader()->resizeSection(columnid, qMax(mainsize, summsize));
    To copy to clipboard, switch view to plain text mode 


    Added after 1:


    Nevermind. The signal was getting emitted as expected, but resizing viewSummary overwrote the resizing of the first operation. The above code works when I temporarily block the signals as shown, and resize the viewSummary as well.

    Qt Code:
    1. ui->viewMain->horizontalHeader()->blockSignals(true);
    2. ui->viewSummary->horizontalHeader()->blockSignals(true);
    3.  
    4. ui->viewMain->resizeColumnToContents(columnid);
    5. ui->viewSummary->resizeColumnToContents(columnid);
    6. int mainsize = ui->viewMain->horizontalHeader()->sectionSize(columnid);
    7. int summsize = ui->viewSummary->horizontalHeader()->sectionSize(columnid);
    8. ui->viewMain->horizontalHeader()->resizeSection(columnid, qMax(mainsize, summsize));
    9. ui->viewSummary->horizontalHeader()->resizeSection(cnum, qMax(mainsize, summsize));
    10.  
    11. ui->viewMain->horizontalHeader()->blockSignals(false);
    12. ui->viewSummary->horizontalHeader()->blockSignals(false);
    To copy to clipboard, switch view to plain text mode 

    This allows me to independently resize both tables to their contents, then resize them together using the larger of the two sizes.
    Last edited by Phlucious; 7th March 2013 at 00:17.

Similar Threads

  1. Drag&Drop between QTableViews
    By Babi07 in forum Qt Programming
    Replies: 0
    Last Post: 9th November 2012, 15:56
  2. Linking of 2 QTableViews
    By Taxi in forum Newbie
    Replies: 6
    Last Post: 4th October 2011, 09:32
  3. Saving QTableView Column Widths
    By stefanadelbert in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2010, 16:23
  4. Dynamic Sizing of QTableView Column Widths
    By tntcoda in forum Qt Programming
    Replies: 1
    Last Post: 17th June 2009, 18:30
  5. auto resizing column/row widths
    By raman_31181 in forum Qt Programming
    Replies: 2
    Last Post: 11th September 2008, 18:16

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.