Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble getting `QWidget* editor` for a `QModelIndex`
    primarykey
    data
    text
    <p>I am having trouble augmenting the default edit behavior of <a href="http://qt-project.org/doc/qt-4.8/qtableview.html" rel="nofollow"><code>QTableView</code></a>. I want the following behavior when the Enter key is pressed:</p> <ol> <li>Start editing the current cell if it is not already being edited.</li> <li>If the cell is being edited, <ul> <li>2a. commit the data and close the editor. Then,</li> <li>2b. make the cell below, if present, the current cell.</li> </ul></li> </ol> <p>2a is the default behavior, and 2b can likely be achieved by using <a href="http://qt-project.org/doc/qt-4.8/qabstractitemview.html#setCurrentIndex" rel="nofollow"><code>QAbstractItemView::setCurrentIndex()</code></a> in a re-implementation of <a href="http://qt-project.org/doc/qt-4.8/qitemdelegate.html#eventFilter" rel="nofollow"><code>QItemDelegate::eventFilter()</code></a> (as suggested <a href="http://qt-project.org/faq/answer/how_can_i_make_the_focus_go_from_one_cell_to_the_next_when_hitting_enter" rel="nofollow">here</a> in a similar context).</p> <p>The problem is in achieving 1. I list below the approaches I have tried till now.</p> <ul> <li><strong>Reconfigure the "platform edit key"</strong> By default, "Editing starts when the platform edit key has been pressed over an item." (<a href="http://qt-project.org/doc/qt-4.8/qabstractitemview.html#EditTrigger-enum" rel="nofollow"><code>QAbstractItemView::EditKeyPressed</code></a>) This key is F2 on my platform (Ubuntu 12.04). I could reconfigure the platform edit key to Enter but <ul> <li>Altering platform defaults seems like a bad idea.</li> <li>I could not find out how to do it.</li> </ul></li> <li><p><strong>Capture the Enter key press</strong> I use <a href="http://qt-project.org/doc/qt-4.8/qshortcut.html" rel="nofollow"><code>QShortCut</code></a> to do this as follows:</p> <pre><code>class CourseTable : public QTableView { /* ... */ }; /* ... */ CourseTable::CourseTable(/* ... */) { /* ... */ QShortcut* shortcut = new QShortcut(QKeySequence(Qt::Key_Return), this); connect(shortcut, SIGNAL(activated()), this, SLOT(handleEnter_())); /* ... */ } /* ... */ void CourseTable::handleEnter_() { QModelIndex idx = this-&gt;currentIndex(); if (this-&gt;state() != QAbstractItemView::EditingState) this-&gt;edit(idx); /* else // see below */ } </code></pre> <p>This does capture the Enter key-press and accomplishes 1 (from above) but now 2 is broken. So, I need to look into the <code>else</code> clause in <code>CourseTable::handleEnter_()</code> above, possibly calling <a href="http://qt-project.org/doc/qt-4.8/qabstractitemview.html#commitData" rel="nofollow"><code>QAbstractItemView::commitData()</code></a> and <a href="http://qt-project.org/doc/qt-4.8/qabstractitemview.html#closeEditor" rel="nofollow"><code>QAbstractItemView::closeEditor</code></a> in it. The problem is that both these functions require a <code>QWidget *editor</code> argument which I just cannot figure out how to get. I could subclass <a href="http://qt-project.org/doc/qt-4.8/qabstractitemdelegate.html" rel="nofollow"><code>QAbstractItemDelegate</code></a>, add a <code>getEditor()</code> method to the derived class, and modify existing code to pass instances of the derived delegate class to <code>CourseTable::setItemDelegate*()</code> functions. But that sounds like too much work.</p></li> </ul> <p>So, any ideas how I can cleanly accomplish both 1 and 2 without having to rewrite my code?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload