Get Involved/development/IDE configuration/Qt Creator: Difference between revisions

From KDE Community Wiki
m (Add screen recording)
(Two chapters)
Line 3: Line 3:
A screen recording version is available https://www.youtube.com/watch?v=ASnDeEaXnbI
A screen recording version is available https://www.youtube.com/watch?v=ASnDeEaXnbI


==Qt Creator and kdesrc-build==
==kdesrc-build==


After you configure kdesrc-build and you can correctly build a KDE projects such as kcalc.
After you configure kdesrc-build and you can correctly build a KDE project such as kcalc.


Edit ~/.config/kdesrc-buildrc . It should look like:
Edit ~/.config/kdesrc-buildrc . It should look like:
Line 21: Line 21:
...                                                                                 
...                                                                                 
     cmake-options -DCMAKE_BUILD_TYPE=Debug
     cmake-options -DCMAKE_BUILD_TYPE=Debug
...
    stop-on-failure false
...
    directory-layout invent
</nowiki>}}
</nowiki>}}


Line 32: Line 36:
</nowiki>}}
</nowiki>}}


E.g. install Qt Creator from your current Linux distro's packages. E.g.
==Qt Creator==
 
Install Qt Creator. E.g. download from Qt the latest version. Or e.g. from your current Linux distribution's packages. E.g.
{{Input|1=<nowiki>
{{Input|1=<nowiki>
apt install qtcreator
apt install qtcreator
Line 44: Line 50:
From the qtcreator main menu
From the qtcreator main menu


> File > Close all projects and editors
> File > Close All Projects and Editors
 
> File > Open file or project <code>~/kde/src/kcalc/CMakeLists.txt</code>
 
Left Sidebar > Edit sidebar > Select "File System" from the combobox. In the "File System" left hand treeview > double click on the file kcalc.cpp. Click inside the source code in text editor.


In the text editor's top bar the text in the combobox changes from "<No Symbols>" to "<Select Symbol>". From this combobox (i.e. the Symbols combobox), select the last item: <code>main(int, char***) -> int</code>.
> File > Open File or Project <code>~/kde/src/utilities/kcalc/CMakeLists.txt</code>


In the left bottom side of qtcreator you can see the "Project Deploy Kit Build Run" selector. It says "kcalc (Desktop kit icon) Debug".
Left Sidebar > Switch to Projects mode Ctrl+5 > Manage Kits... Under Manual, keep only "Desktop (default)". E.g. select Manual > Qt 5.15.4 (qt5) > Remove button > OK.


At this moment the following work correctly: I can edit the source code correctly. I can run the executable that I want via qtcreator main menu > Debug > Start debugging > Start and debug external application <code>~/kde/build/kcalc/bin/kcalc</code>. I.e. I can program inside the debugger while debugging. I can navigate the source code, i.e. go to *.h/*.cpp/Ctrl + click on symbol/right click on symbol > Follow symbol under cursor/F2. I can only navigate towards the *.h files that exist in /usr and to the source code in the project kcalc (i.e. the source code in ~/kde/src/kcalc/).
Button "Import Existing Build..." > Directory: ~/kde/build/utilities/kcalc > Choose.


What I cannot do is, I cannot navigate to files from ~/kde/src/. E.g. If in kcalc.cpp, after <code>int main(int argc, char *argv[])</code>, I want to navigate the symbol "KLocalizedString" it refuses to navigate and says "use of undeclared identifier 'KLocalizedString'".
Everything should be configured correctly.


When I navigate the symbol "KCrash", it navigates to the file "/usr/include/KF5/KCrash/kcrash.h:35". Which is wrong. As I use all non KDE libraries from Kubuntu 22.04, I use qt from Kubuntu 22.04. But I use all of the KDE source code from kdesrc-build, i.e. from ~/kde/src/.
Left Sidebar > Switch to Projects mode Ctrl+5 > Active project: kcalc. Build & Run: not the Desktop kit. Build Settings > build configuration: "Debug2".


I click on the left button "Projects". The GUI editor for CMake is shown. In Desktop > Build > "Key Value" list view I can see "KF5Auth_DIR /usr/lib/x86_64-linux-gnu/cmake/KF5Auth". That means that qtcreator thinks that I use /usr for KDE source code and includes. I use ~/kde/src/.
Click on the left sidebar, the button above "Run Ctrl+R". It should show: Project: kcalc. Kit: not the Desktop kit. Deploy: Deploy Configuration. Build: Debug2. Run: kcalc.


Tools > Options > Kits > remove kit "Imported Kit".
Left Sidebar > Switch to Edit mode (Ctrl+2) > Select "Projects" from the combobox. In the "Projects" left hand treeview > kcalc > kcalc > Source Files > double click on the file kcalc.cpp. The file kcalc.cpp will be shown in the editor view. Click inside the source code in text editor.


I click on the left button "Projects". The GUI editor for CMake is shown. I select "Imported Kit" > Build. Delete the two build configurations "Debug" and "Debug2".
In the text editor's top bar > function/identifier/method/type/symbols combobox, select the last item: <code>main(int, char***) -> int</code>. Click on the line with the opening curly bracket of the function main. From the main menu > Debug > Set or Remove Breakpoint (F9).
I press the button "Import Existing Build..." > "~/kde/build/kcalc". In Imported Kit > Build it creates a build configuration named "Debug". In Desktop > Build > "Key Value" list view I can see "KF5Auth_DIR /home/nmariusp/kde/usr/lib/x86_64-linux-gnu/cmake/KF5Auth". That means that qtcreator uses ~/kde/src/ for KDE source code and includes.
In the left bottom side of qtcreator is the "Project Deploy Kit Build Run" selector. Click on it. It says "kcalc + Deploy configuration + Kit = Imported Kit + Build = Debug + Run = kcalc".


Now, I open the file "kcalc.cpp", navigate to <code>int main(int argc, char *argv[])</code> and I can see that qtcreator recognizes the symbols "QApplication (/usr/include/x86_64-linux-gnu/qt5/QtWidgets/qapplication.h:72)", "KLocalizedString (~/kde/usr/include/KF5/KI18n/klocalizedstring.h:98)", "KCrash (~/kde/usr/include/KF5/KCrash/kcrash.h:35)", "KNumber (~/kde/src/kcalc/knumber/knumber.h:17)". In order to copy file path and line number, right click on the tab (i.e. document combobox) > Copy path and line number. Now I can correctly navigate to all symbols.
Left Sidebar > Start debugging of startup project. The debugger will start correctly and break on the source code line with the curly bracket.


If I edit the source code of
From the main menu Debug > Step Over (F10)/Step Into(F11)/Step Out (Shift+F11).
{{Input|1=<nowiki>
int main(int argc, char *argv[])
{
    int a = argc + 1;
</nowiki>}}
 
and place a breakpoint on this new line. I click on the left bottom button "Start debugging of startup project" (the green triangle + ladybug icon). It builds. And starts the correct executable in the debugger and breaks on line
<code>int a = argc + 1;</code>
 
And I can step in the debugger. And I can see how the value of the local variable a changes from 0 to 2. Now I can Build project kcalc (Ctrl+B).  
 
The button "Run Ctrl+R" also works correctly.
 
{{Input|1=<nowiki>
ps aux | grep kcalc
usern  187843  0.8  0.3 2162696 105592 pts/4  Sl  20:26  0:00 ~/kde/build/kcalc/bin/kcalc
</nowiki>}}

Revision as of 19:37, 31 July 2022

Qt Creator is an IDE from Qt.

A screen recording version is available https://www.youtube.com/watch?v=ASnDeEaXnbI

kdesrc-build

After you configure kdesrc-build and you can correctly build a KDE project such as kcalc.

Edit ~/.config/kdesrc-buildrc . It should look like:

global
...                                                                                                    
    include-dependencies true
...                                                                                                               
    kdedir ~/kde/usr
...                                                                                                              
    source-dir ~/kde/src
...                                                                                                                
    build-dir ~/kde/build
...                                                                                
    cmake-options -DCMAKE_BUILD_TYPE=Debug
...
    stop-on-failure false
...
    directory-layout invent

The line above is important in order to use the debugger from Qt Creator.

Make sure you have the correct number on the line num-cores .

Build kcalc, make sure there are no errors:

kdesrc-build --include-dependencies kcalc

Qt Creator

Install Qt Creator. E.g. download from Qt the latest version. Or e.g. from your current Linux distribution's packages. E.g.

apt install qtcreator

Start Qt Creator. E.g. from a terminal

qtcreator &

From the qtcreator main menu

> File > Close All Projects and Editors

> File > Open File or Project ~/kde/src/utilities/kcalc/CMakeLists.txt

Left Sidebar > Switch to Projects mode Ctrl+5 > Manage Kits... Under Manual, keep only "Desktop (default)". E.g. select Manual > Qt 5.15.4 (qt5) > Remove button > OK.

Button "Import Existing Build..." > Directory: ~/kde/build/utilities/kcalc > Choose.

Everything should be configured correctly.

Left Sidebar > Switch to Projects mode Ctrl+5 > Active project: kcalc. Build & Run: not the Desktop kit. Build Settings > build configuration: "Debug2".

Click on the left sidebar, the button above "Run Ctrl+R". It should show: Project: kcalc. Kit: not the Desktop kit. Deploy: Deploy Configuration. Build: Debug2. Run: kcalc.

Left Sidebar > Switch to Edit mode (Ctrl+2) > Select "Projects" from the combobox. In the "Projects" left hand treeview > kcalc > kcalc > Source Files > double click on the file kcalc.cpp. The file kcalc.cpp will be shown in the editor view. Click inside the source code in text editor.

In the text editor's top bar > function/identifier/method/type/symbols combobox, select the last item: main(int, char***) -> int. Click on the line with the opening curly bracket of the function main. From the main menu > Debug > Set or Remove Breakpoint (F9).

Left Sidebar > Start debugging of startup project. The debugger will start correctly and break on the source code line with the curly bracket.

From the main menu Debug > Step Over (F10)/Step Into(F11)/Step Out (Shift+F11).