Re: Creating Custom Widgets With QStyleItemDelegate
I am having a hard time getting custom delegates to work. I have a QTreeView where each item could require a separate input widget. One could need a simple QLineEdit, another could require a QComboBox, while others may require a custom widget which contains a QLabel next to a QPushButton. I can get the simple widgets to work with QStyledItemDelegate or QItemDelegate, but when I try to show my custom widget (QLabel next to a QPushButton) in createEditor, nothing shows up. Is it possible to show custom widgets as delegates? I have seen a lot of posts reference the paint() method in the delegate class, but I am not sure if I am suppose to draw the widget with the paint method?? Any ideas?
Thanks
Sorry, in the first line I say custom delegates, I meant to say custom widgets working in delegates
Re: Creating Custom Widgets With QStyleItemDelegate
Quote:
Is it possible to show custom widgets as delegates?
Yes.
Quote:
I have seen a lot of posts reference the paint() method in the delegate class, but I am not sure if I am suppose to draw the widget with the paint method?
For your case of composite widget, re-implementig paint() is not required.
Quote:
I can get the simple widgets to work with QStyledItemDelegate or QItemDelegate, but when I try to show my custom widget (QLabel next to a QPushButton) in createEditor, nothing shows up.
The problem may be with the cell size in QTreeView, which may not be enough to fit the custom widget that was created using createEditor(). Try to create the editor widget with no parent, so that when editing is trigerred, you should see the custom composite widget pop up.
I have put togeather a small example, not very clean but works.
Code:
#include <QtGui>
#include <QApplication>
{
Q_OBJECT
public:
, mText(index.data().toString())
{
hBoxLayout->addWidget(mLineEdit,2);
hBoxLayout->addWidget(pushButton,1);
connect(pushButton, SIGNAL(clicked()), SLOT(updateText()));
mLineEdit->setText(mText);
}
public:
{
return mText;
}
signals:
void textChanged
(const QString & text
);
public slots:
{
mText = text;
mLineEdit->setText(text);
}
private slots:
void updateText(void)
{
mText = mLineEdit->text();
emit textChanged(mText);
}
private:
};
class StyledItemDelegate : public QStyledItemDelegate
{
public:
explicit StyledItemDelegate
(QObject * parent
= 0) : QStyledItemDelegate(parent)
{
;
}
{
return new Editor(index, parent);
}
{
return Editor(index, 0).sizeHint();
}
protected:
{
static_cast<Editor*>(editor)->setText(index.data().toString());
}
{
static_cast<Editor*>(editor)->setGeometry(option.rect);
}
{
model->setData(index, static_cast<Editor*>(editor)->text());
}
};
{
model->setRowCount(10);
model->setColumnCount(2);
for(int r = 0; r < model->rowCount(); r ++)
for(int c = 0; c < model->columnCount(); c++)
model
->setData
(model
->index
(r, c
),
QString("Item-%1,%2").
arg(r
).
arg(c
), Qt
::DisplayRole);
}
int main(int argc, char ** argv)
{
StyledItemDelegate delegate;
addData(&model);
treeView.setModel(&model);
treeView.setItemDelegate(&delegate);
treeView.setEditTriggers(treeView.DoubleClicked);
treeView.show();
return app.exec();
}
#include "main.moc"
Re: Creating Custom Widgets With QStyleItemDelegate
That was exactly the problem. I tried without parenting the custom widget and sure enough it showed up. Then I implemented the sizeHint and parented the custom widget and I was able to see the custom widget show up in the appropriate cell. Of course after I did the size hint I had very tall rows, but at least it is getting me in the right direction.
On a sizing note, I assume I can resize my widget to fit in the default size of the cell. Would that be the way to fit the custom widget, or is there a more elegant way?
Many thanks for your help!!
Re: Creating Custom Widgets With QStyleItemDelegate
Quote:
On a sizing note, I assume I can resize my widget to fit in the default size of the cell. Would that be the way to fit the custom widget, or is there a more elegant way?
It depends, if you can make the widget fit cell, else you may need to use sizeHint() of delegate and change the cell size based on whether it is being edited, or just being displayed.
Re: Creating Custom Widgets With QStyleItemDelegate
Well I can use the size hint as you suggest, but it causes the row heights to increase too much. I can pass the row height to my widget I am using in the delegate. I even size the widget to match the row height that is currently in the treeview, however the widget seems to be offset in that index and does not show the whole thing.
I have explored setting sizeHints, but I don't see how a user programmitically sets a size hint. Are you not allowed to dictate the size of your widgets?
Added after 9 minutes:
Nevermind my last question. It appears my problem has to do with my margins used in the layout. Simply adjusting setContentsMargins(0,0,0,0) fixed the problem!!