我试图将一个项目添加到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);
}
答案 0 :(得分:1)
默认情况下,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);
});