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