Qt:单击后开始编辑单元格 [英] Qt: start editing of cell after one click
问题描述
默认情况下, QTableView
中的单元格在双击后开始编辑。如何改变这种行为。 我需要它才能在点击后开始编辑。
By default the cell in QTableView
starts being edited after double click. How to change this behavior. I need it to start editing after one click.
我已将组合框委托设置到单元格。单击单元格时,仅选择它。当双击单元格时,激活 QComboBox
编辑器,但不会展开。我希望它只是一个点击扩展,如果我添加 QComboBox
通过 setCellWidget
函数 QTableWidget
。我需要使用模型视图委托相同的效果。
I have set combo-box delegate to the cell. When clicking the cell it only selects it. When double clicking on the cell the QComboBox
editor is activated but not expanded. I want it to expand after just one click as if I added QComboBox
by setCellWidget
function of QTableWidget
. I need the same effect by using model-view-delegate.
推荐答案
点击后编辑
您可以重新实现mousePressEvent, / p>
Edit after one click You can reimplement mousePressEvent in view you are using
void YourView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
QModelIndex index = indexAt(event->pos());
if (index.column() == 0) { // column you want to use for one click
edit(index);
}
}
QTreeView::mousePressEvent(event);
}
编辑时展开QCombobox 应该在你的子类QItemDelegate中实现setEditorData,并在结束时调用showPopup。
Expanded QCombobox when edit You should imlement setEditorData in your subclass of QItemDelegate and at the end call showPopup.
但它有一些意想不到的行为。当鼠标离开其区域时,QComboBox消失。但对我来说是有利的。
我可以选择不同的项目与单击和释放。
But it has some unexpected behaviour. QComboBox disappears when mouse leave its area. But for me it is advantage. I can select different item with single click and release.
void IconDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
Q_UNUSED(index);
QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
// Add data
comboBox->addItem(QIcon(":/icons/information16.png"), "info");
comboBox->addItem(QIcon(":/icons/warning16.png"), "warning");
comboBox->addItem(QIcon(":/icons/send16.png"), "send");
comboBox->addItem(QIcon(":/icons/select16.png"), "select");
comboBox->showPopup(); // <<<< Show popup here
}
点击并按住以选择项目并在发布上提交数据(只需点击一下即可释放)
Together it works fast way. Click and hold to choose item and commit data on release ( Just one click and release )
点击显示扩展的qcombobox,然后点击选择/隐藏,我现在不知道解决方案。
If you want click to show expanded qcombobox and next click to choose/hide, I do not know solution for now.
这篇关于Qt:单击后开始编辑单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!