20

デフォルトでは、QTableViewダブルクリック後にセルの編集が開始されます。この動作を変更する方法。ワンクリックで編集を開始する必要があります。

コンボボックスデリゲートをセルに設定しました。セルをクリックすると、セルが選択されるだけです。セルをダブルクリックすると、QComboBoxエディタがアクティブになりますが、展開されません。の関数で追加QComboBoxしたかのように、ワンクリックで展開したい。model-view-delegate を使用して同じ効果が必要です。setCellWidgetQTableWidget

4

5 に答える 5

21

この関数setEditTriggersを使用して、編集トリガーを設定できます

C++

yourView->setEditTriggers(QAbstractItemView::AllEditTriggers)

パイソン:

yourView.setEditTriggers(QAbstractItemView.AllEditTriggers)

enum QAbstractItemView::EditTrigger フラグ QAbstractItemView::EditTriggers

この列挙型は、アイテムの編集を開始するアクションを記述します。

Constant    Value   Description
QAbstractItemView::NoEditTriggers   0   No editing possible.
QAbstractItemView::CurrentChanged   1   Editing start whenever current item changes.
QAbstractItemView::DoubleClicked    2   Editing starts when an item is double clicked.
QAbstractItemView::SelectedClicked  4   Editing starts when clicking on an already selected item.
QAbstractItemView::EditKeyPressed   8   Editing starts when the platform edit key has been pressed over an item.
QAbstractItemView::AnyKeyPressed    16  Editing starts when any key is pressed over an item.
QAbstractItemView::AllEditTriggers  31  Editing starts for all above actions.

EditTriggers 型は、QFlags の typedef です。EditTrigger 値の OR の組み合わせを格納します。

于 2015-07-03T03:33:07.167 に答える
17

ワンクリックで編集 使用しているビューで mousePressEvent を再実装できます

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 を呼び出す必要があります。

しかし、いくつかの予期しない動作があります。マウスがその領域を離れると、QComboBox は消えます。しかし、私にとっては利点です。シングルクリックとリリースで別のアイテムを選択できます。

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
}

一緒にそれは速い方法で動作します。クリックしたままにしてアイテムを選択し、リリース時にデータをコミットします(ワンクリックしてリリースするだけです) 。

クリックして展開されたqcomboboxを表示し、次にクリックして選択/非表示にする場合、今のところ解決策がわかりません。

于 2013-09-17T12:53:15.053 に答える