QObject.inherits(className) works strange in PyQt5 - python

QObject.inherits(className) does work different in PyQt5 than in PyQt4 and PySide.
from PyQt5 import QtWidgets
#from PySide import QtGui
#from PyQt4 import QtGui
QtWidgets = QtGui
class MyWidget(QtWidgets.QWidget):
pass
app = QtWidgets.QApplication([])
w = MyWidget()
print(w.inherits("MyWidget"))
In PyQt5 it prints False, while in PyQt4 and PySide (uncomment the second or third line and comment the first one) it prints True. Why is that and how to fix it?

I can confirm this behaviour in PyQt-5.7.
It seems to be a bug, because the same problem does not appear in the latest development snapshot (PyQt5_gpl-5.7.1.dev1611251257). The only solution is to wait until PyQt-5.7.1 is released.

Related

how to suppress console output from QWebEngineView errors?

I get the following error if I create a QWebEngineView instance from Python instances in different working directories:
[2452:9872:1108/052617.050:ERROR:cache_util_win.cc(21)] Unable to move the cache: Access is denied. (0x5)
[2452:9872:1108/052617.050:ERROR:cache_util.cc(135)] Unable to move cache folder C:\Users\Adam\AppData\Local\python\QtWebEngine\Default\GPUCache to C:\Users\Adam\AppData\Local\python\QtWebEngine\Default\old_GPUCache_000
[2452:9872:1108/052617.051:ERROR:disk_cache.cc(184)] Unable to create cache
[2452:9872:1108/052617.056:ERROR:shader_disk_cache.cc(606)] Shader Cache Creation failed: -2
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
from PyQt5.QtCore import Qt
app = QtWidgets.QApplication([])
x = QtWebEngineWidgets.QWebEngineView()
x.load(QtCore.QUrl('http://example.com/'))
It seems this is a known issue and will be fixed in QT6: https://bugreports.qt.io/browse/QTBUG-66014
But in the meantime, how can I suppress this message? I tried changing QtCore.qInstallMessageHandler and also x.page().javaScriptConsoleMessage = lambda self, level, msg, line, sourceID: None, neither affected this message.
One possible solution is to raise the level of the chromium log:
import os
from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
os.environ["QTWEBENGINE_CHROMIUM_FLAGS"] = "--enable-logging --log-level=3"
app = QtWidgets.QApplication([])
x = QtWebEngineWidgets.QWebEngineView()
# ...

code stops at initialising self.cam = QCamera()

