Set foreign key item delegate references key values combo box size according to contents

This commit is contained in:
Bill Somerville 2018-11-25 21:55:19 +00:00
parent 7b4b407455
commit 86fb68f305
2 changed files with 24 additions and 3 deletions

View File

@ -1,7 +1,10 @@
#include "ForeignKeyDelegate.hpp"
#include <QApplication>
#include <QComboBox>
#include <QFontMetrics>
#include <QSize>
#include <QStyleOptionViewItem>
#include "CandidateKeyFilter.hpp"
ForeignKeyDelegate::ForeignKeyDelegate (QAbstractItemModel const * referenced_model
@ -40,3 +43,20 @@ QWidget * ForeignKeyDelegate::createEditor (QWidget * parent
editor->setSizeAdjustPolicy (QComboBox::AdjustToContents);
return editor;
}
QSize ForeignKeyDelegate::sizeHint (QStyleOptionViewItem const& option, QModelIndex const& index) const
{
auto size_hint = QStyledItemDelegate::sizeHint (option, index);
QFontMetrics metrics {option.font};
QStyleOptionComboBox combo_box_option;
combo_box_option.rect = option.rect;
combo_box_option.state = option.state | QStyle::State_Enabled;
for (auto row = 0; row < candidate_key_filter_->rowCount (); ++row)
{
size_hint = size_hint.expandedTo (qApp->style ()->sizeFromContents (QStyle::CT_ComboBox
, &combo_box_option
, {metrics.width (candidate_key_filter_->data (candidate_key_filter_->index (row, 0)).toString ())
, metrics.height ()}));
}
return size_hint;
}

View File

@ -33,9 +33,10 @@ public:
, int referencing_key_role = Qt::EditRole);
~ForeignKeyDelegate ();
QWidget * createEditor (QWidget * parent, QStyleOptionViewItem const&, QModelIndex const&) const override;
private:
QWidget * createEditor (QWidget * parent, QStyleOptionViewItem const&, QModelIndex const&) const override;
QSize sizeHint (QStyleOptionViewItem const&, QModelIndex const&) const override;
QScopedPointer<CandidateKeyFilter> candidate_key_filter_;
};