User:Nmariusp/Qt Creator: Difference between revisions

From KDE Community Wiki
(Create page)
 
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 2: Line 2:


A screen recording version is available https://www.youtube.com/watch?v=ASnDeEaXnbI https://www.youtube.com/watch?v=QVgInye6HDA
A screen recording version is available https://www.youtube.com/watch?v=ASnDeEaXnbI https://www.youtube.com/watch?v=QVgInye6HDA
See also https://community.kde.org/Get_Involved/development/IDE_configuration/Qt_Creator


== Features ==
== Features ==
Line 15: Line 17:


For best results, download Qt Creator from the Qt website. https://www.youtube.com/watch?v=QVgInye6HDA
For best results, download Qt Creator from the Qt website. https://www.youtube.com/watch?v=QVgInye6HDA
==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:
<syntaxhighlight lang="ini">
...                                                                                                   
    include-dependencies true
...                                                                                                             
    kdedir ~/kde/usr
...                                                                                                             
    source-dir ~/kde/src
...                                                                                                               
    build-dir ~/kde/build
...                                                                               
    cmake-options -DCMAKE_BUILD_TYPE=Debug
...
</syntaxhighlight>
The line above is important in order to use the debugger from Qt Creator.
Make sure you have the correct number on the line <code>num-cores </code>.
Build kcalc, make sure there are no errors:
{{Input|1=<nowiki>
kdesrc-build kcalc
</nowiki>}}


==Install Qt Creator==
==Install Qt Creator==
Line 78: Line 51:
From the Qt Creator main menu > View > Workspaces > Views-All. Close all of the tool windows (views) that you do not need, e.g. "3D", "States", "Timeline", "Curves", "Transitions".
From the Qt Creator main menu > View > Workspaces > Views-All. Close all of the tool windows (views) that you do not need, e.g. "3D", "States", "Timeline", "Curves", "Transitions".


==Load a KDE Git repository in Qt Creator==
==Open a KDE project in Qt Creator >= 15==


We'll use kcalc as an example KDE Git repository. First, make sure it was built correctly using <code>kdesrc-build</code>.
Please make sure that you use Qt Creator version 15 or newer.
 
I configure kdesrc-build to use the CMake build configuration "Debug" in order to be able to use the debugger in the IDE.
 
<pre>
cmake-options -DCMAKE_BUILD_TYPE=Debug
...
num-cores 14
</pre>
 
We'll use kcalc as an example KDE module and git repository. First, make sure it was built correctly using <code>kdesrc-build</code>.


<pre>
<pre>
Line 88: Line 71:
Start Qt Creator (for example, from the KDE Application Launcher).
Start Qt Creator (for example, from the KDE Application Launcher).


=== Import the project and the build ===
From the Qt Creator main menu, select File > "Open File or Project" > <code>~/kde/build/kcalc/CMakeCache.txt</code>. Click on the button "Configure Project".  
From the Qt Creator main menu, select '''File''' > '''Open File or Project''' > choose <code>~/kde/src/kcalc/CMakeLists.txt</code> to open the <code>kcalc</code> project.


In the left sidebar, switch to '''Projects''' mode (Ctrl+5), then click '''Manage Kits...'''
=== Run ===


[[File:Qt creator manage kits.png|border|300px|Manage Kits]]
<pre>
 
cat ~/kde/build/kcalc/prefix.sh
Under '''Kits > Manual''', remove all kits except "Desktop (default)" or "Qt (default)". For example, if you see Manual > "Imported Kit", select it and press the '''Remove''' button. Make sure that you have removed all kits whose names end in "- temporary".
</pre>
 
says:
[[File:Qt creator remove all kits except desktop.png|border|500px|Remove all kits, except Desktop]]
<pre>
 
On the '''Projects''' page, click '''Import Existing Build...''' and choose the directory <code>~/kde/build/kcalc</code> to import the <code>kcalc</code> build that the <code>kdesrc-build</code> command created.
 
[[File:Qt creator import existing build.png|border|300px|Import Existing Build]]
 
CMake should now be configured correctly and Qt Creator will run it automatically. You can view its output in the '''General Messages''' output panel (Alt+9).
 
=== Configure build settings ===
On the '''Projects''' page (Ctrl+5), set the '''Active Project''' selector to <code>kcalc</code>. Then, in the '''Build & Run''' section, you should see a kit named '''Imported Kit''' (make sure not to choose the Desktop kit). Click '''Build''' under this kit to configure it.
 
[[File:Qt creator kit build configuration.png|border|250px|kit build configuration]]
 
