QT:如何绘制(和使用)lendedit在代表中?
我有一个自定义列表,并且在视图上(使用QStyleDitemdelegate
)我想要显示许多内容,包括文本编辑 (考虑一个在线购物车,您可以使用这些物品(其中的照片和信息),然后您可以更改数量,但可以在文本编辑中更改数量,而不是Spinbox)。
本文编辑应该能够与模型进行通信。目前,我只能绘制一个空的textedit,但是我不知道如何将其正确连接到editoreRvent
(以及createeditor
,seteDitordata
) 。
void CustomDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &opt,
const QModelIndex &idx) const
{
// My other painting stuff (labels, shapes...)
QStyleOptionFrame panelFrame;
QLineEdit lineEdit;
panelFrame.initFrom(&lineEdit);
panelFrame.rect = rectLE;
panelFrame.state |= QStyle::State_Sunken;
QApplication::style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panelFrame, painter);
}
QWidget *CustomDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
auto editor = new QLineEdit(parent);
editor->setText("test");
return editor;
}
void CustomDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
auto lineEdit = dynamic_cast<QLineEdit*>(editor);
if(lineEdit)
{
lineEdit->setText("test2");
}
}
作为结果,我只能看到一个空的laneedit
,并且不能真正与之互动。
- 如果我将在一个
ModelIndex
委托中具有多型laneedit
s,我该如何在seteDitorData
和createedeeditor
中区分它们。 ?
谢谢
I have a custom list, and on the view (with the QStyledItemDelegate
) I want display many things, including a text edit
(think about an online shopping cart where you have the items (photos and infos of them) and next to them you can change the quantity, but within a text edit, and not a spinbox).
This text edit should be able to communicate with the model. Currently I can only draw an empty textEdit, but I don't know how to connect it properly to the editorEvent
( and createEditor
, setEditorData
).
void CustomDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &opt,
const QModelIndex &idx) const
{
// My other painting stuff (labels, shapes...)
QStyleOptionFrame panelFrame;
QLineEdit lineEdit;
panelFrame.initFrom(&lineEdit);
panelFrame.rect = rectLE;
panelFrame.state |= QStyle::State_Sunken;
QApplication::style()->drawPrimitive(QStyle::PE_PanelLineEdit, &panelFrame, painter);
}
QWidget *CustomDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
auto editor = new QLineEdit(parent);
editor->setText("test");
return editor;
}
void CustomDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
auto lineEdit = dynamic_cast<QLineEdit*>(editor);
if(lineEdit)
{
lineEdit->setText("test2");
}
}
As a the result I can only see an empty lineEdit
and can't really interact with it.
- If I would have multpiple
lineEdit
s inside onemodelIndex
delegate, how could I differentiate them in thesetEditorData
andcreateEditor
?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,
qabStractItemview中的编辑器
仅由编辑触发器创建,这些触发器已设置为它。请参阅 docs>行作为常规小部件,您可以使用解决方法。
我已经使用
qlistView
和qStyledIteMdelegate
使用了这种方法。当我面对相同的问题时,我已经跟踪了
fiewport
的鼠标移动事件,以在鼠标光标下获取qmodelindex
,并且是否有效并且与以前的不同值,我关闭一个编辑器(如果打开了),并使用方法QABSTRACTITEMVIEW :: CLOSSERSTERSENTEDITOR
和QABSTRACTITEMVIEW :: openPersistEntEdeTeditor
。在我的
中,QStyleDitemdelegate
派生的类,我超出了createeditor
,seteDitordAta
andUpdateEditorGeometry
methods。在
createeditor
中只需创建一个小部件,in更新eDitoregeometry
setGeemetry to Editor to Editor,inseteDitordAta
获取所需的数据以从Qmodelindex
并将其设置为编辑器小部件。我用来在
paint
方法中渲染所有行的同一小部件类。By default, editor in
QAbstractItemView
is created only by edit triggers, those are set to it. Refer to docsTo make a view interactive in sense of rendered rows as regular widgets you can use a workaround.
I've used this approach with
QListView
andQStyledItemDelegate
.When i faced with same problem, i've tracked a
viewport
's mouse move event, to get aQModelIndex
under the mouse cursor and if it was valid and different from previous value, i close an editor (if one was opened) and open a new one using methodsQAbstractItemView::closePersistentEditor
andQAbstractItemView::openPersistentEditor
.In my
QStyledItemDelegate
derived class i've overridedcreateEditor
,setEditorData
andupdateEditorGeometry
methods.In
createEditor
just create a widget, inupdateEditorGeometry
setGeometry to editor, insetEditorData
get required data to render fromQModelIndex
and set it to the editor widget.The same widget class i've used to render all rows in view in
paint
method.我在实现中忘记了几件事:
与
landedit
“互动”qt :: Intipisedisitable
必须在qabStractListModel :: flags中设置标志()
,否则编辑器函数将不会被调用。重新进来
UpdateEditorGeometry()
在其中指定landedit
的位置。重新进来
setModeldata()
您可以在其中与模型进行通信。在
drawprimitive
( drawprimitive )只是绘制框架之后,在代表的paint()
函数中绘制文本您也在leandit上写的文字。I forgot several things in my implementation:
To "interact" with the
lineEdit
theQt::ItemIsEditable
flag must be set in theQAbstractListModel::flags()
, otherwise the Editor functions in the Delegate won't get called.To reimplement
updateEditorGeometry()
where you specify thelineEdit
's position.To reimplement
setModelData()
where you can communicate with the model.To draw the text in the Delegate's
paint()
function afterdrawPrimitive
(drawPrimitive just draws the frame, but you want to draw the text you wrote in lineEdit as well. You can get it from the model)