Working with a QListView crashes with no error
-
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(¬e); 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!
-
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(¬e); 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!
- Show that
mModelhas been set to a (still-currently-valid)QStringListModel? qDebug() << mModelmay 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.
- Show that
-
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 0x20005552e1 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?
-
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 0x20005552e1 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?
@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 yourmModel = new QStringListModel(...);statement, executed prior to trying to access the model for any reason?! -
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?
-
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?
@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.
-
Thanks again and I will
-
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?
@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
QStringListModelwhen you writeQStringListModel *mModel;. Your class will indeed have room for that pointer allocated, but it does not point to anyQStringListModelwhen an instance of your class is created. If you are "lucky" (not really!) it is set tonullptr, if you are "unlucky" (much more likely for class members) the pointer has a garbage value. Either way if you try to do anything withmModel->--- whichmModel->setStringList()will be doing --- something "bad" will happen! In the constructor of the class you are likely to want to create the model instance withmModel = new QStringListModel(this), now you have allocated a model and setmModelto 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 ofQStringListModel, which needs to be allocated, it is an actual instance of aQStringListModel, which is created as your instance is created. Which of these two you want/prefer depends on your code.