我正在尝试将项目添加到 QListWidget,选择该项目,然后开始编辑新项目。
该项目被添加,它被选中,但该行不会进入 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
不可编辑,因此如果需要,则必须启用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);
});