FreeLanguageTools/vocabsieve

Fails to start because QApplication is not instantiated before qdarktheme is imported

Closed this issue · 12 comments

Describe the bug
The application doesn't start because qtdarktheme.setup_theme("auto") (in qdarktheme) is called before QApplication is instantiated. The program doesn't start (and gives the backtrace I will give in the 'Additional context' section (I don't know where else to put it with this template)).

It is resolved when I switch the import order (details in the 'Fix' section).

To Reproduce
Steps to reproduce the behavior:

  1. Install Vocabsieve 0.11.0
  2. Run Vocabsieve

Expected behavior
I expect the program to start :>

Screenshots
N.A.

Logs
Irrelevant as the program wasn't even starting. See the backtrace in the 'Additional context' section.

Desktop:

  • OS: Arch Linux (also tested on Manjaro)
  • Vocabsieve version (if nightly, must be latest): 0.11.0-00 (from AUR package)
  • qdarktheme: 2.1.0-3 (from AUR package)

Additional context
Here is the backtrace that I get:

pymorphy3 is available for RU
pymorphy3 is available for UK
Traceback (most recent call last):
  File "/usr/bin/vocabsieve", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/lib/python3.11/site-packages/vocabsieve/main.py", line 864, in main
    qdarktheme.setup_theme("auto")
  File "/usr/lib/python3.11/site-packages/qdarktheme/_main.py", line 160, in setup_theme
    raise Exception("setup_theme() must be called after instantiation of QApplication.")
Exception: setup_theme() must be called after instantiation of QApplication.

Fix
Luckily, this seems like it is easily fixed by changing the order of the imports in main.py so that app is initialized before qdarktheme is imported (assuming my fix isn't messing anything else up (I don't know because I've never used it before so I don't know how well it usually works)).

To fix it I simply moved the import qdarktheme from its current position (main.py:12) to immediatly after the import of app from .global_names (currently main.py:22 with the comment "First local import").

After doing this the program starts and runs properly (at least as far as I can tell). There are some UI issues but I don't know if they are a result of my fix or would be there anyway.

Here is a screenshot of one (dropdown boxes backgrounds are missing):

potential-issue

I thought this has to do with qdarktheme trying to use PyQt6/PySide6 if your system has it instead of PyQt5. I should check if your fix would work.

@1over137 I was helping install it for a family member so I can't check the Manjaro machine, but at least on mine, I have both PyQt6 and PyQt5 installed.

Unless qdarktheme is doing something weird when selecting which one to use I would assume that that part is being handled correctly (I haven't looked into it too deeply so far, though).

Given the order switching makes the app sucessfully start and the exception message warning that "setup_theme() must be called after instantiation of QApplication" it seems like that is at least part of the original problem.

If you are referring to the dropdown-box UI messiness that was there after I got it to start, then I wouldn't be surprised if that were the cause.

As an update on the dropdown box thing/UI after the 'fix':
The theming situation seems much better on my personal computer (the more plain/custom Arch one) than they were on the Manjaro one I sent the first screenshot from. The dropdown-box appears better, but is infact still transparent (the blur from my compositor just happens to make it readable in this case).

My Computer:
interface-on-mine

Manjaro (family member's computer with basically default config):

manjaro-issue

Summary of wierd things that I noticed (that the fix may or may not have caused)

  • On the manjaro the outer border of the dark theme version of the configuration pane remains white.
  • On both, the dropdown-boxes have a transparent background (practically mitigated on mine because I had my compositor set to blur those anyway).

If this turns out to be unrelated I'll make a separate issue.

What I observed:

  • Looking at the source code of qdarktheme, it looks like importing PyQt5.QtCore first would solve the issue. I moved import qdarktheme to right before the .global_names import to see if it would work, and it appears to work just fine.
  • The lack of background for the combobox appears to be unrelated to this specific change, however, but rather more likely due to some kind of bug in the stylesheet in qdarktheme. If PyQt6 or PySide6 isn't installed (like in a virtual environment or the appimage), this does not happen. However, I can't reproduce this in a virtual environment with both PyQt5 and PySide6 installed, which leads me to think that it's your system theme leaking through the qdarktheme stylesheet causing a bug. You can check this by using QT_QPA_PLATFORMTHEME=qt5c python vocabsieve.py (deliberate mistake to use default theme), and this issue does not occur.

On another note, I am planning to move to PySide6 soon, which may or may not affect this.

In the mean time, you should probably let your family member use the AppImage, which is an isolated environment with only PyQt5.

Thanks I didn't even know there was an appimage version.

Yep, moving the import to where you said works just fine as well. (If you want I can close this issue and make a new one for the theming issues now that you confirmed that they are independent.)

On the background issue, your specific test doesn't work.

Both of the computers I tested use kvantum which is specified by the QT_STYLE_OVERRIDE variable instead of QT_QPA_PLATFORMTHEME. I was able to get the backgrounds to load using QT_STYLE_OVERRIDE="" python vocabsieve.py. (The fact that is seems to actually listen to the environment variables, unlike some other Qt apps which just seem to have broken themes regardless, is really nice).

Oddly enough, if I go in the preview of Kvantum to see what it should like with the system theme fully and properly applied, the combo boxes all have completely opaque backgrounds so whatever is going on here is an emergent behavior.

Kvantum preview showing opaque combobox background:
kvantum-preview

The fact that is seems to actually listen to the environment variables

This is just a result of it using system Qt libraries instead of statically linked ones, like in the appimage.

I actually plan to provide a way to use system default theme on Linux. This custom theme is applied mainly to solve the lack of dark theme on Windows and the general ugliness of the default theme, but I know a lot of Linux users would prefer it to respect the system theme, and maybe that can be the default on Linux since we have proper light/dark systemwide themes anyways.

This is just a result of it using system Qt libraries instead of statically linked ones, like in the appimage.

Yea that would make sense. The example I was thinking of was KDE Connect which uses Qt libraries but needs a bunch of KDE specific config things to control the theming (rather than working like other Qt apps).

Mostly though, it was just a compliment for having a pretty straightforward app.

I can confirm that I have the similar issue

But I use Arch Linux btw,
I wanted to add Dark theme globally to my applications. And It works perfectly for most applications, but not for vocabsieve

I've done this:
https://wiki.archlinux.org/title/Dark_mode_switching
image
And only that.

$ vocabsieve
QApplication: invalid style override 'adwaita-dark' passed, ignoring it.
        Available styles: Windows, Fusion
pymorphy3 is available for RU
pymorphy3 is available for UK
Traceback (most recent call last):
  File "/usr/bin/vocabsieve", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/lib/python3.11/site-packages/vocabsieve/main.py", line 862, in main
    qdarktheme.setup_theme(theme)
  File "/usr/lib/python3.11/site-packages/qdarktheme/_main.py", line 160, in setup_theme
    raise Exception("setup_theme() must be called after instantiation of QApplication.")
Exception: setup_theme() must be called after instantiation of QApplication.

EDIT:

  • Okay now I see, that with the AppImage the same complain about dark theme happens, but at least the app opens :)

@Cloufish Have you tried opening a different Qt application with that value for QT_STYLE_OVERRIDE? That error looks like it is a problem with your setup, not the specific application (but I could be wrong).

For example:

QT_STYLE_OVERRIDE="nonexistent" qalculate-qt

Returns

QApplication: invalid style override 'nonexistent' passed, ignoring it.
	Available styles: HighContrastInverse, HighContrast, Adwaita-HighContrastInverse, Adwaita-HighContrast, Adwaita-Dark, Adwaita, Windows, Fusion

Whereas:

QT_STYLE_OVERRIDE="Adwaita-Dark" qalculate-qt

Starts properly with 0 errors.

You might want to do something to ensure that it was properly installed and discoverable.

On my system when I run

QT_STYLE_OVERRIDE="Adwaita-Dark" vocabsieve

I get this for the same part:

pymorphy3 is available for RU
pymorphy3 is available for UK
Attribute Qt::AA_EnableHighDpiScaling must be set before QCoreApplication is created.
Initializing local dictionary object at  /home/user/.local/share/FreeLanguageTools/VocabSieve/dict.db
Either successfully made unique index, or there is already one
 * Serving Flask app 'vocabsieve.reader.server'
 * Debug mode: off
Started checking updates
Finished checking updates

You might want to check that the files actually got installed:

When I run

pacman -Ql adwaita-qt6 adwaita-qt5

I get this:

adwaita-qt6 /usr/
adwaita-qt6 /usr/include/
adwaita-qt6 /usr/include/AdwaitaQt6/
adwaita-qt6 /usr/include/AdwaitaQt6/adwaita.h
adwaita-qt6 /usr/include/AdwaitaQt6/adwaitacolors.h
adwaita-qt6 /usr/include/AdwaitaQt6/adwaitaqt_export.h
adwaita-qt6 /usr/include/AdwaitaQt6/adwaitarenderer.h
adwaita-qt6 /usr/lib/
adwaita-qt6 /usr/lib/cmake/
adwaita-qt6 /usr/lib/cmake/AdwaitaQt6/
adwaita-qt6 /usr/lib/cmake/AdwaitaQt6/AdwaitaQt6Config.cmake
adwaita-qt6 /usr/lib/cmake/AdwaitaQt6/AdwaitaQt6ConfigVersion.cmake
adwaita-qt6 /usr/lib/cmake/AdwaitaQt6/AdwaitaQt6Targets-noconfig.cmake
adwaita-qt6 /usr/lib/cmake/AdwaitaQt6/AdwaitaQt6Targets.cmake
adwaita-qt6 /usr/lib/libadwaitaqt6.so
adwaita-qt6 /usr/lib/libadwaitaqt6.so.1
adwaita-qt6 /usr/lib/libadwaitaqt6.so.1.4.2
adwaita-qt6 /usr/lib/libadwaitaqt6priv.so
adwaita-qt6 /usr/lib/libadwaitaqt6priv.so.1
adwaita-qt6 /usr/lib/libadwaitaqt6priv.so.1.4.2
adwaita-qt6 /usr/lib/pkgconfig/
adwaita-qt6 /usr/lib/pkgconfig/adwaita-qt6.pc
adwaita-qt6 /usr/lib/qt6/
adwaita-qt6 /usr/lib/qt6/plugins/
adwaita-qt6 /usr/lib/qt6/plugins/styles/
adwaita-qt6 /usr/lib/qt6/plugins/styles/adwaita.so
adwaita-qt5 /usr/
adwaita-qt5 /usr/include/
adwaita-qt5 /usr/include/AdwaitaQt/
adwaita-qt5 /usr/include/AdwaitaQt/adwaita.h
adwaita-qt5 /usr/include/AdwaitaQt/adwaitacolors.h
adwaita-qt5 /usr/include/AdwaitaQt/adwaitaqt_export.h
adwaita-qt5 /usr/include/AdwaitaQt/adwaitarenderer.h
adwaita-qt5 /usr/lib/
adwaita-qt5 /usr/lib/cmake/
adwaita-qt5 /usr/lib/cmake/AdwaitaQt/
adwaita-qt5 /usr/lib/cmake/AdwaitaQt/AdwaitaQtConfig.cmake
adwaita-qt5 /usr/lib/cmake/AdwaitaQt/AdwaitaQtConfigVersion.cmake
adwaita-qt5 /usr/lib/cmake/AdwaitaQt/AdwaitaQtTargets-noconfig.cmake
adwaita-qt5 /usr/lib/cmake/AdwaitaQt/AdwaitaQtTargets.cmake
adwaita-qt5 /usr/lib/libadwaitaqt.so
adwaita-qt5 /usr/lib/libadwaitaqt.so.1
adwaita-qt5 /usr/lib/libadwaitaqt.so.1.4.2
adwaita-qt5 /usr/lib/libadwaitaqtpriv.so
adwaita-qt5 /usr/lib/libadwaitaqtpriv.so.1
adwaita-qt5 /usr/lib/libadwaitaqtpriv.so.1.4.2
adwaita-qt5 /usr/lib/pkgconfig/
adwaita-qt5 /usr/lib/pkgconfig/adwaita-qt.pc
adwaita-qt5 /usr/lib/qt/
adwaita-qt5 /usr/lib/qt/plugins/
adwaita-qt5 /usr/lib/qt/plugins/styles/
adwaita-qt5 /usr/lib/qt/plugins/styles/adwaita.so

If that's all correct maybe it is a caching or configuration problem (I don't actually know of what exactly that would be in this case, but if the files are there, that is the only reason I can think of for it not finding the theme). Hope this is helpful.

7c3c3b5 adds an option to use the system theme, which is probably what most users on Linux actually want. You can sidestep this issue with that.

@yuki-tsubaki thanks

I've overwritten the environment variable and the error disappeared

My error now corresponds to your previous one.

pymorphy3 is available for RU
pymorphy3 is available for UK
Traceback (most recent call last):
  File "/usr/bin/vocabsieve", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/usr/lib/python3.11/site-packages/vocabsieve/main.py", line 862, in main
    qdarktheme.setup_theme(theme)
  File "/usr/lib/python3.11/site-packages/qdarktheme/_main.py", line 160, in setup_theme
    raise Exception("setup_theme() must be called after instantiation of QApplication.")
Exception: setup_theme() must be called after instantiation of QApplication.

@Cloufish use the master version which contains the fix
The transparent background thing I'm assuming is a bug in qdarktheme.