Note that there are some explanatory texts on larger screens.

plurals
  1. POGrid-like Inline editing with QTableView and QSqlTableModel
    primarykey
    data
    text
    <p>I am trying to make QTableView behave like a grid table, i.e there is always one empty row in bottom where user input new records to be inserted. Think MS Access subform, or in web dev I think we call it inline editing. </p> <p>I am using Qt 4.7.4, pretty standard code:</p> <pre><code>model = new QSqlTableModel(this); model-&gt;setTable("order_details"); models-&gt;setEditStrategy(QSqlTableModel::OnManualSubmit); model-&gt;setFilter(QString("parent_id = '%1'").arg(ID-&gt;toString()); model-&gt;select(); model-&gt;insertRow(model-&gt;rowCount()); //worked womehow //QSqlRecord rec = model-&gt;record(); //rec.setValue("fieldName", var); //useful for initializing some fields //model-&gt;insertRecord(-1, rec); //internally call insertRow(rowCount()) then setRecord ui-&gt;tblGrid-&gt;setModel(model); </code></pre> <p>If insertRow or insertRecord is put in constructor, nothing happens. no row added. But if I create a pushbutton that calls either of them, a new row is added but it is <strong>uneditable</strong>. </p> <p>To observe this try modifying <a href="http://doc.qt.nokia.com/4.7-snapshot/sql-cachedtable.html" rel="nofollow">Qt's cached table example</a>, adding insertRow() will do nothing if put in constructor. In a slot called from a push button, however, then it will add the row just fine. uneditable, of course.</p> <p>So my question is:</p> <ol> <li>Why is this happening? I've had enough look at the QSqlTableModel's source code, but to understand it we also need to understand QSqlRecord, QSqlQuery, QAbstractItemModel etc... can't wrap my mind around it. </li> <li>To get around uneditable row thing I've subclassed QSqlTableModel overriding flags() to return Qt::ItemIsEditable and Qt::ItemIsenabled to no avail. What did I do wrong?</li> <li>I know I will eventually subclass QTableView to get this behaviour, so I will appreciate it if anyone has some pointers for me, perhaps some project's source I can read. </li> </ol> <p>Thank you, </p> <p>Evan </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.
    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