【发布时间】:2022-06-10 23:59:29
【问题描述】:
我有一个自定义列表,在视图上(使用QStyledItemDelegate)我想显示很多东西,包括文本编辑
(考虑一个在线购物车,您可以在其中拥有商品(照片和信息),在它们旁边您可以更改数量,但在文本编辑中,而不是在旋转框中)。
此文本编辑应该能够与模型通信。目前我只能画一个空的textEdit,但我不知道如何正确连接到editorEvent(和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");
}
}
因此,我只能看到一个空的lineEdit 并且无法真正与之交互。
- 如果我在一个
modelIndex代表中包含多个lineEdits,我如何在setEditorData和createEditor中区分它们?
谢谢
【问题讨论】:
标签: c++ qt delegates qpainter qlineedit