creator-debugger-example.qdoc 4.63 KB
Newer Older
1 2
/****************************************************************************
**
3 4
** Copyright (C) 2016 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
5
**
6
** This file is part of the Qt Creator documentation.
7
**
8 9 10 11 12 13 14
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
15
**
16
** GNU Free Documentation License Usage
17 18
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
19 20 21 22
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
23 24 25 26 27 28 29 30 31 32
**
****************************************************************************/

// **********************************************************************
// NOTE: the sections are not ordered by their logical order to avoid
// reshuffling the file each time the index order changes (i.e., often).
// Run the fixnavi.pl script to adjust the links to the index order.
// **********************************************************************

/*!
33
    \contentspage {Qt Creator Manual}
34
    \previouspage creator-debugging-qml.html
35
    \page creator-debugging-example.html
36
    \nextpage creator-qml-debugging-example.html
37

38
    \title Debugging a C++ Example Application
39 40

    This section uses the \l{Creating a Qt Widget Based Application}{TextFinder}
Tobias Hunger's avatar
Tobias Hunger committed
41 42
    example to illustrate how to debug Qt C++ applications in the
    \uicontrol Debug mode.
43 44 45 46 47 48 49

    TextFinder reads a text file into QString and then displays it with
    QTextEdit. To look at the TextFinder class and see the stored data, place
    a breakpoint in textfinder.cpp, as follows:

    \list 1

50
        \li Click in between the line number and the window border on the line
51 52 53 54
            where we change the cursor position to set a breakpoint.

            \image qtcreator-setting-breakpoint1.png

Tobias Hunger's avatar
Tobias Hunger committed
55 56
        \li Select \uicontrol Debug > \uicontrol {Start Debugging} >
            \uicontrol {Start Debugging} or press \key F5.
57

58 59
        \li To view information about the breakpoint, go to the
            \uicontrol Breakpoints view.
60 61 62

            \image qtcreator-setting-breakpoint2.png

63
        \li To remove a breakpoint, right-click it and select
Tobias Hunger's avatar
Tobias Hunger committed
64
            \uicontrol {Delete Breakpoint}.
65 66


67
        \li To view the base classes and data members of the TextFinder class,
Tobias Hunger's avatar
Tobias Hunger committed
68
            go to the \uicontrol {Locals and Expressions} view.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

            \image qtcreator-watcher.png

    \endlist

    Modify the \c{on_findButton_clicked()} function to move back to
    the start of the document and continue searching once the cursor hits the
    end of the document. Add the following code snippet:

    \code
    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;
    }
    \endcode

    If you compile and run the above code, however, the application does not
    work correctly due to a logic error. To locate this logic error, step
    through the code using the following buttons:
111
    \inlineimage qtcreator-debug-button-stop.png
112 113 114
    (\uicontrol {Stop Debugger}), \inlineimage debugger_stepover_small.png
    (\uicontrol {Step Over}), \inlineimage debugger_stepinto_small.png
    (\uicontrol {Step Into}), and \inlineimage debugger_stepout_small.png
115
    (\uicontrol {Step Out}).
116 117

*/