QModelIndex selection causes application crash

  • Hello everybody,

    I've a strange crashe with QModelIndex. In fact all comments are written into log file but application crashes.

    frmPatient *w = new frmPatient(this,0);
    		int patient = w->currentPatient();
    		WriteIntoLogFile(QString("création du patient n° %1").arg(patient));
    		if(patient != 0)
    			WriteIntoLogFile("raffraichissmeent liste patient");
    			WriteIntoLogFile(QString("sélection du patient n° %1 dans la liste").arg(QString::number(patient)));
    			QAbstractItemModel* model = tabPatient->model();  
    			if (model == 0)
    				WriteIntoLogFile("echec sélection du modèle depuis la liste des patients");
    			QModelIndex start(model->index(0,0,QModelIndex()));
    			if (!start.isValid())
    				WriteIntoLogFile("echec sélection modèle index dans la liste");
    			QModelIndexList list = model->match(start, Qt::DisplayRole, patient);
    			if (list.isEmpty())
    				WriteIntoLogFile(QString("sélection du patient n° %1 dans la liste").arg(QString::number(patient)));
    				WriteIntoLogFile("sélection de l'élement dans la liste");
    				WriteIntoLogFile("CurrentIndex sélectionné");
    				tabPatient->scrollTo(model->index(list[0].row(), 1));	
    				WriteIntoLogFile("scrollTo l'élement"); // this comment is written into log file and application crashes
    				WriteIntoLogFile("l'élement à selectionner est vide");
    	WriteIntoLogFile("suppression objet");
    	delete w;

    I'm on Qt4.8 and the crashes happens only Windows 10 x64.

    Can anyone tell me what's wrong ?

    Many thanks in advance.

  • Qt Champions 2017

    if you single step, what line does it crash in?

    One note though
    you create (dialog`) frmPatient(this,0); with parent as "this"
    but you delete it later yourself. So parent might also try to delete it
    on closing.

  • Lifetime Qt Champion


    To add to @mrjj, you are testing if model is equal zero, if so you write some log and then go on using it, that will surely crash. You should stop there.

    By the way, there's no need for frmPatnient to be allocated on the heap since you destroy it at the end anyway. Just allocate it on the stack.

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.