<?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[Working with a QListView crashes with no error]]></title><description><![CDATA[<p dir="auto">Hi there!</p>
<p dir="auto">First of all I am in the beginning of learning Qt and C++, so it could be a really dumb thing here. What I want to archive is a simple app that list all text files from a folder by name in QListView and by clicking on a name its content in an editor. But I am already struggling to get all file names in the list view.</p>
<pre><code>void MainWindow::addInstance(QUrl url)
{
    QDir dir(url.path());
    QFileInfoList notes = dir.entryInfoList(QStringList() &lt;&lt; "*.txt", QDir::Files, QDir::Name);

    for(int i=0;i&lt;notes.size();i++){
        QString tmpNote;
        QFileInfo fileInfo = notes.at(i);
        QFile note(fileInfo.filePath());

        if(note.open(QIODevice::ReadWrite)){
            QTextStream in(&amp;note);
            tmpNote = in.readAll();
        }else{
            tmpNote = (tr("Error reading file."));
        }
// Up to here the code works as intended
//        model-&gt;append(fileInfo.baseName(),tmpNote, fileInfo.filePath()); // First approach to have a custom model that stores more than only the file name. Did not work so I tried to start smaller.
        noteList &lt;&lt; fileInfo.baseName(); // noteList is a QStringList
    }
qDebug() &lt;&lt; noteList; // Output QList("Neue Notiz", "Schnelle Notiz", "test1", "test 2")
    mModel-&gt;setStringList(noteList); // mModel is QStringListModel *mModel;
qDebug() &lt;&lt; mModel; // Not output app crashed bevor that. I do not see any hint why it crashes.
    ui-&gt;noteView-&gt;setModel(mModel);

// First approach to connect the list with a function to update the editor. Also crashes with no error.
/*    connect(ui-&gt;noteView-&gt;selectionModel(),
            SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
            this,
            SLOT(updateEditor(QItemSelection)));*/
}
</code></pre>
<p dir="auto">The kit I am using is Qt 6.3.2 for macOS.<br />
Is someone so kind and can tell me what I am doing wring or how I can debug this issue.</p>
<p dir="auto">Thanks a lot in advance!</p>
]]></description><link>https://forum.qt.io/topic/139416/working-with-a-qlistview-crashes-with-no-error</link><generator>RSS for Node</generator><lastBuildDate>Sat, 31 Oct 2026 23:35:00 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/139416.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 19 Sep 2022 09:28:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Working with a QListView crashes with no error on Mon, 19 Sep 2022 10:52:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sikarjan-0">@<bdi>Sikarjan-0</bdi></a> said in <a href="/post/729277">Working with a QListView crashes with no error</a>:</p>
<blockquote>
<p dir="auto">why do I have to put mModel = new QStringListModel(...); prior mModel-&gt;setStringList? Is declaring it in the header not enough?</p>
</blockquote>
<p dir="auto">No, it is not enough!  You should take some time to understand this, you will need to in order to program in C++ successfully.</p>
<p dir="auto">Note that you are declaring a <em>pointer to</em> a <code>QStringListModel</code> when you write <code>QStringListModel *mModel;</code>.  Your class will indeed have room for that <em>pointer</em> allocated, but it does not point to any <code>QStringListModel</code> when an instance of your class is created.  If you are "lucky" (not really!) it is set to <code>nullptr</code>, if you are "unlucky" (much more likely for class members) the pointer has a <em>garbage</em> value.  Either way if you try to do <em>anything</em> with <code>mModel-&gt;</code> --- which <code>mModel-&gt;setStringList()</code> will be doing --- something "bad" will happen!  In the constructor of the class you are likely to want to <em>create</em> the model instance with <code>mModel = new QStringListModel(this)</code>, now you have allocated a model and set <code>mModel</code> to point to it.</p>
<p dir="auto">Note this is very different from having your class member variable declared via <code>QStringListModel mModel;</code>.  This is not a <em>pointer to</em> an instance of <code>QStringListModel</code>, which needs to be allocated, it is an actual instance of a <code>QStringListModel</code>, which is created as your instance is created.  Which of these two you want/prefer depends on your code.</p>
]]></description><link>https://forum.qt.io/post/729280</link><guid isPermaLink="true">https://forum.qt.io/post/729280</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 19 Sep 2022 10:52:14 GMT</pubDate></item><item><title><![CDATA[Reply to Working with a QListView crashes with no error on Mon, 19 Sep 2022 10:52:06 GMT]]></title><description><![CDATA[<p dir="auto">Thanks again and I will</p>
]]></description><link>https://forum.qt.io/post/729279</link><guid isPermaLink="true">https://forum.qt.io/post/729279</guid><dc:creator><![CDATA[Sikarjan 0]]></dc:creator><pubDate>Mon, 19 Sep 2022 10:52:06 GMT</pubDate></item><item><title><![CDATA[Reply to Working with a QListView crashes with no error on Mon, 19 Sep 2022 10:49:43 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sikarjan-0">@<bdi>Sikarjan-0</bdi></a> said in <a href="/post/729277">Working with a QListView crashes with no error</a>:</p>
<blockquote>
<p dir="auto">Is declaring it in the header not enough?</p>
</blockquote>
<p dir="auto">Not if it's a pointer like in your case (C++ basics). A pointer just holds a pointer. If you do not assign it a pointer to an object it simply points to some not allocated memory. Please read about heap and stack allocation.</p>
]]></description><link>https://forum.qt.io/post/729278</link><guid isPermaLink="true">https://forum.qt.io/post/729278</guid><dc:creator><![CDATA[jsulm]]></dc:creator><pubDate>Mon, 19 Sep 2022 10:49:43 GMT</pubDate></item><item><title><![CDATA[Reply to Working with a QListView crashes with no error on Mon, 19 Sep 2022 10:39:04 GMT]]></title><description><![CDATA[<p dir="auto">Thanks that was the solution.</p>
<p dir="auto">I feel stupid for not knowing this but why do I have to put mModel = new QStringListModel(...); prior mModel-&gt;setStringList? Is declaring it in the header not enough?</p>
]]></description><link>https://forum.qt.io/post/729277</link><guid isPermaLink="true">https://forum.qt.io/post/729277</guid><dc:creator><![CDATA[Sikarjan 0]]></dc:creator><pubDate>Mon, 19 Sep 2022 10:39:04 GMT</pubDate></item><item><title><![CDATA[Reply to Working with a QListView crashes with no error on Mon, 19 Sep 2022 10:34:04 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sikarjan-0">@<bdi>Sikarjan-0</bdi></a> said in <a href="/post/729273">Working with a QListView crashes with no error</a>:</p>
<blockquote>
<p dir="auto">This is the first time in the code that mModel is used (besides the declaration in the header file).</p>
</blockquote>
<p dir="auto">Assuming you have <code>QStringListModel *mModel;</code> there, that declares it but does not set it to anything, no model is created!  Where is your <code>mModel = new QStringListModel(...);</code> statement, executed prior to trying to access the model for any reason?!</p>
]]></description><link>https://forum.qt.io/post/729275</link><guid isPermaLink="true">https://forum.qt.io/post/729275</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 19 Sep 2022 10:34:04 GMT</pubDate></item><item><title><![CDATA[Reply to Working with a QListView crashes with no error on Mon, 19 Sep 2022 10:28:25 GMT]]></title><description><![CDATA[<p dir="auto">Thanks for your reply.</p>
<p dir="auto">This is the first time in the code that mModel is used (besides the declaration in the header file).</p>
<p dir="auto">Is this what you are asked for?</p>
<pre><code>1 ___lldb_unnamed_symbol1542$$QtCore                     (x86_64) /Users/xxx/Applications/Qt/6.3.2/macos/lib/QtCore.framework/Versions/A/QtCore    0x10a1aca89 
2 QAbstractItemModel::beginResetModel()                  (x86_64) /Users/xxx/Applications/Qt/6.3.2/macos/lib/QtCore.framework/Versions/A/QtCore    0x10a37c3cb 
3 QStringListModel::setStringList(QList&lt;QString&gt; const&amp;) (x86_64) /Users/xxx/Applications/Qt/6.3.2/macos/lib/QtCore.framework/Versions/A/QtCore    0x10a3c96a9 
4 MainWindow::addInstance(QUrl)                          mainwindow.cpp                                                                         72 0x10000b997 
5 MainWindow::MainWindow(QWidget *)                      mainwindow.cpp                                                                         12 0x10000a8fd 
6 MainWindow::MainWindow(QWidget *)                      mainwindow.cpp                                                                         7  0x10000babd 
7 main                                                   main.cpp                                                                               20 0x10000a262 
8 start                                                  (x86_64) /usr/lib/dyld                                                                    0x20005552e 

