Skip to content
The Chat Archive
gpt-4o-2024-05-13· OpenAI WildChat-4.8M (AI2)

How to enable dark theme for Qt applications in Linux?

  1. User

    How to enable dark theme for Qt applications in Linux?

  2. Assistant

    Enabling a dark theme for Qt applications in Linux can be achieved in a few different ways, depending on your desktop environment and the specific theme you'd like to apply. Below are some common methods:

    Method 1: Qt5 Configuration Tool (qt5ct)

    1. Install Qt5 Configuration Tool:

      sudo apt-get install qt5ct
      
    2. Set the Environment Variable: Edit your shell configuration file (e.g., ~/.bashrc, ~/.zshrc) to include the following line:

      export QT_QPA_PLATFORMTHEME=qt5ct
      
    3. Source the Configuration:

      source ~/.bashrc  # or ~/.zshrc
      
    4. Run the Qt5 Configuration Tool:

      qt5ct
      
    5. In the Qt5 Configuration Tool:

      • Go to the Appearance tab.
      • Select a dark theme from the available styles (e.g., Adwaita-dark, Breeze-Dark).
    6. Apply and Exit:

      • Click on Apply.
      • Restart your Qt applications to see the changes.

    Method 2: kvantum Theme Engine

    1. Install Kvantum:

      sudo apt-get install kvantum
      sudo apt-get install qt5-style-kvantum qt5-style-kvantum-themes
      
    2. Set Kvantum as the Style: Edit your shell configuration file to include:

      export QT_STYLE_OVERRIDE=kvantum
      export QT_QPA_PLATFORMTHEME=qt5ct
      
    3. Source the Configuration:

      source ~/.bashrc  # or ~/.zshrc
      
    4. Run Kvantum Manager:

      kvantummanager
      
    5. In the Kvantum Manager:

      • Select a dark theme from the available options or download and install additional themes.

    Method 3: Using a Desktop Environment's Settings

    If you are using a desktop environment like KDE Plasma, you can generally enable a dark theme through the system settings:

    1. KDE Plasma:

      • Open System Settings.
      • Go to Appearance > Global Theme or Application Style.
      • Select a dark theme such as Breeze Dark.
    2. GNOME:

      • Install the gnome-tweaks tool:
        sudo apt-get install gnome-tweaks
        
      • Open GNOME Tweaks.
      • Go to Appearance and select a dark theme.

    Method 4: Applying Qt Environment Variables

    You can directly apply some environment variables that affect Qt application theming.

    1. Dynamic Variables: Add these variables to your shell configuration file (e.g., ~/.bashrc, ~/.zshrc):

      export QT_STYLE_OVERRIDE=Fusion
      export QT_QPA_PLATFORMTHEME=gtk2
      
    2. Restart Session:

      source ~/.bashrc  # or ~/.zshrc
      

    Method 5: Custom QT Stylesheets

    You can also create a custom stylesheet for Qt applications:

    1. Create a Dark Theme Stylesheet (e.g., dark-theme.qss):

      QWidget {
          background-color: #2E2E2E;
          color: white;
      }
      
    2. Apply via Environment Variable: Add this to your shell configuration file:

      export QT_QSS_FILE=/path/to/dark-theme.qss
      
    3. Apply Stylesheet Programmatically: You can apply the stylesheet in your Qt application code:

      QFile file("/path/to/dark-theme.qss");
      file.open(QFile::ReadOnly | QFile::Text);
      QTextStream stream(&file);
      qApp->setStyleSheet(stream.readAll());
      

    Conclusion

    Different methods might suit different environments and personal preferences. Using qt5ct or kvantum usually covers most use cases effectively. If you're working within a specific desktop environment, their built-in theming options are often the most seamless route.