Qt
Related articles
Qt is a cross-platform application and widget toolkit that uses standard C++ but makes extensive use of a special code generator (called the Meta Object Compiler, or moc) together with several macros to enrich the language. Some of its more important features include:
- Running on the major desktop platforms and some of the mobile platforms.
- Extensive internationalization support.
- A complete library that provides SQL database access, XML parsing, thread management, network support, and a unified cross-platform application programming interface (API) for file handling.
The Qt framework is emerging as a major development platform and is the basis of the KDE software community, among other important open source and proprietary applications such as VLC, VirtualBox, Opera, Mathematica, Skype and many others.
Contents
Installation
Two versions of Qt are currently available in the official repositories. They can be installed with the following packages:
- Qt 5.x is available in the qt5-base package, with documentation in the qt5-doc package.
- Qt 4.x is available in the qt4 package, with documentation on AUR in the qt4-docAUR package.
- Qt 3.x is available from the AUR in the qt3AUR package, with documentation on AUR in the qt3-docAUR package.
Qt5 Platform Plugins and Missing Dependencies
Using qt5-base will install Qt5, which makes use of platform abstractions, but some platform abstraction plugin dependencies may be missing. Qt may tell you that the plugin is unavailable, rather than resolve the chain of missing dependencies. For instance, if your application relies on xcb, and dependencies of the xcb library are missing, Qt will report:
This application failed to start because it could not find or load the Qt platform plugin "xcb". Available plugins are: xcb. Reinstalling the application may fix this problem.
The Qt5 page proposes a list of packages that are recommended for installation, blindly installing all of which may not be very Arch-like. Instead, you may identify the plugin (in the case of xcb, libqxcb.so), and use ldd
to identify missing dependencies. Installing these dependencies should resolve the issue.
Default Qt toolkit
By installing qtchooser you can restore the usual bins (e.g. qmake) in /usr/bin
and setup the Qt toolkit to use. By default Qt5 is used.
Using environment variables
To define default Qt toolkit, you can create QT_SELECT
environment variable. For example, to set Qt4, do export QT_SELECT=4
in your shell's init file (e.g. ~/.bash_profile.
or ~/.zsh_profile.
).
Using configuration files
You can set default Qt toolkit by creating a symlink ~/.config/qtchooser/default.conf
to one of .conf files in /etc/xdg/qtchooser/
directory. For example, to set Qt4 symlink /etc/xdg/qtchooser/4.conf
to ~/.config/qtchooser/default.conf
:
$ ln -s/etc/xdg/qtchooser/4.conf
~/.config/qtchooser/default.conf
Appearance
Qt4
Qt4 application will try to mimic the behavior of the desktop environment they are running on, unless they run into some problems or hard-coded settings.
- In KDE 4, it uses the actually selected Qt style in KDE. It can be configured using KDE System Settings (systemsettings), the settings can be found in Common Appearance and Behavior > Application Appearance > Style > Widget Style
- In KDE Plasma 5, it uses Plastique.
- In Cinnamon, GNOME, Xfce, it uses GTK+ (QGtkStyle).
- In other desktop environments, it uses Windows.
For those who want to change the look and feel of Qt4 application, the Qt Configuration (qtconfig-qt4) GUI tool is available. It offers a very simple configuration for the appearance of Qt4 applications that gives the user easy access to the current Qt Style, colors, fonts and other more advanced options.
Qt keeps all its configuration information in /etc/xdg/Trolltech.conf
(system-wide) or ~/.config/Trolltech.conf
(user-specific). The file is rather difficult to navigate because it contains a lot of information not related to appearance, but for any changes you can just add to the end of the file and overwrite any previous values (make sure to add your modification under the [Qt] header).
For example, to change the theme to QtCurve, add:
~/.config/Trolltech.conf
... [Qt] style=QtCurve
The following styles are included in Qt4: CDE, Cleanlooks, GTK+, Motif, Plastique, Windows. Others can be installed from the official repositories or the AUR (most are written for KDE Plasma Desktop):
- Breeze — Artwork, styles and assets for the Breeze visual style for the Plasma Desktop.
- Oxygen — KDE Oxygen style.
- QtCurve — A configurable set of widget styles for KDE and Gtk.
- Phase — Widget style designed to be functional but not drab, and aesthetic but not distracting.
- Skulpture — A GUI style addon for KDE and Qt programs that features a classical three dimensional artwork with shadows and smooth gradients to enhance the visual experience.
- Bespin — A very configurable KDE theme.
Qt5
Qt5 decides the style to use based on what desktop environment is used:
- In KDE Plasma 5, it uses the actually selected Qt style in KDE Plasma 5. It can be configured using KDE 5 System Settings (systemsettings5), the settings can be found in Appearance > Application Style > Widget Style.
- In KDE 4, it uses Oxygen (if available) or Fusion.
- In Cinnamon, GNOME, MATE, LXDE, Xfce, it uses GTK+ (QGtkStyle).
- In other desktop environments, it uses Fusion.
To force a specific style, you can set the QT_STYLE_OVERRIDE
environment variable. Specifically, set it to GTK+
if you want to use the gtk theme. Qt5 applications also support the -style
flag, which you can use to launch a Qt5 application with a specific style.
The following styles are included in Qt5: GTK+, Fusion, Windows. Others can be installed from the official repositories:
- Breeze — Artwork, styles and assets for the Breeze visual style for the Plasma Desktop.
- Oxygen — KDE Oxygen style.
- QtCurve — A configurable set of widget styles for KDE and Gtk.
Qt Style Sheets
An interesting way of customizing the look and feel of a Qt application is using Style Sheets, which are just simple CSS files. Using Style Sheets, one can modify the appearance of every widget in the application.
To run an application with a different style just execute:
$ qt_application -stylesheet style.qss
For more information on Qt Style Sheets see the official documentation or other tutorials. As an example Style Sheet see this Dolphin modification.
GTK+ and Qt
If you have GTK+ and Qt applications, their looks might not exactly blend in very well. If you wish to make your GTK+ styles match your Qt styles please read Uniform Look for Qt and GTK Applications.
Development
Supported platforms
Qt supports most platforms that are available today, even some of the more obscure ones, with more ports appearing every once in a while. For a more complete list see the Qt Wikipedia article.
Tools
The following are official Qt tools:
- Qt Creator — A cross-platform IDE tailored for Qt that supports all of its features.
- Qt Linguist — A set of tools that speed the translation and internationalization of Qt applications.
- Qt Assistant — A configurable and redistributable documentation reader for Qt qch files.
- Qt Designer — A powerful cross-platform GUI layout and forms builder for Qt widgets.
- Qt Quick Designer — A visual editor for QML files which supports WYSIWYG. It allows you to rapidly design and build Qt Quick applications and components from scratch.
- QML Viewer — A tool for loading QML documents that makes it easy to quickly develop and debug QML applications.
- qmake — A tool that helps simplify the build process for development project across different platforms, similar to cmake, but with fewer options and tailored for Qt applications.
- uic — A tool that reads *.ui XML files and generates the corresponding C++ files.
- rcc — A tool that is used to embed resources (such as pictures) into a Qt application during the build process. It works by generating a C++ source file containing data specified in a Qt resource (.qrc) file.
- moc — A tool that handles Qt's C++ extensions (the signals and slots mechanism, the run-time type information, and the dynamic property system, etc.).
Bindings
Qt has bindings for all of the more popular languages, for a full list see this list.
The following examples display a small 'Hello world!' message in a window.
C++
- Package:
- Website: http://qt-project.org/
- Build:
- The Qt4 version:
g++ $(pkg-config --cflags --libs QtGui) -o hello hello.cpp
- The Qt5 version:
g++ $(pkg-config --cflags --libs Qt5Widgets) -o hello hello.cpp
- The Qt4 version:
- Run with:
./hello
hello.cpp
#include <QApplication> #include <QLabel> int main(int argc, char **argv) { QApplication app(argc, argv); QLabel hello("Hello world!"); hello.show(); return app.exec(); }
QML
- Package: qt4 or qt5-declarative.
- Website: http://qt-project.org/
- Run with:
qmlviewer-qt4 hello.qml
orqmlscene-qt5 hello.qml
hello.qml
import QtQuick 1.0 Rectangle { id: page width: 400; height: 100 color: "lightgray" Text { id: helloText text: "Hello world!" anchors.horizontalCenter: page.horizontalCenter anchors.verticalCenter: page.verticalCenter font.pointSize: 24; font.bold: true } }
Python (PyQt)
- Package:
- python-pyqt4 - Python 3.x bindings for Qt 4
- python2-pyqt4 - Python 2.x bindings for Qt 4
- python-pyqt5 - Python 3.x bindings for Qt 5
- python2-pyqt5 - Python 2.x bindings for Qt 5
- Website: http://www.riverbankcomputing.co.uk/software/pyqt/intro
- Run with:
python hello-pyqt.py
orpython2 hello-pyqt.py
.
hello-pyqt.py
import sys from PyQt4 import QtGui app = QtGui.QApplication(sys.argv) label = QtGui.QLabel("Hello world!") label.show() sys.exit(app.exec_())
The Qt 5.x version is slighly different:
hello-pyqt.py
import sys from PyQt5 import QtWidgets app = QtWidgets.QApplication(sys.argv) label = QtWidgets.QLabel("Hello world!") label.show() sys.exit(app.exec_())
Python (PySide)
- Package:
- python-pyside - Python 3.x bindings
- python2-pyside - Python 2.x bindings
- Website: http://www.pyside.org/
- Run with:
python hello-pyside.py
orpython2 hello-pyside.py
hello-pyside.py
import sys from PySide.QtCore import * from PySide.QtGui import * app = QApplication(sys.argv) label = QLabel("Hello world!") label.show() sys.exit(app.exec_())
C#
- Package: kdebindings-qyotoAUR
- Website: http://techbase.kde.org/Development/Languages/Qyoto
- Build with:
mcs -pkg:qyoto hello.cs
- Run with:
mono hello.exe
hello.cs
using System; using Qyoto; public class Hello { public static int Main(String[] args) { new QApplication(args); new QLabel("Hello world!").Show(); return QApplication.Exec(); } }
Ruby
- Package: kdebindings-qtrubyAUR
- Website: http://rubyforge.org/projects/korundum/
- Run with:
ruby hello.rb
hello.rb
require 'Qt4' app = Qt::Application.new(ARGV) hello = Qt::Label.new('Hello World!') hello.show app.exec
Java
- Package: qtjambiAUR or qtjambi-betaAUR.
- Website: http://qt-jambi.org/
- Build with:
javac Hello.java -cp /opt/qtjambi-beta/qtjambi-linux64-community-4.7.0/qtjambi-4.7.0.jar
. - Run with:
java -cp /opt/qtjambi-beta/qtjambi-linux64-community-4.7.0/qtjambi-4.7.0.jar:. Hello
.
Hello.java
import com.trolltech.qt.gui.*; public class Hello { public static void main(String args[]) { QApplication.initialize(args); QLabel hello = new QLabel("Hello World!"); hello.show(); QApplication.exec(); } }
Perl
- Package: kdebindings-perlqtAUR
- Website: http://code.google.com/p/perlqt4/
- Run with:
perl hello.pl
hello.pl
use QtGui4; my $a = Qt::Application(\@ARGV); my $hello = Qt::Label("Hello World!", undef); $hello->show; exit $a->exec;
Lua
- Package: libqtluaAUR
- Website: http://www.nongnu.org/libqtlua/
- Run with:
qtlua hello.lua
hello.lua
label = qt.new_widget("QLabel") label:setText("Hello World!") label:show()
Troubleshooting
Icon theme is not applied
Since Qt 5.1 SVG support has moved into a module. Since qt5-base does not depend on qt5-svg it may happen that the qt5-base is installed but not qt5-svg. This results in deceptive icon theme behaviour. Since SVG is not supported the icons are silently skipped and the icon theme may seem to be unused. Installing qt5-svg explicitly solves the problem.
Qt4 style not respected
If pure Qt4 (non-KDE) applications do not stick with your selected Qt4 style, then you'll probably need to tell Qt4 how to find KDE's styles (Oxygen, Phase etc.). You just need to set the environment variable QT_PLUGIN_PATH. E.g. put
export QT_PLUGIN_PATH=$HOME/.kde4/lib/kde4/plugins/:/usr/lib/kde4/plugins/
into your /etc/profile
(or ~/.profile
if you do not have root access). qtconfig-qt4
should then be able to find your kde styles and everything should look nice again!
Alternatively, you can symlink the Qt4 styles directory to the KDE4 styles one:
# ln -s /usr/lib/{kde,qt}4/plugins/styles/[theme name]