How to force Qt application running via sudo to use user theme/fonts configuration?

0

everyone.

I am developing applications in Qt5, but it requires low level hardware access and thus need to be executed as root. My target operating systems are Debian (ARM) and Ubuntu (x86).

Problem is, that all apps that are executed as root (via sudo) are using root settings and enviroment.

I need to somehow force Qt app to run with same theme and fonts as normal user.

Is it somehow possible to achieve this? Like executing via shell script with additional execution parameters and commands?

Here are screenshots to show difference:

Qt app running as root via sudo

Qt app running as user

HeliTux

Posted 2017-10-28T15:46:57.837

Reputation: 11

2Better solution IMHO: Split off hardware dependent part, wrap in an API and execute as a separate process as root. Then the Qt GUI part can run as the user. That also makes it easier to add other UIs, e.g. commandline. – dirkt – 2017-10-28T16:58:02.787

I second dirkt's suggestion. Or, as an alternative, add the required POSIX capabilities (e.g. CAP_SYS_RAWIO) to the executable file and enable the capability only in the parts of the program where it is really needed. See man 7 capabilities. Running GUI programs as root is generally considered a bad idea, since they contain a massive amount of software (Qt, for example) that has not been audited for this kind of use. – Johan Myréen – 2017-10-28T19:24:16.743

Well, I am not that great developer to reimplement all in API. I developed C++ CLI apps few years, but quite new to Qt implementations. I am using hidapi library by signal11, thats main HW level implementation (with my small customizations). My first idea was to somehow allow opening all USB HID devices as user via something like udev rule, but not found any too "generic" solution yet... – HeliTux – 2017-10-28T19:45:13.783

Answers

0

Your user themes reside in ~/.themes in your home directory, where root won't be looking. System-wide themes will always work fine from /usr/share/themes.

You could open your terminal and enter :

sudo ln -s ~/.themes /root/.themes

This way root and normal users will share the theme directory.

If required, you might also do the same for font and icon preferences :

sudo ln -s ~/.icons /root/.icons
sudo ln -s ~/.fonts /root/.fonts

The only disadvantage is that you will not have any visual cue to remind you when a running the application with root privileges.

harrymc

Posted 2017-10-28T15:46:57.837

Reputation: 306 093

Unfortunately this has zero effect. It looks like Qt is looking for theme data somewhere else. I maybe reconsider to post this questing to the stackoverflow, because its maybe more Qt code related.... – HeliTux – 2017-10-28T19:49:08.313

These might not be themes but rather Qt styles. These reside in something like ~/.config/QtProject/qtcreator/styles - better verify this on your computer. If this is indeed the right folder, try to repeat the above for ~/.config. – harrymc – 2017-10-29T08:24:03.153

0

Unfortunately it seems that its not possible in newer Qt version on recent distros. Problem is described in this link for Arch, but suggested workaround is not working on Ubuntu/Debian.

But found workaround, that is probably much better if you want 100% static design (on all machines) in root Qt application.

You need to specify custom Qt stylesheet and custom style for application:

sudo ./QtApplication -style Adwaita -stylesheet=./stylesheet.css

System availiable styles are located in /usr/lib/x86_64-linux-gnu/qt5/plugins/styles/ . In my case, availiable styles are Adwaita, Breeze and Oxygen. These styles only affects style of UI elements - e.g. pushbuttons, lists, dials and so on. To apply custom UI colors and fonts, I used custom stylesheet. I wrote my own based on this custom stylesheet intended for QtCreator

Not sure how to apply icon theme also, but this is not a big deal in my case.

This will force app to run with completely custom specified design, no matter how local style and fonts are set up. It can be used on user and root

HeliTux

Posted 2017-10-28T15:46:57.837

Reputation: 11