我遇到了我的QTableViewQItemDelegate类的问题。对于一个列,我的委托创建一个简单的组合框,一切都正常工作。对于我的第二列,我需要一个单个小部件中有两个组合框的小部件。

我在我的QItemDelegate中写下了以下代码,只是为了清除这仅显示我的第二列的代码,这是一个不起作用的代码。另一个简单的组合框未显示正常:

QWidget *UserDefinedUnitsDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index ) const
{
    //set up a simple widget with a layout
    QWidget* pWidget = new QWidget(parent);
    QHBoxLayout* hLayout = new QHBoxLayout(pWidget);
    pWidget->setLayout(hLayout);

    //add two combo boxes to the layout
    QComboBox* comboEditor = new QComboBox(pWidget);    
    QComboBox* comboEditor2 = new QComboBox(pWidget);   

    //now add both editors to this
    hLayout->addWidget(comboEditor);
    hLayout->addWidget(comboEditor2);
    return pWidget;
}
.

现在,这只是很好,但是当我编辑它并点击其他地方时,它不会停止编辑。任何人都可以提供任何指针吗?

编辑:所以我需要在某些时候调用commitdata()和closeiteditor()。任何人都可以在哪里提供指针?

谢谢。

有帮助吗?

解决方案

您可以将编辑器窗口小部件保留为类的成员,并在其中一个组合框的当前索引已更改时发出commitdata。因此,您可以将CurrentIndexChanged(int)连接到插槽并从那里发出commitdata:

QWidget *UserDefinedUnitsDelegate::createEditor(QWidget *parent,const QStyleOptionViewItem & option ,const QModelIndex & index ) const
{
    //set up a simple widget with a layout
    pWidget = new QWidget(parent);
    QHBoxLayout* hLayout = new QHBoxLayout(pWidget);
    pWidget->setLayout(hLayout);

    //add two combo boxes to the layout
    QComboBox* comboEditor = new QComboBox(pWidget);    
    QComboBox* comboEditor2 = new QComboBox(pWidget);   

    connect(comboEditor,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int)));
    connect(comboEditor2,SIGNAL(currentIndexChanged(int)),this,SLOT(setData(int)));

    //now add both editors to this
    hLayout->addWidget(comboEditor);
    hLayout->addWidget(comboEditor2);
    return pWidget;
}

void UserDefinedUnitsDelegate::setData(int val)
{
    emit commitData(pWidget);
}
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top