Návod: Ladění v jazyce C++

Tento návod používá příklad TextFinder k ilustraci toho, jak ladit aplikace Qt v jazyce C++ v režimu „ Debug “.

TextFinder načte textový soubor do režimu „ QString “ a poté jej zobrazí pomocí funkce ` QTextEdit`. Chcete-li si prohlédnout třídu TextFinder a uložená data:

  1. V nástroji „ textfinder.cpp “ klikněte mezi číslem řádku a okrajem okna na řádku, kde změníte polohu kurzoru, abyste nastavili zarážku.

    Nastavení zarážky v editoru

  2. Přejděte do Debug > Start Debugging > Start Debugging of Startup Project nebo stiskněte klávesu F5.
  3. Chcete-li zobrazit informace o zarážce, přejděte do zobrazení „ Breakpoints “.

    Zobrazení podrobností o zarážkách v okně „Zarážky“

  4. Chcete-li zarážku odstranit, klikněte na ni pravým tlačítkem myši a vyberte možnost „ Delete Breakpoint “.
  5. Chcete-li zobrazit základní třídy a datové členy třídy TextFinder, přejděte do zobrazení „ Locals “.

    Zobrazení základních tříd a datových členů třídy

Upravte funkci „ on_findButton_clicked() “ tak, aby se kurzor po dosažení konce dokumentu vrátil na začátek dokumentu a pokračoval v hledání. Přidejte následující úryvek kódu:

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;
}

Pokud však výše uvedený kód zkompilujete a spustíte, aplikace nebude kvůli logické chybě fungovat správně. Chcete-li tuto logickou chybu najít, procházejte kód pomocí následujících tlačítek: „ Zastavit ladicí program “ (Stop Debugger), „ Překroč “ (Step Over), „ Vstupte do “ (Step Into) a „ Vyrazte ven “ (Step Out).

Viz také Výukový program: Aplikace „ Qt Widgets “, Ladění a ladicí programy.

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.