PDA

View Full Version : QTableView column to take size of delegate editor



Cotlone
21st April 2010, 03:46
Hey everyone,

This is my first post here, I'm pretty new to Qt and I'd appreciate your help, thanks.

I've got a QTableView which I have stuck the example SpinBox delegate in. My problem is the when I edit a value in the table, the spinBox is has a small width due to my column heading labels being short. As a result, the values in the spinBox editor become difficult to read (seeing only one character at a time).

I attempted to use editor->setMinimumWidth(45); within the delegate constructor, which does change the size of the delegate editor when it is opened. Unfortunately the column width seems to have been determined based on the display delegate, meaning it overlaps the column to its right.

I'd appreciate it if anyone could let me know if there is an easy way to base the column's width on the size of the delegate editor not the delegate display. (not sure if my terminology is correct there)

For the set up of the table, I'm using resizeColumnsToContents() as below.



QTableView *table = new QTableView;
table->setModel(chanSetModel);
table->setItemDelegateForColumn(4, new SpinBoxDelegate);
table->resizeColumnsToContents();


... and this is my delegate constructor.



QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMinimum(0);
editor->setMaximum(63);
editor->setMinimumWidth(45);

return editor;
}



Thanks in advance for your help!

- Michael

Cotlone
29th April 2010, 05:21
For anybody who might face the same problem, my solution involved creating a reimplementation of the sizeHint function. I also removed the setMinimumWidth(45) line as it was no longer needed.



QSize SpinBoxDelegate::sizeHint(const QStyleOptionViewItem & /*option*/, const QModelIndex &/*index*/ ) const
{
return QSize(45,10);
}


Instead of using constants for values for this it would be to base the size on the font metrics I suppose, and probably use a monospaced font.