Solved QCompleter & QTreeview get crash on MacOS and Qt 5.12.2
-
Greetings to all,
For a QLineEdit I tried to create an auto-completer from a list requested on an API server. I'm able to have the correct behavior but some time when the dialog window is opening the whole application crash. If I remove the whole code to create the QCompleter I do not have this crash.
It seems I did something not correctly but as I'm new with Qt I cannot figure out what is wrong and why sometime the dialog works as expected and sometime the app just crash.Here is how I create and bind my QCompleter to QLineEdit, maybe you will see something wrong...
void CreateAppointmentDialog::patientsFetched(QJsonArray patients) { // Create an auto completer QCompleter *completer = new QCompleter(this); completer->setCompletionMode(QCompleter::PopupCompletion); completer->setCaseSensitivity(Qt::CaseInsensitive); completer->setMaxVisibleItems(10); // No effect ? completer->setFilterMode(Qt::MatchContains); // Create a new model to list patients QStandardItemModel *patientsModel = new QStandardItemModel(patients.count(), 3, completer); int counter = 0; foreach (const QJsonValue &value, patients) { QJsonObject patientObj = value.toObject(); QModelIndex patientNameIdx = patientsModel->index(counter, 0); QModelIndex patientBirthdateIdx = patientsModel->index(counter, 1); QModelIndex patientIdIdx = patientsModel->index(counter, 2); QString patientName = patientObj["lastname"].toString() + " " + patientObj["firstname"].toString(); QString patientBirthdate = patientObj["birthdate"].toString() != "" ? QDate::fromString(patientObj["birthdate"].toString(), Qt::ISODate).toString("dd.MM.yyyy") : ""; QString patientId = patientObj["id"].toString(); patientsModel->setData(patientNameIdx, patientName); patientsModel->setData(patientBirthdateIdx, patientBirthdate); patientsModel->setData(patientIdIdx, patientId); counter++; } completer->setModel(patientsModel); QTreeView *treeView = new QTreeView; completer->setPopup(treeView); treeView->setRootIsDecorated(false); treeView->hideColumn(2); treeView->header()->hide(); treeView->header()->setStretchLastSection(false); treeView->header()->setSectionResizeMode(0, QHeaderView::Stretch); treeView->header()->setSectionResizeMode(1, QHeaderView::ResizeToContents); ui->patientsEdit->setCompleter(completer); connect(treeView, &QTreeView::clicked, [=]{ selectedPatientId = treeView->currentIndex().sibling(treeView->currentIndex().row(),2).data().toString(); }); }
Any help will be really appreciate.
Regards,
-
After investigating it seems that QCompleter is not my problem.
Then I think I need to learn how to debug my app to understand from where this crash is coming. -
Hi
The first start is just to start the app in debug mode (f5) and get it to crash.
That will show a list of called methods until it crashed.
That often gives a good clue to what is happening. -
Thank's for your advise!
I finally figured out what was wrong and the problem was coming from the customContextMenu where I specified a bad table to popup the menu from QModelIndex in the parent where I called the dialog CreateAppointmentDialog.Sorry I was sure that it was first coming from the combo of QCompleter & QTreeview.