Claire Huang
Claire Huang

Reputation: 981

In QTableView, What "signal" triggers the editing mode

I'm trying to write some Inherit class (Let's call it A) from QTableView, and hope to overwrite the slot

void edit ( const QModelIndex & index )

from QAbstractItemView. I know that this function can trigger editing mode, but here is my question: I hope that whenever editing mode are triggered by user in this class A, the program can go inside the overwritten slot A::edit.

However, it seems only when A::edit are directly called, the program can get in.

Since QTableView comes from QAbstractItemView, if the editing mode is triggered by other way(such as mouse double click), the program will run default QAbstractItemView::edit instead of A::edit.

I tried to connect the signal "activated", but apparently it's not the correct signal to trigger editing mode. Is there anyway to do something every time when an editor is triggered?

Also, I'd like to do something when the editing mode is ended by pressing Enter or ESC or mouse clicking on other place. The same situation happens at the slot

void editorDestroyed ( QObject * editor )

Can anyone help me to solve this problem? I'll really appreciate it, thanks!

Upvotes: 2

Views: 3662

Answers (1)

Tom Panning
Tom Panning

Reputation: 4772

QAbstractItemView::edit(const QModelIndex& index) is not virtual, so that's why when you call it directly you get your subclass's behavior, but when existing code calls it they get they base class behavior. If that doesn't ring a bell, read that link; Qt has a lot of virtual and non-virtual functions and knowing what the difference is will save you a lot of headaches.

However, QAbstractItemView::edit(const QModelIndex& index, EditTrigger trigger, QEvent* event) is virtual, so you can override it. I haven't verified it, but presumably the non-virtual edit() calls this edit(), so it should have the same effect.

QAbstractItemView::editorDestroyed(QObject* editor) is also virtual, so I'm not sure why it isn't be working. However, there is also QAbstractItemView::closeEditor(QWidget* editor, QAbstractItemDelegate::EndEditHint hint) which is also virtual, so you might want to try reimplementing that in your subclass. The closeEditor() documentation also suggests commitData(), which is also virtual. The Qt item views have a lot of similar methods, so don't assume that the first one you see is going to do exactly what you want/expect.

FYI, in case your not used to reimplementing virtual methods in a subclass, the fastest/easiest way to make sure your implementation is being called is to do something like:

class A : public QTableView {
    void closeEditor ( QWidget * editor, QAbstractItemDelegate::EndEditHint hint ) {
        qDebug("my closeEditor was called!");
        // call the real implementation so that the base class continues to work properly
        QTableView::closeEditor(editor, hint);
    }
};

You could do this with edit(), editorDestroyed(), closeEditor(), and commitData() to see which ones are called when.

Upvotes: 5

Related Questions