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:
ui->viewMain->resizeColumnToContents(columnid);
ui->viewSummary->resizeColumnToContents(columnid);
int mainsize = ui->viewMain->horizontalHeader()->sectionSize(columnid);
int summsize = ui->viewSummary->horizontalHeader()->sectionSize(columnid);
ui->viewMain->horizontalHeader()->resizeSection(columnid, qMax(mainsize, summsize));
ui->viewMain->resizeColumnToContents(columnid);
ui->viewSummary->resizeColumnToContents(columnid);
int mainsize = ui->viewMain->horizontalHeader()->sectionSize(columnid);
int summsize = ui->viewSummary->horizontalHeader()->sectionSize(columnid);
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.
ui->viewMain->horizontalHeader()->blockSignals(true);
ui->viewSummary->horizontalHeader()->blockSignals(true);
ui->viewMain->resizeColumnToContents(columnid);
ui->viewSummary->resizeColumnToContents(columnid);
int mainsize = ui->viewMain->horizontalHeader()->sectionSize(columnid);
int summsize = ui->viewSummary->horizontalHeader()->sectionSize(columnid);
ui->viewMain->horizontalHeader()->resizeSection(columnid, qMax(mainsize, summsize));
ui->viewSummary->horizontalHeader()->resizeSection(cnum, qMax(mainsize, summsize));
ui->viewMain->horizontalHeader()->blockSignals(false);
ui->viewSummary->horizontalHeader()->blockSignals(false);
ui->viewMain->horizontalHeader()->blockSignals(true);
ui->viewSummary->horizontalHeader()->blockSignals(true);
ui->viewMain->resizeColumnToContents(columnid);
ui->viewSummary->resizeColumnToContents(columnid);
int mainsize = ui->viewMain->horizontalHeader()->sectionSize(columnid);
int summsize = ui->viewSummary->horizontalHeader()->sectionSize(columnid);
ui->viewMain->horizontalHeader()->resizeSection(columnid, qMax(mainsize, summsize));
ui->viewSummary->horizontalHeader()->resizeSection(cnum, qMax(mainsize, summsize));
ui->viewMain->horizontalHeader()->blockSignals(false);
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.
Bookmarks