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.
Related
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.
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.
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.
I was trying to do gui programming in Python.I figured out that PySide is a good framework to start with.As i was running Python 2.7.2 i downloaded PySide 2.7 and tried running a sample app.I got QtCore Library not loaded error.
This is the error that i got..
from PySide import QtCore, QtGui
ImportError: dlopen(/Library/Python/2.7/site-packages/PySide/QtCore.so, 2):
Library not loaded: QtCore.framework/Versions/4/QtCore
Referenced from: /Library/Python/2.7/site-packages/PySide/QtCore.so
Reason: image not found
I googled and found out that many people were facing the same issue and i saw solutions being posted based on exporting DYLD_FRAMEWORK_PATH. I was not able to follow this.
Could anyone please tell me whats the issue and how to fix it!!
Thanks..
You don't mention the OS you are working on, but from the paths in your error message it looks like you're on Mac OSX.
I'm not an expert in PySide at all, but I had the same problem a while ago and I think I know what's going on: The library at /Library/Python/2.7/site-packages/PySide/QtCore.so is the part that makes the Qt Core C++ library available to Python. It is just the wrapper though or some sort of translator between C++ and Python, the actual C++ functionality is elsewhere - and when the Python interpreter tries to load the C++ library that contains that functionality from QtCore.framework/Versions/4/QtCore, if fails to find it, hence the error message.
A quick and dirty way to solve your problem is to create symbolic links from the location where QtCore.so expects the C++ library to where it actually is. For that, you will obviously have to find the C++ library. If you downloaded Qt 4.8.4 as an installer from the Qt Project page, the libraries AFAIK are somewhere in /usr/lib, so you would create a symlink like this:
ln -vis /usr/lib/<insert subfolder>/QtCore.framework /Library/Python/2.7/site-packages/PySide/QtCore.framework
You will have to do this in a similar way for QtGui and any other Qt library you want to use as well. Note that this obviously does not symlink the library itself, but the folder in which QtCore.so expects it.
An alternate way would be to build PySide from the sources (which is what I ended up doing), but that takes longer - and you sound like you just want to get going with Python and Qt.
Have you installed standalone QT package for mac?
Qt for Mac OS X: Download Qt 4.7.4 ftp://ftp.qt-project.org/qt/source/qt-mac-opensource-4.7.4.dmg standalone pyside installation raises same error log for me
I have an image file , as C:/44637landscapes-2007.jpg
I want to load this file using QPixmap using PySide
I tried as following .
pixmap = QPixmap('C:/44637landscapes-2007.jpg')
But documentation says like QPixmap(':/xxxxxx.jpeg') . What does ':' means ?
How do I load an image at 'C:\' ?
EDIT : The problem was with trying to load "JPEG" . It was able to load "PNG" , with no issues. So what else , I need to do to load "JPEG" ?
Thanks
Jijoy
Qt does something a little weird with images. If you are using only pngs you will never have a problem. The handling of other image formats is handled a little differently.
If you take a look in your Qt directory (where all the binaries and source is) you will find a directory called: plugins\imageformats
In that directory there are a ton of dlls (or .so) one for each image format that Qt supports. In your case I presume the one that interests you is qjpeg4.dll
Now, when you run your application and you try to use a jpeg, Qt will try to load this plugin, but if it can't find it, then your image won't show up.
We had a similar problem with ico files. It worked in our development environment because the Qt folder was in our PATH, and therefore it magically found this plugin. However, when we were releasing our software we would distribute only those dlls that we needed, and therefore it didn't work.
After much investigation this is what we did:
Our application is located in a bin/ directory. This is where the exe, and all Qt dlls are put. QtCore4.dll and QtGui4.dll etc...
Under the bin directory we create a subdirectory called 'imageformats'
Put qjpeg4.dll and any other image plugins you might need.
Change the code as follows:
QApplication _App(argc, argv);
// Find the path of your bin directory - whereever your exe resides.
QString _Path = _PathToBin + QString(QDir::separator()) + "imageformats");
// This line is providing a path to Qt telling it to look here for plugins
QApplication::addLibraryPath(_Path);
// Initialize the main form
return _App.exec();
This should make sure that Qt is aware of the jpeg handling plugin, and load your image in the UI.
You can construct an instance of QPixMap by providing the path name of the file. What is the error you get when you try your code ? Change your code to something like:
pixmap = QPixMap(r'C:\filename.jpeg')
The documentation what you are referring to is the way you would load a Qt resource. One can convert an image that is needed by the application to a Qt resource so as to load it in a platform independent manner. For steps to do so refer to The Qt Resource System
documentation.
If you're trying to build your Python code to an executable this covers the issue for .ico files but the process is the same for JPEG:
PyQt/PySide - icon display
If you're having problems when running from your installed interpreter then your path variable probably isn't set correctly, your qt.conf (in the base python dir) has been deleted/changed or you don't have qjpeg4.dll where its supposed to be (<python dir>\Lib\site-packages\PySide\plugins\imageformats).
My qt.conf looks like:
[Paths]
Prefix = C:/Python27/lib/site-packages/PySide
Binaries = .
Plugins = plugins
Translation = translation
C:/Python27/lib/site-packages/PySide is part of my system PATH variable and ahead of any PyQt (or other Qt) directories.