creator-debugger-example.qdoc 3.93 KB
Newer Older
1
2
/****************************************************************************
**
3
** Copyright (c) 2014 Digia Plc and/or its subsidiary(-ies).
hjk's avatar
hjk committed
4
** Contact: http://www.qt-project.org/legal
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
**
**
** GNU Free Documentation License
**
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of this
** file.
**
**
****************************************************************************/

// **********************************************************************
// 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.
// **********************************************************************

/*!
26
    \contentspage {Qt Creator Manual}
27
    \previouspage creator-debugging-qml.html
28
    \page creator-debugging-example.html
29
    \nextpage creator-qml-debugging-example.html
30

31
    \title Debugging a C++ Example Application
32
33
34
35
36
37
38
39
40
41
42

    This section uses the \l{Creating a Qt Widget Based Application}{TextFinder}
    example to illustrate how to debug Qt C++ applications in the \gui Debug
    mode.

    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

43
        \li Click in between the line number and the window border on the line
44
45
46
47
            where we change the cursor position to set a breakpoint.

            \image qtcreator-setting-breakpoint1.png

48
        \li Select \gui{Debug > Start Debugging > Start Debugging} or press
49
50
            \key{F5}.

51
        \li To view the breakpoint, click the \gui{Breakpoints} tab.
52
53
54

            \image qtcreator-setting-breakpoint2.png

55
        \li To remove a breakpoint, right-click it and select
56
57
58
            \gui{Delete Breakpoint}.


59
        \li To view the base classes and data members of the TextFinder class,
60
61
62
63
64
65
66
67
68
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
            go to the \gui{Locals and Expressions} view.

            \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:
103
104
105
106
107
108
109
110
    \inlineimage qtcreator-debug-button-stop.png
    (\gui Stop),
    \inlineimage qtcreator-debug-button-step-over.png
    (\gui {Step Over}),
    \inlineimage qtcreator-debug-button-step-into.png
    (\gui {Step Into}), and
    \inlineimage qtcreator-debug-button-step-out.png
    (\gui {Step Out}).
111
112

*/