Qt 具有多个小部件的委托

Qt 具有多个小部件的委托,qt,delegates,pyqt4,pyside,pyqt5,Qt,Delegates,Pyqt4,Pyside,Pyqt5,基本模拟: 你好, 我所拥有的: 包含我的项目的QAbstractListModel 将包含单个小部件或委托的QListView QListView中的每个项目都需要在小部件中有3个可编辑区域 前言: 我很难弄清楚如何让一个适当的分层小部件系统工作。如果您查看附加的图像,每个绿色框表示我的模型中的一个“项”,以及我视图中用户的一个不可侵犯项,而每个绿色框内的“小部件”上的数据是来自模型中该项并与之相关的数据 因此,如果有人在小部件1中编辑QLineEdit1,我希望Widget1相关项的模

基本模拟:

你好,

我所拥有的:

  • 包含我的项目的QAbstractListModel
  • 将包含单个小部件或委托的QListView
  • QListView中的每个项目都需要在小部件中有3个可编辑区域
前言:

我很难弄清楚如何让一个适当的分层小部件系统工作。如果您查看附加的图像,每个绿色框表示我的模型中的一个“项”,以及我视图中用户的一个不可侵犯项,而每个绿色框内的“小部件”上的数据是来自模型中该项并与之相关的数据

因此,如果有人在小部件1中编辑QLineEdit1,我希望Widget1相关项的模型中的数据会更新

问题:

我不知道如何让多个编辑器在同一个委托中工作。如果我点击该项(QLineEdit),索引当然会与主小部件项(绿色框)相关-我找不到任何东西来告诉我我点击了绘制的QLineEdit,因此如果我只是在createEditor内返回一个可编辑的QLineEdit,它将是一个通用的,放在任何地方,我需要找出它与哪个小部件相关,这样我就可以正确地更新模型,还可以让编辑器在我单击绘制的QLineEdit的位置绘制

我不知道如何让一个好的系统工作。我理解代表,我理解MVC,所有这些都很好。我只是不知道我想做的是可能的,还是我需要改变一下,做点别的。如果我真的需要尝试其他东西,我会很感激一些建议

谢谢

下面是一些基本代码(现在小部件中没有单独的项目,但是已经设置好了基础):

导入系统 从PySide导入QtCore、QtGui 委托\数据\角色=37 委托索引=0 类ItemWidget(QtGui.QWidget): def uuu init uuu(self,parent=None): super(ItemWidget,self)。\uuuuuu init\uuuuuuuuuuu(parent=parent) self.main_layout=QtGui.QVBoxLayout() self.setLayout(self.main_布局) 类ItemDelegate(QtGui.QStyledItemDelegate): def uuu init uuu(self,parent=None): 超级(ItemDelegate,self)。\uuuuu初始化(父项=父项) def sizeHint(自身、选项、索引): 返回QtCore.QSize(80,80) def createEditor(自身、父项、选项、索引): editor=QtGui.QLineEdit(父级) 编辑器。设置固定宽度(200) 编辑器。设置固定高度(50) 返回编辑器 def setEditorData(自身、编辑器、索引): item\u str=index.data(QtCore.Qt.DisplayRole) description=项目_str['description'] editor.setValue(说明) def setEditorData(自身、编辑器、索引): 打印“正在运行setEditorData” 如果index.column()==委托索引: 打印“代表” 其他: QtGui.QStyledItemDelegate(self、编辑器、索引) 类ItemModel(QtCore.QAbstractListModel): def uuu init uuu(self,parent=None): super(ItemModel,self)。\uuuuu init\uuuuuuuu(parent=parent) self.items=[ {'one':'1','name':'one','thumbnail':'charlie.jpg','description':'aabb'}, {'two':'2','name':'two','thumbnail':'charlie.jpg','description':'aabb'}, {'three':'3','name':'three','thumbnail':'charlie.jpg','description':'aabb'}, {'four':'4','name':'four','thumbnail':'charlie.jpg','description':'aabb'}, {'five':'5','name':'five','thumbnail':'charlie.jpg','description':'aabb'}, {'six':'6','name':'six','thumbnail':'charlie.jpg','description':'aabb'} ] def行数(自身,索引): 返回len(自我项目) def数据(self,index,role=QtCore.Qt.DisplayRole): 如果不是index.isValid(): 一无所获 如果角色==QtCore.Qt.DisplayRole:
如果0您的代码只定义了编辑器,也就是说,Qt在编辑项目时显示了什么。您需要提供一个绘制方法,然后Qt调用该方法为每个项目绘制

下面是一个示例(在C++中),它在项目的右边缘绘制了一个三点按钮:

void ColorDelegate::paint(
    QPainter * a_Painter,
    const QStyleOptionViewItem & a_Option,
    const QModelIndex & a_Index
) const
{
    // Draw the button:
    QStyleOptionButton button;
    button.rect = buttonRectFromItemRect(a_Option.rect);
    button.text = "...";
    button.state = QStyle::State_Enabled;
    QApplication::style()->drawControl(QStyle::CE_PushButton, &button, a_Painter);

    // Draw the text, using the original delegate:
    QStyleOptionViewItem txt(a_Option);
    txt.rect.setWidth(txt.rect.width() - txt.rect.height() - 1);
    Super::paint(a_Painter, txt, a_Index);
}

(摘自)

您的模型适合您?是的,我的模型工作正常-在您双击之前,现在没有任何显示,因为这只是在显示我很难知道如何知道在委托内部单击了什么。我觉得奇怪的是,在
role==QtCore.Qt.DisplayRole
中,您必须返回一个字符串,但您返回的是一个字典。我得到以下信息:再次说明,其中没有显示任何内容,这只是一个非常基本的视图,没有任何内容填充代理绘制的项目-可以在角色部分随意返回字符串,但这不是问题所指的。
void ColorDelegate::paint(
    QPainter * a_Painter,
    const QStyleOptionViewItem & a_Option,
    const QModelIndex & a_Index
) const
{
    // Draw the button:
    QStyleOptionButton button;
    button.rect = buttonRectFromItemRect(a_Option.rect);
    button.text = "...";
    button.state = QStyle::State_Enabled;
    QApplication::style()->drawControl(QStyle::CE_PushButton, &button, a_Painter);

    // Draw the text, using the original delegate:
    QStyleOptionViewItem txt(a_Option);
    txt.rect.setWidth(txt.rect.width() - txt.rect.height() - 1);
    Super::paint(a_Painter, txt, a_Index);
}