Tutorial: Debug in C++
Questo tutorial utilizza l'esempio TextFinder per illustrare come eseguire il debug delle applicazioni Qt C++ in modalità Debug.
TextFinder legge un file di testo in modalità " QString " e lo visualizza con " QTextEdit". Per esaminare la classe TextFinder e visualizzare i dati memorizzati:
- In
textfinder.cpp, fare clic tra il numero di riga e il bordo della finestra sulla riga in cui si desidera modificare la posizione del cursore per impostare un punto di interruzione.
- Vai su Debug > Start Debugging > Start Debugging of Startup Project oppure premi F5.
- Per visualizzare le informazioni relative al punto di interruzione, passare alla vista " Breakpoints ".

- Per rimuovere un punto di interruzione, fare clic con il pulsante destro del mouse su di esso e selezionare " Delete Breakpoint".
- Per visualizzare le classi base e i membri dati della classe TextFinder, passare alla vista " Locals ".

Modifica la funzione " on_findButton_clicked() " in modo che, una volta che il cursore raggiunge la fine del documento, torni all'inizio del documento e continui la ricerca. Aggiungi il seguente frammento di codice:
void TextFinder::on_findButton_clicked() { QString searchString = ui->lineEdit->text(); QTextDocument *document = ui->textEdit->document(); QTextCursor cursor = ui->textEdit->textCursor(); cursor = document->find(searchString, cursor, QTextDocument::FindWholeWords); ui->textEdit->setTextCursor(cursor); bool found = cursor.isNull(); if (!found && previouslyFound) { int ret = QMessageBox::question(this, tr("End of Document"), tr("I have reached the end of the document. Would you like " "me to start searching from the beginning of the document?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); if (ret == QMessageBox::Yes) { cursor = document->find(searchString, QTextDocument::FindWholeWords); ui->textEdit->setTextCursor(cursor); } else return; } previouslyFound = found; }
Se si compila ed esegue il codice sopra riportato, tuttavia, l’applicazione non funziona correttamente a causa di un errore logico. Per individuare questo errore logico, scorrere il codice utilizzando i seguenti pulsanti:
(Stop Debugger),
(Step Over),
(Step Into) e
(Step Out).
Vedi anche Tutorial: Applicazione " Qt Widgets ", Debugging e Debugger.
Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.