Посібник: Налагодження C++
У цьому посібнику на прикладі програми TextFinder показано, як налагоджувати програми Qt на C++ у режимі « Debug ».
TextFinder зчитує текстовий файл у режим « QString », а потім відображає його за допомогою функції « QTextEdit ». Щоб переглянути клас TextFinder та збережені дані:
- У «
textfinder.cpp» клацніть між номером рядка та межею вікна на тому рядку, де ми змінюємо положення курсора, щоб встановити точку зупинки.
- Перейдіть до « Debug » > « Start Debugging » > « Start Debugging of Startup Project » або натисніть клавішу F5.
- Щоб переглянути інформацію про точку зупинки, перейдіть до вікна « Breakpoints ».

- Щоб видалити точку зупинки, клацніть її правою кнопкою миші та виберіть « Delete Breakpoint ».
- Щоб переглянути базові класи та члени даних класу TextFinder, перейдіть до вікна « Locals ».

Змініть функцію « on_findButton_clicked() » так, щоб курсор повертався до початку документа та продовжував пошук, коли досягає кінця документа. Додайте такий фрагмент коду:
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; }
Однак якщо скомпілювати та запустити наведений вище код, програма не працюватиме коректно через логічну помилку. Щоб знайти цю логічну помилку, виконайте покроковий перегляд коду за допомогою таких кнопок: «
» (Stop Debugger), «
» (Step Over), «
» (Step Into) та «
» (Step Out).
Див. також Посібник: Додаток « Qt Widgets », Налагодження та Налагоджувачі.
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.