In the '''Build Settings''' panel, make sure the '''Edit build configuration:''' field is set to <code>Debug2</code>.
 
[[File:Qt creator build settings debug2.png|border|500px|build settings debug2]]
 
In the '''Current Configuration''' section, click the '''Batch Edit...''' button to open the CMake configuration editor. Then, open the file <code>~/kde/build/kcalc/prefix.sh</code> and translate its contents to the format of the configuration editor.
 
For example, for Debian-based distributions, <code>~/kde/build/kcalc/prefix.sh</code> might look like this:
<syntaxhighlight lang="bash">
export PATH=/home/username/kde/usr/bin:$PATH
export PATH=/home/username/kde/usr/bin:$PATH
 
export XDG_DATA_DIRS=/home/username/kde/usr/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
# LD_LIBRARY_PATH only needed if you are building without rpath
# export LD_LIBRARY_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
 
export XDG_DATA_DIRS=/home/username/kde/usr/share:${XDG_DATA_DIRS:-/usr/local/share/:/usr/share/}
export XDG_CONFIG_DIRS=/home/username/kde/usr/etc/xdg:${XDG_CONFIG_DIRS:-/etc/xdg}
export XDG_CONFIG_DIRS=/home/username/kde/usr/etc/xdg:${XDG_CONFIG_DIRS:-/etc/xdg}
export QT_PLUGIN_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/plugins:$QT_PLUGIN_PATH
export QT_PLUGIN_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/plugins:$QT_PLUGIN_PATH
export QML2_IMPORT_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/qml:$QML2_IMPORT_PATH
export QML2_IMPORT_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/qml:$QML2_IMPORT_PATH
export QT_QUICK_CONTROLS_STYLE_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/qml/QtQuick/Controls.2/:$QT_QUICK_CONTROLS_STYLE_PATH
export QT_QUICK_CONTROLS_STYLE_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/qml/QtQuick/Controls.2/:$QT_QUICK_CONTROLS_STYLE_PATH
</syntaxhighlight>
export MANPATH=/home/username/kde/usr/share/man:${MANPATH:-/usr/local/share/man:/usr/share/man}
export SASL_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/sasl2:${SASL_PATH:-/usr/lib/x86_64-linux-gnu/sasl2}
</pre>


The above script would translate to the following block of text for the '''Edit CMake Configuration''' window in Qt Creator. You'll need to repeat these steps when loading other KDE Git repositories in Qt Creator, so it's helpful to save this to a file such as <code>~/kde/qtcreator_run_environment.txt</code>.
Run:
<syntaxhighlight lang="bash">
<pre>
PATH=+/home/username/kde/usr/bin
source ~/kde/build/kcalc/prefix.sh
XDG_DATA_DIRS=+/home/username/kde/usr/share
XDG_CONFIG_DIRS=+/home/username/kde/usr/etc/xdg
QT_PLUGIN_PATH=+/home/username/kde/usr/lib/x86_64-linux-gnu/plugins
QML2_IMPORT_PATH=+/home/username/kde/usr/lib/x86_64-linux-gnu/qml
QT_QUICK_CONTROLS_STYLE_PATH=+/home/username/kde/usr/lib/x86_64-linux-gnu/qml/QtQuick/Controls.2/
</syntaxhighlight>


