diff src/metadataview.cpp @ 35:f10d4e035eec

(issue10) Use width of header as minimum of the column size
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 06 May 2015 16:08:41 +0200
parents 4e16fbd10945
children 0c05958d254c
line wrap: on
line diff
--- 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 <QHeaderView>
 #include <QItemSelectionModel>
 #include <QSettings>
+#include <QFontMetrics>
+#include <QApplication>
 
 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;
+        }
+    }
+}
This site is hosted by Intevation GmbH (Datenschutzerklärung und Impressum | Privacy Policy and Imprint)