PyQt5 Application Not Inheriting qt5ct Style - python

My PyQt5 application only uses windows or fusion style. It's not inheriting GTK style set in qt5ct.
from PyQt5.QtWidgets import QStyleFactory
print(QStyleFactory.keys())
**['Windows', 'Fusion']**
Things attempted:
Install qt5ct & qt-styleplugins
Set qt5ct to gtk2
My "~/.profile" looks like this:
export QT_QPA_PLATFORMTHEME="gtk2"
export QT_AUTO_SCREEN_SCALE_FACTOR=0
export QT_STYLE_OVERRIDE="gtk2"
export EDITOR=/usr/bin/nano
export GTK2_RC_FILES="$HOME/.gtkrc-2.0"
I have tried including/excluding override with & without quotes, using gtk+, gtk instead of gtk2 as well without success.
Other Non-anaconda based QT based applications such as vlc inherit from the gtk style set. I'm guessing this is because there are two versions of QT (one installed by anaconda and the other by pacman in Arch Linux).
Would anyone have a solution for this? Using fusion/windows in a linux environment is looking quite ugly.
Thanks.

Related

Can't get custom PyQt5 widget plugin to show up in Qt designer (macos)

I would like to make a custom widget plugin for Qt Designer using python (3.7 with pyqt5). Everything should work, but it doesn't show up in Qt Designer.
Here is what I have done so far after much trial and error and a little help from others with similar issues (Qt Designer: could not find custom PyQt widget plugins and Custom QWidgets. How do I build/get the pyqt5 plugin for Qt Designer on Mac?)
I installed Qt Designer (Creator) 5.13 from the Qt website.
I installed SIP (4.9.18) and PyQt5 (5.13.0) from source rather than pip because it was necessary to get the libpyqt5.dylib file that is necessary (no pyqt5-tools for mac). I put this file in the /Users/[user]/Qt/5.13.0/clang_64/plugins/designer directory
I just want to get the setup correct before making my own plugin. So, I downloaded analogclock.py and analogclockplugin.py from https://github.com/baoboa/pyqt5/tree/master/examples/designer/plugins and modified the plugins.py file like this:
from PyQt5.QtCore import QLibraryInfo, QProcess, QProcessEnvironment
# Tell Qt Designer where it can find the directory containing the plugins and
# Python where it can find the widgets.
env = QProcessEnvironment.systemEnvironment()
env.insert('PYQTDESIGNERPATH', '[path to the plugin.py files]/designer_plugins')
env.insert('PYTHONPATH', '[path to the widgets]/designer_widgets')
# Start Designer.
designer = QProcess()
designer.setProcessEnvironment(env)
designer_bin = QLibraryInfo.location(QLibraryInfo.BinariesPath)
designer_bin = '/Users/[user]/Qt/5.13.0/clang_64/bin/Designer.app/Contents/MacOS/Designer'
designer.start(designer_bin)
designer.waitForFinished(-1)
I ran plugins.py. Qt Designer opens correctly, and when I checked the Designer-->About Plugins I see libpyqt5.dylib inside Loaded Plugins. However, the PyAnalogClock widget was not inside of it and the plugin widget was not in the left-side widget box.
I tried to debug by setting environmental variables from the terminal like this:
[user]$ export QT_DEBUG_PLUGINS=1
[user]$ export PYQTDESIGNERPATH='[path to the widgets]/designer_widgets'
[user]$ export PYTHONPATH='[path to the widgets]/designer_widgets'
[user]$ /Users/[user]/Qt/5.13.0/clang_64/bin/Designer.app/Contents/MacOS/Designer
The relevant portion of the output was this:
Found metadata in lib /Users/[user]/Qt/5.13.0/clang_64/plugins/designer/libpyqt5.dylib, metadata=
{
"IID": "org.qt-project.Qt.QDesignerCustomWidgetCollectionInterface",
"archreq": 0,
"className": "PyCustomWidgets",
"debug": false,
"version": 331008
}
loaded library "/Users/[user]/Qt/5.13.0/clang_64/plugins/designer/libpyqt5.dylib"
and toward the end
loaded library "Python.framework/Versions/3.7/Python"
ModuleNotFoundError: No module named 'PyQt5'
ModuleNotFoundError: No module named 'PyQt5'
When I did not export PYQTDESIGNERPATH and PYTHONPATH from the command line, this error disappeared. But of course, then Qt doesn't know where the files are.
In any case, that is my current state. I do not understand why the PyQt5 module cannot be found by Qt Designer or what else I can try to get this to work.
here is an 🤦‍♂️ momemt
So, I found this on the PyQt reference guide which I had somehow overlooked
For a simple but complete and fully documented example of a custom widget that defines new Qt signals, slots and properties, and its plugin, look in the examples/designer/plugins directory of the PyQt5 source package
ok. did that. ran the demo. it works fine. Copied the whole plugins directory somewhere else. still fine. So I must have done something funky naming the folders or something. anyway, now I will just work on making my own plugin. days of frustration because of being dumb. but happy it works.

