[SOLVED] Reg : Need some help with using custom StyledItemDelegate - Column Not Resizing
-
Hello
I am facing the following issue. I have three SytedItemDelegates (comboboxdelegate - to display comboboxes ; datedelegate for displaying delegates and picturedelegate for displaying QPixmap). I then call setItemDelegate for column for corresponding display requirement.
However, when the code runs the photo column does not show correctly. I need to manually click the row number for it to resize. Any pointers will be helpful. -
did you reimplement sizeHint() of your delegates correctly?
-
Thanks for the reply.
This is the sizeHint code for
- comboboxdelegate
QSize mycomboboxdelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QSize(80,80);
}
void mycomboboxdelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{painter->drawText(option.rect,Qt::AlignCenter,index.data(Qt::DisplayRole).toString());
}
- DateDelegate
QSize mydateeditdelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
qDebug()<<"Photo Delegate Called";
return QSize(80,80);}
void mydateeditdelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QStyleOptionViewItemV4 display_option(option); display_option.text = QDate::fromString(index.data().toString(), Qt::ISODate).toString("ddd dd/MMM/yyyy"); QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &display_option, painter);
}
- Photo Delegate
QSize myphotodelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
qDebug()<<"Photo Delegate Called";
return QSize(80,80);
}
void myphotodelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QByteArray array = index.data().toByteArray();
QPixmap pixmap;
pixmap.loadFromData(array,"PNG",Qt::AutoColor);
pixmap.scaled(80,80,Qt::KeepAspectRatio,Qt::FastTransformation);
painter->drawPixmap(option.rect,pixmap);
}Code where the above delegates are called.
ui->tableView->setModel(employmentstatus);
ui->tableView->setItemDelegateForColumn(2,photodelegate);
ui->tableView->setItemDelegateForColumn(3,combodelegate);
ui->tableView->setItemDelegateForColumn(5,combodelegate);
ui->tableView->setItemDelegateForColumn(6,combodelegate);
ui->tableView->resizeColumnsToContents();
ui->tableView->resizeRowToContents(2);Please help
-
please use code-tags ... it's a pain reading code like this without it.
-
One Question, I am creating the above mentioned delegates and passing through the constructor the form name. Based upon form name, i am returning the QSize, Would this be a better approach when I am using the same delegates for multiple formats.
-
Hello Sorry for bothering, but can you provide me a tip on using code tags.
-
[quote author="roahanramesh" date="1377004715"]Hello Sorry for bothering, but can you provide me a tip on using code tags. [/quote]
There is convenience button in the "toolbar" when you write/edit a post. But it's a simple as surrounding your code with @-signs. -
- MyDateDelegate .cpp implementation
@QSize mydateeditdelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
qDebug()<<"Date Delegate Called";
return QSize(80,80);
}
void mydateeditdelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{QStyleOptionViewItemV4 display_option(option); display_option.text = QDate::fromString(index.data().toString(), Qt::ISODate).toString("ddd dd/MMM/yyyy"); QApplication::style()->drawControl(QStyle::CE_ItemViewItem, &display_option, painter);
}
@-
MyCombobox Implementation
@
QSize mycomboboxdelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QSize(80,80);
}
void mycomboboxdelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{painter->drawText(option.rect,Qt::AlignCenter,index.data(Qt::DisplayRole).toString());
}
@ -
PictureDelegate Implementation
@QSize myphotodelegate:: sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
qDebug()<<"Photo Delegate Called";
return QSize(80,80);
}
void myphotodelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QByteArray array = index.data().toByteArray();
QPixmap pixmap;
pixmap.loadFromData(array,"PNG",Qt::AutoColor);
pixmap.scaled(80,80,Qt::KeepAspectRatio,Qt::FastTransformation);
painter->drawPixmap(option.rect,pixmap);
}
@ -
Code where the above delegates get called and implemented
@ employmentstatus = new QSortFilterProxyModel(this);
employmentstatus->setSourceModel(branchfilter);
employmentstatus->setDynamicSortFilter(true);
employmentstatus->setFilterKeyColumn(-1);
employmentstatus->setFilterRegExp("ACTIVE");combodelegate = new mycomboboxdelegate(formname,this);
datedelegate = new mydateeditdelegate(formname,this);
photodelegate = new myphotodelegate(formname,this);ui->tableView->setModel(employmentstatus);
ui->tableView->setItemDelegateForColumn(2,photodelegate);
ui->tableView->setItemDelegateForColumn(3,combodelegate);
ui->tableView->setItemDelegateForColumn(5,combodelegate);
ui->tableView->setItemDelegateForColumn(6,combodelegate);
ui->tableView->resizeColumnsToContents();
ui->tableView->resizeRowsToContents();
@
- MyDateDelegate .cpp implementation
-
I'm not sure if it will solve your problem:
but also set the delegate for the whole view.
@
ui->tableView->setItemDelegate(photodelegate);
@And you could also combine your 2 delegates and implement them in 1 single implementation. This saves you the amount of code to manage.
You get also the QModelIndex passed to the methods, which lets to check in which column you currently are. -
I tried to use this approach, but however, in places where the delegates dont get applied, the lineedit does not appear, It crashes the software, could you please help.