1 # -----------------------------------------------------------------------------
2 # Copyright © 2014-2015 Colin Duquesnoy
3 # Copyright © 2009- The Spyder Development Team
5 # Licensed under the terms of the MIT License
6 # (see LICENSE.txt for details)
7 # -----------------------------------------------------------------------------
9 """Provides QtCore classes and functions."""
11 from typing
import TYPE_CHECKING
13 from . import PYQT5
, PYQT6
, PYSIDE2
, PYSIDE6
14 from . import QT_VERSION
as _qt_version
16 from ._utils
import possibly_static_exec
, possibly_static_exec_
19 from PyQt5
.QtCore
import *
20 from PyQt5
.QtCore
import pyqtBoundSignal
as SignalInstance
21 from PyQt5
.QtCore
import pyqtProperty
as Property
22 from PyQt5
.QtCore
import pyqtSignal
as Signal
23 from PyQt5
.QtCore
import pyqtSlot
as Slot
26 from PyQt5
.QtCore
import Q_ENUM
as QEnum
29 except ImportError: # fallback for Qt5.9
30 from PyQt5
.QtCore
import Q_ENUMS
as QEnum
33 from PyQt5
.QtCore
import QT_VERSION_STR
as __version__
35 # Those are imported from `import *`
36 del pyqtSignal
, pyqtBoundSignal
, pyqtSlot
, pyqtProperty
, QT_VERSION_STR
39 from PyQt6
import QtCore
40 from PyQt6
.QtCore
import *
41 from PyQt6
.QtCore
import QT_VERSION_STR
as __version__
42 from PyQt6
.QtCore
import pyqtBoundSignal
as SignalInstance
43 from PyQt6
.QtCore
import pyqtEnum
as QEnum
44 from PyQt6
.QtCore
import pyqtProperty
as Property
45 from PyQt6
.QtCore
import pyqtSignal
as Signal
46 from PyQt6
.QtCore
import pyqtSlot
as Slot
49 # Seems like there is an error with sip. Without first
50 # trying to import `PyQt6.QtGui.Qt`, some functions like
51 # `PyQt6.QtCore.Qt.mightBeRichText` are missing.
53 with contextlib
.suppress(ImportError):
54 from PyQt6
.QtGui
import Qt
57 QCoreApplication
.exec_
= lambda *args
, **kwargs
: possibly_static_exec(
62 QEventLoop
.exec_
= lambda self
, *args
, **kwargs
: self
.exec(*args
, **kwargs
)
63 QThread
.exec_
= lambda self
, *args
, **kwargs
: self
.exec(*args
, **kwargs
)
65 # Those are imported from `import *`
75 # Allow unscoped access for enums inside the QtCore module
76 from .enums_compat
import promote_enums
81 # Alias deprecated ItemDataRole enum values removed in Qt6
82 Qt
.BackgroundColorRole
= (
83 Qt
.ItemDataRole
.BackgroundColorRole
85 Qt
.TextColorRole
= Qt
.ItemDataRole
.TextColorRole
= Qt
.ForegroundRole
87 # Alias for MiddleButton removed in PyQt6 but available in PyQt5, PySide2 and PySide6
88 Qt
.MidButton
= Qt
.MiddleButton
90 # Add removed definition for `Qt.ItemFlags` as an alias of `Qt.ItemFlag`
91 # passing as default value 0 in the same way PySide6 6.5+ does.
92 # Note that for PyQt5 and PySide2 those definitions are two different classes
93 # (one is the flag definition and the other the enum definition)
94 Qt
.ItemFlags
= lambda value
=0: Qt
.ItemFlag(value
)
98 from PySide2
.QtCore
import *
100 __version__
= PySide2
.QtCore
.__version
__
102 # Missing QtGui utility functions on Qt
103 if getattr(Qt
, "mightBeRichText", None) is None:
105 from PySide2
.QtGui
import Qt
as guiQt
107 Qt
.mightBeRichText
= guiQt
.mightBeRichText
110 # Fails with PySide2 5.12.0
113 QCoreApplication
.exec = lambda *args
, **kwargs
: possibly_static_exec_(
118 QEventLoop
.exec = lambda self
, *args
, **kwargs
: self
.exec_(*args
, **kwargs
)
119 QThread
.exec = lambda self
, *args
, **kwargs
: self
.exec_(*args
, **kwargs
)
120 QTextStreamManipulator
.exec = lambda self
, *args
, **kwargs
: self
.exec_(
126 import PySide6
.QtCore
127 from PySide6
.QtCore
import *
129 __version__
= PySide6
.QtCore
.__version
__
131 # Missing QtGui utility functions on Qt
132 if getattr(Qt
, "mightBeRichText", None) is None:
133 from PySide6
.QtGui
import Qt
as guiQt
135 Qt
.mightBeRichText
= guiQt
.mightBeRichText
138 # Alias deprecated ItemDataRole enum values removed in Qt6
139 Qt
.BackgroundColorRole
= (
140 Qt
.ItemDataRole
.BackgroundColorRole
141 ) = Qt
.BackgroundRole
142 Qt
.TextColorRole
= Qt
.ItemDataRole
.TextColorRole
= Qt
.ForegroundRole
143 Qt
.MidButton
= Qt
.MiddleButton
145 # Map DeprecationWarning methods
146 QCoreApplication
.exec_
= lambda *args
, **kwargs
: possibly_static_exec(
151 QEventLoop
.exec_
= lambda self
, *args
, **kwargs
: self
.exec(*args
, **kwargs
)
152 QThread
.exec_
= lambda self
, *args
, **kwargs
: self
.exec(*args
, **kwargs
)
153 QTextStreamManipulator
.exec_
= lambda self
, *args
, **kwargs
: self
.exec(
158 # Passing as default value 0 in the same way PySide6 6.3.2 does for the `Qt.ItemFlags` definition.
159 if parse(_qt_version
) > parse("6.3"):
160 Qt
.ItemFlags
= lambda value
=0: Qt
.ItemFlag(value
)
162 # For issue #153 and updated for issue #305
164 QDate
.toPython
= lambda self
, *args
, **kwargs
: self
.toPyDate(
168 QDateTime
.toPython
= lambda self
, *args
, **kwargs
: self
.toPyDateTime(
172 QTime
.toPython
= lambda self
, *args
, **kwargs
: self
.toPyTime(
176 if PYSIDE2
or PYSIDE6
:
177 QDate
.toPyDate
= lambda self
, *args
, **kwargs
: self
.toPython(
181 QDateTime
.toPyDateTime
= lambda self
, *args
, **kwargs
: self
.toPython(
185 QTime
.toPyTime
= lambda self
, *args
, **kwargs
: self
.toPython(
190 # Mirror https://github.com/spyder-ide/qtpy/pull/393
192 QLibraryInfo
.path
= QLibraryInfo
.location
193 QLibraryInfo
.LibraryPath
= QLibraryInfo
.LibraryLocation
195 QLibraryInfo
.location
= QLibraryInfo
.path
196 QLibraryInfo
.LibraryLocation
= QLibraryInfo
.LibraryPath