5
votes

I'm showing a popup menu to select some values in a QTableWidget. The lowest item is a "Modify list" entry, when I select it a new window should automatically appear and the QComboBox should vanish and the cell return to a Qt::DisplayRole state.

Now Qt has all those nice API-calls like QTableWidget.edit() and QTableWidget.editItem(), what I'm really looking for is a QTableWidget.endEditing(), preferably without specifying the index of the cell, though I could get that using this call:

table.currentIndex()

… but I don't know if I can guarantee that the current cell is the cell being edited at all times.

Is there an API to close those kind of editors?

Popup menu

5

5 Answers

7
votes

QTableWidget inherits 19 public slots from QWidget. One of those is setDisabled(), which should disable input events for that widget and all of its children.

I would try:

table.setDisabled( true );
table.setDisabled( false );

Although you said it does not work for you, there is an alternative method: If you don't like that (the table loses focus, I believe), you can try using EditTriggers. For example:

table.setEditTriggers( QAbstractItemView::NoEditTriggers );
6
votes

table.setCurrentItem(None) is what worked for me. (Don’t forget to block signals if you use some cellChanged/itemChanged slot function.)

This is with PyQt. For C++ I think replace None with NULL.

1
votes

You may be able to use QTableWidget.closePersistentEditor() to close the editor. However, QAbstractItemView.closeEditor() may be closer to what you want, especially since you seem to be comfortable with the QModelIndex-based API and are already using a custom editor widget.

1
votes

In my case, none of the options worked properly. So, I figured: I need to send the key press event to the line edit itself. The following works with QTreeView but probably does work with any other view or widget that opens a line edit to edit cells.

QWidget* editingWidget = treeView->findChild<QLineEdit*>();
if(editingWidget)
{
    QKeyEvent keyPressEvent(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
    QApplication::sendEvent(editingWidget, &keyPressEvent);
    QApplication::processEvents(); // see note below
}

In my case, I wanted to start editing another field directly when having finished editing one item. That is why I put processEvents there, in most cases you can probably remove that line.

PS: yeah, it's C++, but should be easily adaptable to Python. I found this thread when I searched for the C++ solution, so maybe it helps anyone else, too.

0
votes

I can't speak for list widgets. But, I got here trying to do something similar.

I was double-clicking a cell, and based on the column, bringing up a sub-form with a list, then when that was closed move to the next appropriate column based on the value selected.

My problem was I could get the value in the cell and "select" the next appropriate cell, but the original cell stayed selected in edit mode!

It finally dawned on me that my double-click was selecting the cell, ie. editing.

A single-click selects the cell but doesn't open an edit mode.

Side note: Never could get that sub-form to act truly modal, so I created a loop in the calling form: while the sub form was visible, with the only code being app.processEvents()