且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Qt:单击后开始编辑单元格

更新时间:2023-12-03 11:57:22

点击后编辑
您可以重新实现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.