Paste

URL To paste - | raw - Sun Jan 27 2019 15:58:00 GMT+0000 (Coordinated Universal Time)
#include "mheaderview.h"

MHeaderView::MHeaderView(Qt::Orientation orientation, QWidget *parent)
  : QHeaderView(orientation, parent)
{
}

void MHeaderView::setIdealColumnSize(int logicalIndex)
{
  QAbstractItemModel *mod = model();
  if (mod == nullptr)
  {
    return;
  }

  int oldSize = sectionSize(logicalIndex);
  int newSize = oldSize;

  for (int i = 0; i < mod->rowCount(); i++)
  {
    QString text = mod->data(mod->index(i, logicalIndex)).toString();
    newSize = std::max(newSize, fontMetrics().horizontalAdvance(text));
  }

  // There is still some margin somewhere... This should probably come from QStyle?
  newSize += 4;

  if (newSize != oldSize)
  {
    resizeSection(logicalIndex, newSize);
    emit sectionResized(logicalIndex, oldSize, newSize);
  }
}