Merge pull request #742 from ggrtk/qtutils

QtUtils: Improve QTableView column resizing
This commit is contained in:
Connor McLaughlin 2020-08-13 01:06:12 +10:00 committed by GitHub
commit 323574348b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 5 deletions

View File

@ -4,6 +4,7 @@
#include <QtGui/QDesktopServices>
#include <QtGui/QKeyEvent>
#include <QtWidgets/QDialog>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMessageBox>
#include <QtWidgets/QScrollBar>
@ -43,15 +44,18 @@ QWidget* GetRootWidget(QWidget* widget, bool stop_at_window_or_dialog)
void ResizeColumnsForTableView(QTableView* view, const std::initializer_list<int>& widths)
{
const int min_column_width = view->horizontalHeader()->minimumSectionSize();
const int max_column_width = view->horizontalHeader()->maximumSectionSize();
const int total_width =
std::accumulate(widths.begin(), widths.end(), 0, [](int a, int b) { return a + std::max(b, 0); });
std::accumulate(widths.begin(), widths.end(), 0, [&min_column_width, &max_column_width](int a, int b) {
return a + ((b < 0) ? 0 : std::clamp(b, min_column_width, max_column_width));
});
const int padding = 2;
const int scrollbar_width = ((view->verticalScrollBar() && view->verticalScrollBar()->isVisible()) ||
view->verticalScrollBarPolicy() == Qt::ScrollBarAlwaysOn) ?
view->style()->pixelMetric(QStyle::PM_ScrollBarExtent) :
view->verticalScrollBar()->width() :
0;
const int flex_width = std::max(view->width() - total_width - scrollbar_width - padding, 1);
const int flex_width = std::max(view->contentsRect().width() - total_width - scrollbar_width, 1);
int column_index = 0;
for (const int spec_width : widths)

View File

@ -20,7 +20,7 @@ QFrame* CreateHorizontalLine(QWidget* parent);
/// Returns the greatest parent of a widget, i.e. its dialog/window.
QWidget* GetRootWidget(QWidget* widget, bool stop_at_window_or_dialog = true);
/// Resizes columns of the table view to at the specified widths. A width of -1 will stretch the column to use the
/// Resizes columns of the table view to at the specified widths. A negative width will stretch the column to use the
/// remaining space.
void ResizeColumnsForTableView(QTableView* view, const std::initializer_list<int>& widths);