I am using python3.5, pyqt5 and qt5. My problem may be simple but since i am getting no error and never experienced this problem i am writing this here. Here is my code:
import sys
from PyQt5 import QtCore , QtWidgets, QtGui, QtMultimedia, QtMultimediaWidgets
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
from PyQt5.QtWidgets import QApplication, QPushButton, QMainWindow
from PyQt5.QtMultimedia import QCamera, QCameraInfo, QMediaObject, QCameraViewfinderSettings, QCameraImageCapture
from PyQt5.QtMultimediaWidgets import QCameraViewfinder
class Camera(QObject):
def __init__(self, parent = QObject()):
super(Camera, self).__init__(parent)
print("TROLOLOL3")
self.cam = QCamera()
self.caminfo = QCameraInfo(self.cam)
self.camvfind = QCameraViewfinder()
self.camvfindset = QCameraViewfinderSettings()
self.cammode = self.cam.CaptureMode(2)
self.camimgcap = QCameraImageCapture(self.cam)
if __name__ == '__main__':
print("TROLOLOL1")
app = QtWidgets.QApplication(sys.argv)
print("TROLOLOL2")
cam = Camera()
print("TROLOLOL4")
sys.exit(app.exec_())`
I deleted some irrelevant code for the overview. I tested when the code stops and at self.cam = QCamera() the code just stops, doesnt exit or shows a error. Why does this happen? This code compiled without error on Raspbian but now i am on Ubuntu Mate with Raspberry Pi 3.
If you have questions feel free to ask

snapshoot of widget pyqt5

Hi every one I am traying to get a snapshoot of a widget using pyqt5 I am using this code but I can't create an object of the class QScreen
I get an error :
PyQt5.QtGui.QScreen cannot be instantiated or sub-classed
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QPixmap,QScreen
from PyQt5.QtWidgets import QApplication
from datetime import datetime
date = datetime.now()
filename = date.strftime('%Y-%m-%d_%H-%M-%S.jpg')
app = QApplication(sys.argv)
sc=QtGui.QScreen()
sc.grabWindow(QApplication.desktop().winId()).save(filename, 'jpg')
You can get a reference to the screen with :
sc = app.screens()[0]
This method returns a list of screens, i assume you want the first (with index [0])

PyQt5 QWebEngineView causes blurry/fuzzy scaling issue

When a QWebEngineView is added to the window, it causes strange scaling issues across the entire window. This is seen with the latest version of PyQt5 from pip (PyQt5=5.9, Qt5=5.9.1), Python 3.6, and Windows 10. For example:
from PyQt5 import QtGui, QtCore, QtWidgets
from PyQt5 import QtWebEngineWidgets
#
class WebViewer(QtWebEngineWidgets.QWebEngineView):
def __init__(self, parent=None):
super().__init__(parent)
page = QtWebEngineWidgets.QWebEnginePage(self)
self.setPage(page)
self.setUrl(QtCore.QUrl('http://apple.com'))
#
#
if __name__ == '__main__':
appQT = QtWidgets.QApplication([])
#
main_widget = QtWidgets.QWidget(None)
window_layout = QtWidgets.QVBoxLayout(main_widget)
#
window_layout.addWidget(QtWidgets.QTextEdit("1. abc<br/>2. def<br/>3. ghi", main_widget))
####window_layout.addWidget(WebViewer(main_widget))
#
main_widget.show()
appQT.exec_()
#
Results in:
If I uncomment the window_layout.addWidget(WebViewer(main_widget)) line, I get:
The mouse position also does not correspond with what Qt thinks it's clicking on. Is this a bug in Qt5, or is there some dpi/scaling setting I should change? This is also seen with PyQt5.7.1 and PyQt5.8. This doesn't occur with a QWebView in older versions of PyQt5.

Importing from PySide or PyQt4 [duplicate]

This question already has answers here:
How can you easily select between PyQt or PySide at runtime?
(3 answers)
Closed 8 years ago.
I'd like to use either PyQt4 or PySide for imports, whichever is installed. For some reason though I can't do this:
from PyQt4 import QtGui
from QtGui import QApplication
Instead of the last line, I have to do this, I'm not sure why:
from PyQt4.QtGui import QApplication
That doesn't bother me too much, but it makes it very frustrating importing multiple things using whichever library is installed:
try:
from PyQt4 import QtGui, QtWebKit, QtCore
from PyQt4.QtGui import QApplication, QMainWindow, QFrame, QAction, ...
...
except ImportError:
from PySide import QtGui, QtWebKit, QtCore
from PySide.QtGui import QApplication, QMainWindow, QFrame, QAction, ...
...
It gets pretty repetitive.
Also, this doesn't work, Python won't allow it:
import PyQt4 as SomeQt
from SomeQt import QtGui
So I can't find a good way to cut down on repetition between the PyQt4 and PySide sections. Is there a better way? Is there any harm in just using import *:
try:
from PyQt4 import QtGui, QtWebKit, QtCore
from PyQt4.QtGui import *
...
except ImportError:
from PySide import QtGui, QtWebKit, QtCore
from PySide.QtGui *
...
to at least cut down on the amount of repetition necessary?
I would just do following:
try:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
...
except ImportError:
from PySide.QtCore import *
from PySide.QtGui import *
...
...
QThread()
QApplication()
QWidget()
...
Wild imports are usually discouraged by Python community and best practice. The reasoning is that you are unnecessarily polluting your namespace. Me personally, I do not care for Qt, as every class name is starting with Q, e.g. QSomething. This makes it in my opinion very unlikely to collide with another class names from my or 3rd party modules.
To be safe, you can also do something like:
try:
from PyQt4 import QtCore as Qc
from PyQt4 import QtGui as Qg
...
except ImportError:
from PySide import QtCore as Qc
from PySide import QtGui as Qg
...
...
Qc.QThread()
Qc.QApplication()
Qg.QWidget()
...

Categories

Resources