# HG changeset patch # User Andre Heinecke # Date 1430921321 -7200 # Node ID f10d4e035eeccb8f5a6d24e572df30f7256428b4 # Parent 40a683d1a3183101fefc1a200fcf4d7a6782833f (issue10) Use width of header as minimum of the column size diff -r 40a683d1a318 -r f10d4e035eec src/metadataview.cpp --- a/src/metadataview.cpp Wed May 06 16:07:35 2015 +0200 +++ b/src/metadataview.cpp Wed May 06 16:08:41 2015 +0200 @@ -20,6 +20,8 @@ #include #include #include +#include +#include MetaDataView::MetaDataView(QWidget *parent, Qt::WindowFlags f) : QWidget(parent, f) { @@ -47,7 +49,6 @@ mView->setModel(mSortModel); mView->horizontalHeader()->setStretchLastSection(true); - mView->resizeColumnsToContents(); // mView->setColumnWidth(0, 60); mView->setSelectionBehavior(QAbstractItemView::SelectRows); mView->setSelectionMode(QAbstractItemView::SingleSelection); @@ -68,6 +69,7 @@ mSortModel->setSourceModel(mCSVModel); qDebug() << "Parsed: " << mCSVModel->rowCount() << " rows."; applyDefaultSort(); + resizeColsToHeaders(); return QString(); } @@ -179,10 +181,23 @@ } } if (idx == -1) { - qDebug() << "Failed to find configured sort column: " << sortField; return; } qDebug() << "Applying default sort order on column " << idx; mView->sortByColumn(idx, sortAsc ? Qt::AscendingOrder : Qt::DescendingOrder); } +void MetaDataView::resizeColsToHeaders() { + QFontMetrics fm(qApp->font()); + /* We do this manually here to avoid resizing to the real contents as + * we want the columns in the width of the header data. And we only + * want to increase that size. */ + for (int i=0; i < mSortModel->columnCount(); i++) { + const QString entry = mSortModel->headerData(i, Qt::Horizontal).toString(); + int w = fm.width(entry) + 20; + if (w > mView->horizontalHeader()->sectionSize(i)) { + mView->horizontalHeader()->resizeSection(i, w); + qDebug() << "Resizing " << i << " to: " << w; + } + } +} diff -r 40a683d1a318 -r f10d4e035eec src/metadataview.h --- a/src/metadataview.h Wed May 06 16:07:35 2015 +0200 +++ b/src/metadataview.h Wed May 06 16:08:41 2015 +0200 @@ -59,6 +59,9 @@ /**@brief applies the default sort order from configuration */ void applyDefaultSort(); + /**@brief resizes the columns to headers content */ + void resizeColsToHeaders(); + protected: QxtCsvModel *mCSVModel; QSortFilterProxyModel *mSortModel;