PyQt5 FileDialog: Show network folders

My system is as follows: python 3.6.0 with PyQt 5.8.1 installed in an anaconda environment on ubuntu 16.10.
I want to show/select network folders (connected via samba) with the PyQt5 FileDialog, but am unable to do so.
In PyQt4 (another environment) it works.
relevant code:
from PyQt5 import QtWidgets
fileDialog=QtWidgets.QFileDialog()
workDirectory = str(fileDialog.getExistingDirectory(caption= "Set Work Directory", directory=defaultWorkDirectory))
fileDialog.deleteLater()
The PyQt5 documentation just leads to the C++ one and there is this about network/local files: "Note: The non-native QFileDialog supports only local files." and "When possible, this static function will use the native file dialog and not a QFileDialog. On platforms which don't support selecting remote files, Qt will allow to select only local files."
I thought, ubuntu supports this (just like in PyQt4).
Is there a way to do this or do i have to settle with PyQt4?
Thanks,
Topsrek
This happens because the gtk3 file dialog hides non-local files by default. (this has to be explicitly enabled in the Qt gtk3 platform implementation, bugreport)
Qt4 doesn’t have gtk3 support so you get a gtk2 file dialog which doesn’t do this.
I didn’t find a way to get Qt5 to use the gtk2 dialog and as you found out yourself the non-native dialog doesn’t support network locations either.
As a workaround you can navigate to already mounted locations in /run/user/(your username)/gvfs/*.
As ugly as this may seem, I suggest using this instead of going back to PyQt4.
In case anyone wants to modify this behavior in Qt5, here is one solution. In qtbase/src/plugins/platformthemes/gtk3/qgtk3dialoghelpers.cpp, change the parameter of gtk_file_chooser_set_local_only:
void QGtk3FileDialogHelper::applyOptions()
{
GtkDialog *gtkDialog = d->gtkDialog();
const QSharedPointer<QFileDialogOptions> &opts = options();
gtk_window_set_title(GTK_WINDOW(gtkDialog), qUtf8Printable(opts->windowTitle()));
//gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(gtkDialog), true);
gtk_file_chooser_set_local_only(GTK_FILE_CHOOSER(gtkDialog), false);
//...
}
Now the GTK 3 file dialog can show the mounted shares. See gtk-file-chooser-set-local-only.

I deleted some files from my computer and now I can't run a script/program. What do I do?

I recently permanently deleted some files from computer. Now I can't run a script/program.
When I open an new terminal at the folder, I get this message...
Last login: Mon Oct 26 07:56:13 on ttys005
Vanessa-Chiangs-MacBook-Air:GTIM 0.8.2 vcchiang$ python3 GTIM.py
Traceback (most recent call last):
File "GTIM.py", line 6, in <module>
from PyQt5 import QtCore, QtGui, QtWidgets
ImportError: cannot import name 'QtCore'
Vanessa-Chiangs-MacBook-Air:GTIM 0.8.2 vcchiang$
What do I do?
You deleted something that is needed by some Python process invoked from your shell. When you invoke GTIM.py from the Terminal, some import is missing. What appears to be missing is all or parts of PyQt5.
You need to reinstall the thing that is needed by the shell startup. It looks like it is failing on some Qt Python bindings, which may either be found in core Python or via some separate library (I'm not sure, but a web search should sort that out).
Now, I did a brief search, and it appears that Python Qt5 is a separate module. Though my guess is that it is not part of the full Mac installer for Python, so you might just need to find the right Python Qt4 installer and run that.
However, if you deleted any of the stuff that came with the OS X system for Python 2 (the version that Apple ships) then you might have to restore from backup. It's going to be tricky to sort out what, exactly, you need from a Time Machine backup, but you can always just restore everything the way it was prior to when you mucked about with the system.
At this point you are going to have to do a little research, learn a little, and incrementally move forward until you know what is missing, what version, and how to replace it. We can help, but it is going to take some back and forth.
However, since we see that you are running Python3, which does not ship with OS X, you might as well just reinstall Python 3.5.0 from here. My guess might be wrong, and it might just replace the QtCore Python module that is missing. It looks like PyQt[4|5] has to be installed separately using a straightforward (but possible confusing, if you have never done it before) installation process.
https://gist.github.com/guillaumevincent/10983814
http://robscurity.blogspot.ca/2015/02/installing-pyqt-on-mac-os-x-yosemite.html
(Using Homebrew and Ruby) http://macappstore.org/pyqt5/
It is possible that this OS X machine has "Fink" or "HomeBrew" or "MacPorts" installed on it, which might make things easier, as in that last link.
Conclusion
This is the line we are concerned with:
from PyQt5 import QtCore, QtGui, QtWidgets
The QtCore Python bindings from the PyQt5 module are missing, and this Python script is failing.
Since it looks like you may have deleted parts of the PyQt5 python module bindings, you probably need to replace that, using one of the methods above. Either directly, using tools like sip, or using a helper like Fink, Homebrew, or MacPorts. It will depend on what is on this machine already (i.e., was PyQt5 installed with Homebrew already? I can't tell you this) and how good you are at figuring out the links I provided.
Your first step should be seeing if any of these commands are found when invoking them at a Terminal:
fink
brew
port
If any of these return anything other than "command not found" (or similar), then you should be able to figure out how to use that command to re-install the PyQt5 Python bindings.

Error: no module named gtk.glade

I researched and tried this for two days now and I cannot get gtk to work on Windows 7 with Python 3.4! Whenever I launch my .py file on Python 3.4 with import gtk, I get No module named gtk! I installed pygobject but it did not help. Even gtk3-demo command works in the windows cmd prompt.
I finally got gtk to import (I think) by copying the GTK directory right to C:\Python34\Lib. But now I have a problem with gtk.glade.
Where is this? Where do I copy it from and to where?
You are possibly using an outdated tutorial, see the current Python GTK 3 tutorial for a more up-to-date reference.
In particular, the way to import GTK has changed in GTK3 to:
from gi.repository import Gtk
And instead of libglade, you would use the newer Gtk.Builder class like so:
ui = Gtk.Builder()
ui.add_from_file("my_glade_file.glade")
(you still develop the UI using Glade, it is only how you access it from your program that has changed).

Tkinter OpenGL context in Python

I need to create an OpenGL context in Tkinker, for using it with PyOpenGL Python module.
Tkinker doesn't natively support OpenGL context, but I found this page on PyOpenGL docs, explaining how to use a wrapper included in the module for this:
http://pyopengl.sourceforge.net/documentation/context/
I tried to run the provided code but I got a message saying TOGL module was not found.
I downloaded the module from http://togl.sourceforge.net/, but couldn't get it to work.
PS. I did the test on Mac OS X, with Python 3.2, using virtualenv.
PyOpenGL provides Python bindings for the Tk OpenGL widget (Togl) but not Togl itself, that is why you had to download it. Now, to install Togl is easy but there isn't a tool ready to perform the task. Since the Python bindings will use Tcl to load the Togl module, the widget needs to live in one of the directories present in Tcl's auto_path, which is where Tcl looks for loading libraries. What you can do is start a Tcl interpreter, tclsh, and check which are these directories by doing puts $auto_path. In my case I copied the directory lib/Togl2.0 (inside the Togl's .tar.gz) to /opt/local/lib/tcl8.5. You can also extend auto_path to look for other directories, but I'm not covering that here.
Then I tested using Python 2.7 on Mac OSX. Doing import OpenGL.Tk tries to load Togl, too bad it fails. The reason is that Togl comes precompiled for i386, since I built Python as a universal binary all I did was run it as arch -i386 python2.7, and now import OpenGL.Tk works.

Categories