<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to get the selection model from QSqlTableModel]]></title><description><![CDATA[<p dir="auto">Does the QSqlTableModel know which row is selected by a QTableView. I have something like this:</p>
<pre><code>QSqlTableModel *m_accountModel = new QSqlTableModel(this);
model.setTable("account");
model.select();
...
m_ui-&gt;tableViewAccount-&gt;setModel(m_accountModel);
m_ui-&gt;tableViewAccount-&gt;resizeColumnsToContents();
m_ui-&gt;tableViewAccount-&gt;horizontalHeader()-&gt;setStretchLastSection(true);
m_ui-&gt;tableViewAccount-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);
m_ui-&gt;tableViewAccount-&gt;setSelectionMode(QAbstractItemView::SingleSelection);
m_ui-&gt;tableViewAccount-&gt;setEditTriggers(QAbstractItemView::NoEditTriggers)
</code></pre>
<p dir="auto">Now I would like to get the selection of m_ui-&gt;tableViewAccount out of the m_accountModel. At the moment I'm doing it like this:</p>
<pre><code>void FormAccount::on_selectionChanged(const QItemSelection &amp;selected, const QItemSelection &amp;deselected)
{
    Q_UNUSED(deselected)

    if (m_ui-&gt;tableViewAccount-&gt;selectionModel()-&gt;hasSelection()) {
        qDebug() &lt;&lt; "selection:" &lt;&lt; selected;
    }
</code></pre>
<p dir="auto">Is it possible to get the selection directly from the model without using FormAccount::on_selectionChanged()? Or is there an easier and cleaner way to achive this.</p>
]]></description><link>https://forum.qt.io/topic/112050/how-to-get-the-selection-model-from-qsqltablemodel</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Apr 2026 20:32:02 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/112050.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 27 Feb 2020 14:28:25 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to get the selection model from QSqlTableModel on Thu, 27 Feb 2020 15:06:42 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/vronin">@<bdi>VRonin</bdi></a> That makes perfect sense. :-) Thank you very much.</p>
]]></description><link>https://forum.qt.io/post/580042</link><guid isPermaLink="true">https://forum.qt.io/post/580042</guid><dc:creator><![CDATA[Infinity]]></dc:creator><pubDate>Thu, 27 Feb 2020 15:06:42 GMT</pubDate></item><item><title><![CDATA[Reply to How to get the selection model from QSqlTableModel on Thu, 27 Feb 2020 14:51:00 GMT]]></title><description><![CDATA[<p dir="auto">The model doesn't know the selection (it's always useful to remember that 1 model can be attached to multiple views and they can have different selections so it can't be a property of the model).<br />
You can get it from the view using <code>m_ui-&gt;tableViewAccount-&gt;selectionModel()-&gt;selectedIndexes()</code> without using the signal however if that's what you want</p>
]]></description><link>https://forum.qt.io/post/580041</link><guid isPermaLink="true">https://forum.qt.io/post/580041</guid><dc:creator><![CDATA[VRonin]]></dc:creator><pubDate>Thu, 27 Feb 2020 14:51:00 GMT</pubDate></item></channel></rss>