Delegate editor sizing problem
I have have implemented a custom delegate and model for editing all the QTableView cells in a very controlled way. When my delegates create the editor, it doesn't fill up the available space in the cell. Is there something that I need to do to make this happen?
Thanks in advance,
Susan:confused:
Re: Delegate editor sizing problem
Reimplement the sizeHint method, and return the cell size.
Re: Delegate editor sizing problem
How does the delegate find out the size of the cell? It has no direct access to the view that I know of.
Re: Delegate editor sizing problem
Sorry about that... sizeHint is a wrong path.
Try overriding QItemDelegate::updateEditorGeometry(...);
Like this:
Code:
[COLOR=#8B0000][FONT='Courier New,courier'][I][/I][/FONT][/COLOR]void MyDelegate::updateEditorGeometry( QWidget* editor, const QStyleOptionViewItem &option, const QModelIndex &index )
{
editor->setGeometry( option.rect );
}
Using option.rect should resize your editor to fit perfectly into the cell.
Regards
Re: Delegate editor sizing problem
Just what I was looking for! I'll give this a try.
--Susan
Re: Delegate editor sizing problem
Unfortunately, this method never gets called!!! I am not sure why. I have an example MVC implementation from a training class I attended and the delgates never implemented this method and the editor (spinbox or combo) was always sized correctly... Ideas?
Re: Delegate editor sizing problem
No idea why it isn't called, but take a look at the Spin Box delegate example in the Qt Demos ( Item Views section ).
It does something similar to what you want.
Re: Delegate editor sizing problem
I think its not called because in the Qt source for QItemDelegete, it isn't virtual!! (it is supposed to be). Stepping through the Qt code for creating the delegate editor, it invokes QItemDelegate::updateEditorGeometry, not my derivation! I'll look at the example that you site, but I think this is the culprit. My example from the class is pretty straight forward too and doesn't event reimplement that method... :(
Re: Delegate editor sizing problem
This is the member description from QItemDelegate. As you can see it is virtual. Also this member is public and const. Perhaps you didn't declare it as public in your class?
Re: Delegate editor sizing problem
Actually you're right. I just took a look at QItemDelegate.h and the member is not virtual.
But it is in QAbstractItemDelegate. Couldn't you derive your delegate from QAbstractItemDelegate instead?
Re: Delegate editor sizing problem
Quote:
Originally Posted by
smacchia
I think its not called because in the Qt source for QItemDelegete, it isn't virtual!!
It is virtual. It doesn't have to be marked as virtual in QItemDelegate definition, since it was declared as virtual in QAbstractItemDelegate.
Quote:
Originally Posted by
marcel
Also this member is public and const. Perhaps you didn't declare it as public in your class?
Also make sure you didn't forget about "const". It's quite common mistake.
Re: Delegate editor sizing problem
Oh duh - it was the "const". Thanks! It works great now.
Re: Delegate editor sizing problem
i got a similar problem with delegates and a QTreeView, but i'm using editing widgets that contain other widgets, eg. a QWidget containing a QLineEdit.
first, i tried adding a layout to the container widget and adding sub-widgets to it, but the result was that the item didn't seem to be editable anymore at all (or the widget has hone somewhere / was zero-sized or whatever).
i tried overriding the setGeometry function for my subclasses but when the delegate calls setGeometry, allways the standard QWidget function is called.
i declared setGeometry as:
Code:
virtual void setGeometry
(const QRect &r
)
Re: Delegate editor sizing problem
If you use a completely custom widget for editing, you need to reimplement setModelData() and setEditorData() in the delegate. And probably you need to set a proper focus policy and maybe even a focus proxy. See docs for details.
Re: Delegate editor sizing problem
i allready overloaded setModelData() and setEditorData(). editing works just fine - only the layout is messed up.
Re: Delegate editor sizing problem
Can we see the code of the widget and its layouts?
Re: Delegate editor sizing problem
here it is, the layout stuff has been commented out:
Code:
{
public:
{
//
//QVBoxLayout *p_pLayout = new QVBoxLayout();
//this->setLayout(p_pLayout);
};
~ItemEditor(void){};
virtual void setData
(QVariant &i_Data
) = 0;
};
class ItemLineEditor : public ItemEditor
{
private:
public:
ItemLineEditor
(QWidget *i_pParent
= NULL): ItemEditor
(i_pParent
) {
// without layout
//using a layout doesn't work at all
//m_pLineEdit = new QLineEdit(this);
//this->layout()->addWidget(m_pLineEdit);
}
{
return m_pLineEdit->text();
}
{
this->m_pLineEdit->setText(i_Data.toString());
}
// gets never called
virtual void setGeometry(const QRect& rect)
{
ItemEditor::setGeometry(rect);
this->m_pLineEdit->setGeometry(rect);
}
};
as you may see, i'm doing this because i need a unified way of accessing the user input from different widgets (combobox, doublespinbox, lineedit etc.)
Re: Delegate editor sizing problem
Quote:
Originally Posted by
maximAL
as you may see, i'm doing this because i need a unified way of accessing the user input from different widgets (combobox, doublespinbox, lineedit etc.)
Qt already supports that in form of "user properties".
Re: Delegate editor sizing problem
hmm, i actually didn't think of this.
i tried to implement it now, by ran into the problem that i can set a property for any kind of widget, but then there is no unified way to initialize the actual content of the widget.
eg, for a QLineEdit i could set the data property to the old value before editing, but how do i actually set the value as text for editing? i can't do it in the c'tor, since the property won't be set yet at this time.
Re: Delegate editor sizing problem
1. setGeometry() is not virtual, so overriding it doesn't make much sense,
2. You never set the geometry of the line edit - you should either be using layouts or reimplement resizeEvent() and use setGeometry() to set the geometry of all child widgets
3. I don't really know what you mean about a unified way to initialize the contest, but maybe seeing how it is done for a widget mapper helps:
http://doc.trolltech.com/qq/qq21-dat...toofferchoices (what's important is the content of set*Data methods).