Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Working with a QListView crashes with no error
Qt 6.12 LTS is out! See what's new in the release blog

Working with a QListView crashes with no error

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sikarjan 0
    wrote on last edited by
    #1

    Hi there!

    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.

    void MainWindow::addInstance(QUrl url)
    {
        QDir dir(url.path());
        QFileInfoList notes = dir.entryInfoList(QStringList() << "*.txt", QDir::Files, QDir::Name);
    
        for(int i=0;i<notes.size();i++){
            QString tmpNote;
            QFileInfo fileInfo = notes.at(i);
            QFile note(fileInfo.filePath());
    
            if(note.open(QIODevice::ReadWrite)){
                QTextStream in(&note);
                tmpNote = in.readAll();
            }else{
                tmpNote = (tr("Error reading file."));
            }
    // Up to here the code works as intended
    //        model->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 << fileInfo.baseName(); // noteList is a QStringList
        }
    qDebug() << noteList; // Output QList("Neue Notiz", "Schnelle Notiz", "test1", "test 2")
        mModel->setStringList(noteList); // mModel is QStringListModel *mModel;
    qDebug() << mModel; // Not output app crashed bevor that. I do not see any hint why it crashes.
        ui->noteView->setModel(mModel);
    
    // First approach to connect the list with a function to update the editor. Also crashes with no error.
    /*    connect(ui->noteView->selectionModel(),
                SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
                this,
                SLOT(updateEditor(QItemSelection)));*/
    }
    

    The kit I am using is Qt 6.3.2 for macOS.
    Is someone so kind and can tell me what I am doing wring or how I can debug this issue.

    Thanks a lot in advance!

    JonBJ 1 Reply Last reply
    0
    • S Sikarjan 0

      Hi there!

      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.

      void MainWindow::addInstance(QUrl url)
      {
          QDir dir(url.path());
          QFileInfoList notes = dir.entryInfoList(QStringList() << "*.txt", QDir::Files, QDir::Name);
      
          for(int i=0;i<notes.size();i++){
              QString tmpNote;
              QFileInfo fileInfo = notes.at(i);
              QFile note(fileInfo.filePath());
      
              if(note.open(QIODevice::ReadWrite)){
                  QTextStream in(&note);
                  tmpNote = in.readAll();
              }else{
                  tmpNote = (tr("Error reading file."));
              }
      // Up to here the code works as intended
      //        model->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 << fileInfo.baseName(); // noteList is a QStringList
          }
      qDebug() << noteList; // Output QList("Neue Notiz", "Schnelle Notiz", "test1", "test 2")
          mModel->setStringList(noteList); // mModel is QStringListModel *mModel;
      qDebug() << mModel; // Not output app crashed bevor that. I do not see any hint why it crashes.
          ui->noteView->setModel(mModel);
      
      // First approach to connect the list with a function to update the editor. Also crashes with no error.
      /*    connect(ui->noteView->selectionModel(),
                  SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
                  this,
                  SLOT(updateEditor(QItemSelection)));*/
      }
      

      The kit I am using is Qt 6.3.2 for macOS.
      Is someone so kind and can tell me what I am doing wring or how I can debug this issue.

      Thanks a lot in advance!

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Sikarjan-0

      • Show that mModel has been set to a (still-currently-valid) QStringListModel?
      • qDebug() << mModel may not be able to print out the model, though it probably ought not crash.
      • If your code "crashes", run it under debugger, it will break at the fault. Find the stack trace pane, look at that to see where it has crashed from, show that here if you cannot figure it.
      1 Reply Last reply
      0
      • S Offline
        S Offline
        Sikarjan 0
        wrote on last edited by Sikarjan 0
        #3

        Thanks for your reply.

        This is the first time in the code that mModel is used (besides the declaration in the header file).

        Is this what you are asked for?

        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<QString> const&) (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 
        
        

        1 to 3 a greyed out and an arrow points at 3

        I am building this in cmake and I am on a M1 Mac. Is the project set-up wrong?

        JonBJ 1 Reply Last reply
        0
        • S Sikarjan 0

          Thanks for your reply.

          This is the first time in the code that mModel is used (besides the declaration in the header file).

          Is this what you are asked for?

          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<QString> const&) (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 
          
          

          1 to 3 a greyed out and an arrow points at 3

          I am building this in cmake and I am on a M1 Mac. Is the project set-up wrong?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @Sikarjan-0 said in Working with a QListView crashes with no error:

          This is the first time in the code that mModel is used (besides the declaration in the header file).

          Assuming you have QStringListModel *mModel; there, that declares it but does not set it to anything, no model is created! Where is your mModel = new QStringListModel(...); statement, executed prior to trying to access the model for any reason?!

          1 Reply Last reply
          2
          • S Offline
            S Offline
            Sikarjan 0
            wrote on last edited by
            #5

            Thanks that was the solution.

            I feel stupid for not knowing this but why do I have to put mModel = new QStringListModel(...); prior mModel->setStringList? Is declaring it in the header not enough?

            jsulmJ JonBJ 2 Replies Last reply
            0
            • S Sikarjan 0

              Thanks that was the solution.

              I feel stupid for not knowing this but why do I have to put mModel = new QStringListModel(...); prior mModel->setStringList? Is declaring it in the header not enough?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Sikarjan-0 said in Working with a QListView crashes with no error:

              Is declaring it in the header not enough?

              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.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • S Offline
                S Offline
                Sikarjan 0
                wrote on last edited by
                #7

                Thanks again and I will

                1 Reply Last reply
                0
                • S Sikarjan 0

                  Thanks that was the solution.

                  I feel stupid for not knowing this but why do I have to put mModel = new QStringListModel(...); prior mModel->setStringList? Is declaring it in the header not enough?

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by
                  #8

                  @Sikarjan-0 said in Working with a QListView crashes with no error:

                  why do I have to put mModel = new QStringListModel(...); prior mModel->setStringList? Is declaring it in the header not enough?

                  No, it is not enough! You should take some time to understand this, you will need to in order to program in C++ successfully.

                  Note that you are declaring a pointer to a QStringListModel when you write QStringListModel *mModel;. Your class will indeed have room for that pointer allocated, but it does not point to any QStringListModel when an instance of your class is created. If you are "lucky" (not really!) it is set to nullptr, if you are "unlucky" (much more likely for class members) the pointer has a garbage value. Either way if you try to do anything with mModel-> --- which mModel->setStringList() will be doing --- something "bad" will happen! In the constructor of the class you are likely to want to create the model instance with mModel = new QStringListModel(this), now you have allocated a model and set mModel to point to it.

                  Note this is very different from having your class member variable declared via QStringListModel mModel;. This is not a pointer to an instance of QStringListModel, which needs to be allocated, it is an actual instance of a QStringListModel, which is created as your instance is created. Which of these two you want/prefer depends on your code.

                  1 Reply Last reply
                  2

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt
                  • Unsolved