[Interest] QStandardItemModel - Delegate with custom widget
Dirk Neumann
dirk.neumann at dn-informatik.de
Thu Jan 26 23:01:03 CET 2017
Hello,
I know that this is nearly an everlasting topic - place a custom Widget
into a QTreeView.
The widget I want to be displayed is up to now I arranged a QLabel and a
QTextEdit, but in Future the widget will be of higher complexity.
Nevertheless, the widget itself works for me and is called
ReqTextItemWidget.
I derived QStyledItemDelegate and overrode createEditor(),
setEditorData(), setModelData(). All these methods do what I expect,
the data from model will be passed into editor and vice versa, I can
edit data, fine.
I also overrode sizeHint() and paint() of the delegate. My sizeHint
method looks:
QSize ReqTextDelegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const{
ReqTextItemWidget *item_widget=new ReqTextItemWidget();
QString value=index.data().toString();
item_widget->set_caption("Unicornpolish");
item_widget->set_maintext(value);
item_widget->setGeometry(option.rect);
return item_widget->sizeHint();
}
where set_maintext() sets the text in the QTextEdit and set_caption()
sets the text in the QLabel, just to get a calculated size.
This is my paint()-method:
void ReqTextDelegate::paint(QPainter *painter,const
QStyleOptionViewItem &option, QModelIndex const& index)
const{ QPaintDevice* original_pdev_ptr = painter->device();
if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
ReqTextItemWidget *item_widget=new ReqTextItemWidget(); QString
value=index.data().toString();
item_widget->set_caption("Unicornpolish");
item_widget->set_maintext(value);
// geometry
item_widget->setGeometry(option.rect);
painter->end();
// rendering of QWidget itself
//region relative to option.rect
int region_x=-100; // ----> value
int region_y=-25; // ----> value
int region_w=option.rect.width()-region_x;
int region_h=option.rect.height()-region_y;
item_widget->render(painter->device(), QPoint(option.rect.x(),
option.rect.y()), QRegion(region_x, region_y, region_w, region_h),
QWidget::RenderFlag::DrawChildren);
painter->begin(original_pdev_ptr);
}
My problem now is the adjustment of the values region_x and region_y
for placing the widget just to the same place as the editor.
But hard coding values are eval. Not portable. No-Go. What can I do
here?
I also played around with setIndexWidget(), but then it seems
that I loose control over the widgets: Selecting and editing in one
widget has nothing to do with the selected item in my QTreeView. I
could catch some signals from the widgets to adjust the selection of
the QTreeView (and maybe can even handle it by using QSignalMapper) but
I fear the moment when I do some changes in the model which change the
tree structure...
Greetings
Dirk.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 833 bytes
Desc: Digitale Signatur von OpenPGP
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20170126/7fe1490d/attachment.sig>
More information about the Interest
mailing list