QListWidget 的 editItem() 输出 "edit: editing failed"

QListWidget's editItem() outputs "edit: editing failed"

我正在尝试向 QListWidget 添加一个项目,select 该项目,然后开始编辑新项目。

项目被添加,并且被 selected,但是该行没有被引入 QLineEdit,或者它试图使用的任何东西。

这是我的小部件的代码,相关代码位于 "m_addButton" 的连接下:

Sequencer::Sequencer(QWidget* parent) :
    QWidget(parent)
{
    m_list = new QListWidget();
    m_addButton = new QPushButton("Add Step");
    m_removeButton = new QPushButton("Remove Step");

    connect(m_addButton, &QPushButton::clicked, this, [&]()
    {
        m_list->addItem(""); // This part works,
        m_list->setCurrentItem(m_list->item(m_list->count() - 1)); // This part works...
        m_list->editItem(m_list->currentItem()); // This part puts out "edit: editing failed" into the console.
    });

    connect(m_removeButton, &QPushButton::clicked, this, [&]()
    {
        if (!m_list->selectedItems().isEmpty())
        {
            qDeleteAll(m_list->selectedItems());
        }
    });

    QHBoxLayout* hLayout = new QHBoxLayout();
    hLayout->addStretch();
    hLayout->addWidget(m_addButton);
    hLayout->addWidget(m_removeButton);

    QVBoxLayout* vLayout = new QVBoxLayout();
    vLayout->addWidget(m_list);
    vLayout->addLayout(hLayout);

    setLayout(vLayout);
}

默认一个QListWidgetItem is not editable so if you want it to be, you must enable the Qt::ItemIsEditable标志:

connect(m_addButton, &QPushButton::clicked, this, [&]()
{
    QListWidgetItem *item = new QListWidgetItem;
    item->setFlags(item->flags() | Qt::ItemIsEditable); # enable
    m_list->addItem(item);
    m_list->setCurrentItem(item);
    m_list->editItem(item);
});