Note that there are some explanatory texts on larger screens.

plurals
  1. POQt4 - How to add a row in a QTableWidget when sorting is enable?
    primarykey
    data
    text
    <p>I have a <code>QTableWidget</code> with which I can add rows by pushing a button. The problem is that I have <code>_table-&gt;setSortingEnabled(true);</code>, and when I'll insert a new row, I won't know where it is. Because since I have set the table sortable, when I add a row, this row will move to it's position (depending on which column is sorted). How can I know which one it is then?</p> <p>Example with one column (Name) sorted by alphabetical order:</p> <pre><code>Name | ------ abc toto zozo </code></pre> <p>If I now add a row like follow :</p> <pre><code>int newRow = _table-&gt;rowCount(); _table-&gt;insertRow(newRow); QTableWidgetItem *item1 = new QTableWidgetItem; item1-&gt;setText("boom"); _table-&gt;setItem(newRow, 0, item1); </code></pre> <p>The row will go between 'abc' and 'toto'.</p> <pre><code>Name | ------ abc &lt;&lt;&lt; boom toto zozo </code></pre> <p>Here it'll work properly, but I have multiple columns and if now I try for example:</p> <pre><code>QTableWidgetItem *item2 = new QTableWidgetItem; item1-&gt;setText("45"); _table-&gt;setItem(newRow, 1, item2); </code></pre> <p>The value that'll change is the value of the second column of 'zozo' and not the one I want, 'boom'.</p> <p>I tried to explain with an example the problem, I hope it helped. So how can I add a new row, set it's text for each of its column if I have the sorting enable on my <code>QTableWidget</code> ?</p> <p><strong>Edit : Answer from nonexplosive (in the comments) :</strong></p> <p>Just need to do like follow for the second <code>QWidgetItem</code> :</p> <pre><code>QTableWidgetItem *item2 = new QTableWidgetItem; item1-&gt;setText("45"); _table-&gt;setItem(item1-&gt;row(), 1, item2); // change newRow by item1-&gt;row() </code></pre>
    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.
 

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