Note: if you want to see debugging messages, you can add corresponding variables here. See [[Guidelines and HOWTOs/Debugging/Using Error Messages#Controlling_Messages]]
echo PATH=$PATH
 
echo XDG_DATA_DIRS=$XDG_DATA_DIRS
=== Configure run settings ===
echo XDG_CONFIG_DIRS=$XDG_CONFIG_DIRS
In the '''Build & Run''' section of the '''Projects''' page, click '''Run''' under the '''Imported Kit''' to configure the Run settings.
echo QT_PLUGIN_PATH=$QT_PLUGIN_PATH
 
echo QML2_IMPORT_PATH=$QML2_IMPORT_PATH
In the '''Run''' section under '''Run Settings''', find '''Run configuration:''' and select <code>kcalc</code>. This is the CMake executable target for the GUI application <code>kcalc</code>. The other items in the combobox such as <code>knumbertest</code> are binary executable (CTest type) tests.
echo QT_QUICK_CONTROLS_STYLE_PATH=$QT_QUICK_CONTROLS_STYLE_PATH
echo MANPATH=$MANPATH
echo SASL_PATH=$SASL_PATH
</pre>
says:
<pre>
PATH=/home/username/kde/usr/bin:/home/username/.local/bin:/home/username/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/username/.local/share/JetBrains/Toolbox/scripts
XDG_DATA_DIRS=/home/username/kde/usr/share:/usr/local/share:/usr/share:/var/lib/snapd/desktop
XDG_CONFIG_DIRS=/home/username/kde/usr/etc/xdg:/home/username/.config/kdedefaults:/etc/xdg
QT_PLUGIN_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/plugins:
QML2_IMPORT_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/qml:
QT_QUICK_CONTROLS_STYLE_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/qml/QtQuick/Controls.2/:
MANPATH=/home/username/kde/usr/share/man:/usr/local/share/man:/usr/share/man
SASL_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/sasl2:/usr/lib/x86_64-linux-gnu/sasl2
</pre>


[[File:Qt creator run target.png|border|500px|run target]]
Save the text block above to file <code>~/kde/expanded-prefix-sh.txt</code>. The same text block will be needed when opening in the IDE Qt Creator other KDE projects. The content of this file is also needed when running or debugging a KDE project in other IDEs e.g. JetBrains CLion.


In the '''Environment''' section > open the '''Details''' section next to '''Use Build Environment'''. If the '''Reset''' button is clickable, click it. Press the '''Batch Edit...''' button and paste in the configuration you prepared for the Build Settings in the previous section.
Copy to clipboard the content of the file <code>~/kde/expanded-prefix-sh.txt</code>. From the Qt Creator left sidebar > Projects with tooltip "Switch to Projects mode Ctrl+5" > under the bold text "Build & Run" > Run. The "Run Settings" page is shown. In the combobox "Run configuration:" select "kcalc", in the "Environment" group box > expand "Details", to the right of the button "Append Path..." is an empty multi line edit box, paste inside this edit box. As a safety measure you can check the checkbox "Show in Application Output when running". When Qt Creator runs or debugs the kcalc process, the values of the environment values of the process kcalc are shown in the tool window "Application Output Alt+3". Make sure that the value of the environment variables is correct.


[[File:Qt creator run environment.png|border|700px|run environment]]
Now you can run kcalc correctly, from the Qt Creator main menu > Build > Run Ctrl+R.


Click the button in the sidebar that shows an icon of a computer monitor with the project name above it and the build configuration below it. It should show:
Now you can debug kcalc correctly, from the Qt Creator main menu > Debug > Start Debugging > Start Debugging of Startup Project F5. The debugger can stop on the first line of the C++ process entry point function "main()", from the Qt Creator main menu > Debug > "Start and Break on Main F10".
{| class="wikitable"
|-
| '''Project''' || kcalc
|-
| '''Kit''' || Imported Kit
|-
| '''Deploy''' || Deploy Configuration
|-
| '''Build''' || Debug2
|-
| '''Run''' || kcalc
|}
 
[[File:Qt creator project button above run.png|border|200px|project button above run]]
 
CMake configure was run automatically. You can run it again from the Qt Creator main menu > '''Build''' > '''Run CMake'''.


=== Build the project ===
=== Build the project ===
You can build by pressing the hammer icon on the lower left with tooltip "Build Project Ctrl+B", Or from the Qt Creator main menu > '''Build''' > '''Build Project "kcalc"'''.
You can build by pressing the hammer icon on the lower left with tooltip "Build Project "kcalc" Ctrl+B", or from the Qt Creator main menu > Build > "Build Project "kcalc" Ctrl+B".
 
=== Try the debugger ===
In the left sidebar, switch to '''Edit''' mode (Ctrl+2). Select "Projects" from the drop-down menu at the top of the leftmost panel. Expand kcalc > kcalc > Source Files > double click on the file <code>kcalc.cpp</code> to show it in the editor view.
 
[[File:Qt creator open file in editor.png|border|300px|open file in editor]]
 
In the text editor's top bar, open the '''<Select Symbol>''' drop-down menu, scroll to the bottom of the options, and select the last item in it: <code>main(int, char**) -> int</code>. Click on the line with the opening curly bracket of the function main. Set a breakpoint by opening the '''Debug''' menu > '''Set or Remove Breakpoint''' (F9).
 
Start debugging the project by opening the '''Debug''' menu > '''Start Debugging''' > '''Start Debugging of Startup Project''' (F5). The debugger will start and pause execution on the source code line with the curly bracket.
 
Use the debugger by opening the '''Debug''' menu > '''Step Over''' (F10)/'''Step Into''' (F11)/'''Step Out''' (Shift+F11).
 
==Enable parallel jobs in Qt Creator==
 
* Left Sidebar > Switch to Projects mode (Ctrl+5).
* Under the Build settings of the imported kit find the "Build Steps" option.
* Click on the "Details" button on the right side of the "Build Steps" option to access the detailed build configuration.
* Under the "Tool arguments" field add the argument <code>-j<jobs></code>.  Replace "<jobs>" with the desired number of parallel jobs you want to use during compilation. For example, if you have a CPU with 16 threads and want to use 12 parallel jobs, you would enter <code>-j12</code>.
[[File:Qt creator parallel jobs.png|border|600px|Parallel jobs]]
* Rebuild your project to get faster compilation.


== Tips and Tricks ==
== Tips and Tricks ==
Line 201: Line 131:


When developing a library, it may be convenient to launch some application that uses it, from the current project. For example, you work with Ark's libraries used in dolphin context menu actions. You can make your run configuration to launch custom binary - dolphin. See [https://doc.qt.io/qtcreator/creator-run-settings.html#specifying-a-custom-executable-to-run documentation] on how to configure that.
When developing a library, it may be convenient to launch some application that uses it, from the current project. For example, you work with Ark's libraries used in dolphin context menu actions. You can make your run configuration to launch custom binary - dolphin. See [https://doc.qt.io/qtcreator/creator-run-settings.html#specifying-a-custom-executable-to-run documentation] on how to configure that.
==See also==
https://community.kde.org/User:Nmariusp/Clion

Latest revision as of 03:12, 19 November 2024

Qt Creator is an IDE from Qt.

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

See also https://community.kde.org/Get_Involved/development/IDE_configuration/Qt_Creator

Features

Qt Creator is a good choice when starting to contribute to KDE.

Qt Creator has: support for kdesrc-build, a good debugger, source code navigation, Qt widgets UI designer, basic QML editor, QML debugger, Qt resources editor, Qt project templates, good CMake support, C++ static analyzers.

Additional features:

  • Source code navigation: switch header/source, follow symbol, switch between function declaration/definition, find references, open type hierarchy, open include hierarchy.
  • Refactor rename symbol.
  • Views: class view, tests view, document outline view and document outline combo box, CMake structure view, file system view.

For best results, download Qt Creator from the Qt website. https://www.youtube.com/watch?v=QVgInye6HDA

Install Qt Creator

Using the Qt online installer - recommended

The newest version of Qt Creator installed from the Qt website will have the latest features and the latest bug fixes.

Video version https://www.youtube.com/watch?v=QVgInye6HDA

Go to https://www.qt.io/ on the top right click on the button "Download. Try.". The web browser navigates to "https://www.qt.io/download", in the top right quarter of the page it says "Open source user? Find out how you can use Qt under the (L)GPL and contribute to the Qt project. Download open source". Click on the button "Download open source". The web browser navigates to https://www.qt.io/download-open-source. In the bottom middle of the page it says "Looking for Qt binaries? Find them in the Qt Online Installer. It will steer you to the right download version and help you install tools and add-on components that are available for your open source license. Download the Qt Online Installer". Click on the button "Download the Qt Online Installer" > Linux > "Qt Online Installer for Linux (64-bit)". A file named e.g. qt-unified-linux-x64-4.6.1-online.run is downloaded.

chmod +x qt-unified-linux-x64-4.6.1-online.run
./qt-unified-linux-x64-4.6.1-online.run

The Qt online installer will start. The Qt online installer requires that you create an online user account for https://www.qt.io .

From Linux OS binary packages

E.g.

apt install qtcreator

QML Designer

Qt Creator has a "forms editor" for QML. You can enable it from the Qt Creator main menu > Help > About Plugins... > Qt Quick > enable "QmlDesigner". If it asks you to restart Qt Creator, do it.

Test it. From Qt Designer main menu > File > Close all Files and Editors. File > New Project... > Application (Qt) > Qt Quick Application, finish the wizard correctly. Open the file "Main.qml" in "Switch to Edit mode Ctrl+2". In the left hand side, choose "Switch to Design mode Ctrl+3".

From the Qt Creator main menu > View > Workspaces > Views-All. Close all of the tool windows (views) that you do not need, e.g. "3D", "States", "Timeline", "Curves", "Transitions".

Open a KDE project in Qt Creator >= 15

Please make sure that you use Qt Creator version 15 or newer.

I configure kdesrc-build to use the CMake build configuration "Debug" in order to be able to use the debugger in the IDE.

cmake-options -DCMAKE_BUILD_TYPE=Debug
...
num-cores 14

We'll use kcalc as an example KDE module and git repository. First, make sure it was built correctly using kdesrc-build.

kdesrc-build kcalc

Start Qt Creator (for example, from the KDE Application Launcher).

From the Qt Creator main menu, select File > "Open File or Project" > ~/kde/build/kcalc/CMakeCache.txt. Click on the button "Configure Project".

Run

cat ~/kde/build/kcalc/prefix.sh

says:

export PATH=/home/username/kde/usr/bin:$PATH
export XDG_DATA_DIRS=/home/username/kde/usr/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
export XDG_CONFIG_DIRS=/home/username/kde/usr/etc/xdg:${XDG_CONFIG_DIRS:-/etc/xdg}
export QT_PLUGIN_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/plugins:$QT_PLUGIN_PATH
export QML2_IMPORT_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/qml:$QML2_IMPORT_PATH
export QT_QUICK_CONTROLS_STYLE_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/qml/QtQuick/Controls.2/:$QT_QUICK_CONTROLS_STYLE_PATH
export MANPATH=/home/username/kde/usr/share/man:${MANPATH:-/usr/local/share/man:/usr/share/man}
export SASL_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/sasl2:${SASL_PATH:-/usr/lib/x86_64-linux-gnu/sasl2}

Run:

source ~/kde/build/kcalc/prefix.sh

echo PATH=$PATH
echo XDG_DATA_DIRS=$XDG_DATA_DIRS
echo XDG_CONFIG_DIRS=$XDG_CONFIG_DIRS
echo QT_PLUGIN_PATH=$QT_PLUGIN_PATH
echo QML2_IMPORT_PATH=$QML2_IMPORT_PATH
echo QT_QUICK_CONTROLS_STYLE_PATH=$QT_QUICK_CONTROLS_STYLE_PATH
echo MANPATH=$MANPATH
echo SASL_PATH=$SASL_PATH

says:

PATH=/home/username/kde/usr/bin:/home/username/.local/bin:/home/username/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/username/.local/share/JetBrains/Toolbox/scripts
XDG_DATA_DIRS=/home/username/kde/usr/share:/usr/local/share:/usr/share:/var/lib/snapd/desktop
XDG_CONFIG_DIRS=/home/username/kde/usr/etc/xdg:/home/username/.config/kdedefaults:/etc/xdg
QT_PLUGIN_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/plugins:
QML2_IMPORT_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/qml:
QT_QUICK_CONTROLS_STYLE_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/qml/QtQuick/Controls.2/:
MANPATH=/home/username/kde/usr/share/man:/usr/local/share/man:/usr/share/man
SASL_PATH=/home/username/kde/usr/lib/x86_64-linux-gnu/sasl2:/usr/lib/x86_64-linux-gnu/sasl2

Save the text block above to file ~/kde/expanded-prefix-sh.txt. The same text block will be needed when opening in the IDE Qt Creator other KDE projects. The content of this file is also needed when running or debugging a KDE project in other IDEs e.g. JetBrains CLion.

Copy to clipboard the content of the file ~/kde/expanded-prefix-sh.txt. From the Qt Creator left sidebar > Projects with tooltip "Switch to Projects mode Ctrl+5" > under the bold text "Build & Run" > Run. The "Run Settings" page is shown. In the combobox "Run configuration:" select "kcalc", in the "Environment" group box > expand "Details", to the right of the button "Append Path..." is an empty multi line edit box, paste inside this edit box. As a safety measure you can check the checkbox "Show in Application Output when running". When Qt Creator runs or debugs the kcalc process, the values of the environment values of the process kcalc are shown in the tool window "Application Output Alt+3". Make sure that the value of the environment variables is correct.

Now you can run kcalc correctly, from the Qt Creator main menu > Build > Run Ctrl+R.

Now you can debug kcalc correctly, from the Qt Creator main menu > Debug > Start Debugging > Start Debugging of Startup Project F5. The debugger can stop on the first line of the C++ process entry point function "main()", from the Qt Creator main menu > Debug > "Start and Break on Main F10".

Build the project

You can build by pressing the hammer icon on the lower left with tooltip "Build Project "kcalc" Ctrl+B", or from the Qt Creator main menu > Build > "Build Project "kcalc" Ctrl+B".

Tips and Tricks

Custom executable to run

When developing a library, it may be convenient to launch some application that uses it, from the current project. For example, you work with Ark's libraries used in dolphin context menu actions. You can make your run configuration to launch custom binary - dolphin. See documentation on how to configure that.

See also

https://community.kde.org/User:Nmariusp/Clion