QT:如何绘制(和使用)lendedit在代表中?

发布于 2025-02-06 19:05:28 字数 1493 浏览 3 评论 0原文

我有一个自定义列表,并且在视图上(使用QStyleDitemdelegate)我想要显示许多内容,包括文本编辑 (考虑一个在线购物车,您可以使用这些物品(其中的照片和信息),然后您可以更改数量,但可以在文本编辑中更改数量,而不是Spinbox)。

本文编辑应该能够与模型进行通信。目前,我只能绘制一个空的textedit,但是我不知道如何将其正确连接到editoreRvent(以及createeditorseteDitordata) 。

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,我该如何在seteDitorDatacreateedeeditor中区分它们。 ?

谢谢

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 lineEdits inside one modelIndex delegate, how could I differentiate them in the setEditorData and createEditor?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稍尽春風 2025-02-13 19:05:29

默认情况下,qabStractItemview中的编辑器仅由编辑触发器创建,这些触发器已设置为它。请参阅 docs

>行作为常规小部件,您可以使用解决方法。

我已经使用qlistViewqStyledIteMdelegate使用了这种方法。

当我面对相同的问题时,我已经跟踪了fiewport的鼠标移动事件,以在鼠标光标下获取qmodelindex,并且是否有效并且与以前的不同值,我关闭一个编辑器(如果打开了),并使用方法QABSTRACTITEMVIEW :: CLOSSERSTERSENTEDITORQABSTRACTITEMVIEW :: openPersistEntEdeTeditor

在我的中,QStyleDitemdelegate派生的类,我超出了createeditorseteDitordAta and UpdateEditorGeometry methods。

createeditor中只需创建一个小部件,in 更新eDitoregeometry setGeemetry to Editor to Editor,in seteDitordAta获取所需的数据以从Qmodelindex并将其设置为编辑器小部件。

我用来在paint方法中渲染所有行的同一小部件​​类。

By default, editor in QAbstractItemView is created only by edit triggers, those are set to it. Refer to docs

To make a view interactive in sense of rendered rows as regular widgets you can use a workaround.

I've used this approach with QListView and QStyledItemDelegate.

When i faced with same problem, i've tracked a viewport's mouse move event, to get a QModelIndex 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 methods QAbstractItemView::closePersistentEditor and QAbstractItemView::openPersistentEditor.

In my QStyledItemDelegate derived class i've overrided createEditor, setEditorData and updateEditorGeometry methods.

In createEditor just create a widget, in updateEditorGeometry setGeometry to editor, in setEditorData get required data to render from QModelIndex and set it to the editor widget.

The same widget class i've used to render all rows in view in paint method.

无语# 2025-02-13 19:05:29

我在实现中忘记了几件事:

  1. landedit“互动” qt :: Intipisedisitable必须在qabStractListModel :: flags中设置标志(),否则编辑器函数将不会被调用。

  2. 重新进来UpdateEditorGeometry()在其中指定landedit的位置。

  3. 重新进来setModeldata()您可以在其中与模型进行通信。

  4. drawprimitive drawprimitive )只是绘制框架之后,在代表的paint()函数中绘制文本您也在leandit上写的文字。

I forgot several things in my implementation:

  1. To "interact" with the lineEdit the Qt::ItemIsEditable flag must be set in the QAbstractListModel::flags(), otherwise the Editor functions in the Delegate won't get called.

  2. To reimplement updateEditorGeometry() where you specify the lineEdit's position.

  3. To reimplement setModelData() where you can communicate with the model.

  4. To draw the text in the Delegate's paint() function after drawPrimitive (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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文