</code></pre>
<p dir="auto">1 to 3 a greyed out and an arrow points at 3</p>
<p dir="auto">I am building this in cmake and I am on a M1 Mac. Is the project set-up wrong?</p>
]]></description><link>https://forum.qt.io/post/729273</link><guid isPermaLink="true">https://forum.qt.io/post/729273</guid><dc:creator><![CDATA[Sikarjan 0]]></dc:creator><pubDate>Mon, 19 Sep 2022 10:28:25 GMT</pubDate></item><item><title><![CDATA[Reply to Working with a QListView crashes with no error on Mon, 19 Sep 2022 09:56:46 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sikarjan-0">@<bdi>Sikarjan-0</bdi></a></p>
<ul>
<li>Show that <code>mModel</code> has been set to a (still-currently-valid) <code>QStringListModel</code>?</li>
<li><code>qDebug() &lt;&lt; mModel</code> may not be able to print out the model, though it probably ought not crash.</li>
<li>If your code "crashes", run it under debugger, it will break at the fault.  Find the <em>stack trace</em> pane, look at that to see where it has crashed from, show that here if you cannot figure it.</li>
</ul>
]]></description><link>https://forum.qt.io/post/729268</link><guid isPermaLink="true">https://forum.qt.io/post/729268</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 19 Sep 2022 09:56:46 GMT</pubDate></item></channel></rss>