From f6014cf9a8e5ae572492a5fa4f54f7288595feff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=81=8D=E5=85=AE=E6=83=9A=E5=85=AE?= <101191390+HIllya51@users.noreply.github.com> Date: Fri, 21 Jun 2024 19:17:21 +0800 Subject: [PATCH] qt6 --- .../LunaTranslator/LunaTranslator_main.py | 16 ++++++++------ .../LunaTranslator/gui/usefulwidget.py | 21 +++++++------------ LunaTranslator/LunaTranslator/qtsymbols.py | 9 +++++++- .../LunaTranslator/rendertext/textbrowser.py | 13 ++---------- .../LunaTranslator/rendertext/webview.py | 14 +++++++++---- LunaTranslator/run_test.bat | 2 ++ 6 files changed, 40 insertions(+), 35 deletions(-) create mode 100644 LunaTranslator/run_test.bat diff --git a/LunaTranslator/LunaTranslator/LunaTranslator_main.py b/LunaTranslator/LunaTranslator/LunaTranslator_main.py index 5a9c6267..1ea4701c 100644 --- a/LunaTranslator/LunaTranslator/LunaTranslator_main.py +++ b/LunaTranslator/LunaTranslator/LunaTranslator_main.py @@ -24,15 +24,19 @@ if __name__ == "__main__": from qtsymbols import * if isqt5: - QApplication.addLibraryPath( - "./LunaTranslator/runtime/PyQt5/Qt5/plugins" - ) # 中文字符下不能自动加载 + # 中文字符下不能自动加载 + QApplication.addLibraryPath("./LunaTranslator/runtime/PyQt5/Qt5/plugins") QApplication.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling) QApplication.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps) - if gobject.testuseqwebengine(): + if gobject.testuseqwebengine(): + # maybe use qwebengine + + QApplication.setAttribute(Qt.ApplicationAttribute.AA_ShareOpenGLContexts) + if not isqt5: + # devtool QApplication.setAttribute( - Qt.ApplicationAttribute.AA_ShareOpenGLContexts - ) # maybe use qwebengine + Qt.ApplicationAttribute.AA_DontCreateNativeWidgetSiblings + ) QApplication.setHighDpiScaleFactorRoundingPolicy( Qt.HighDpiScaleFactorRoundingPolicy.PassThrough diff --git a/LunaTranslator/LunaTranslator/gui/usefulwidget.py b/LunaTranslator/LunaTranslator/gui/usefulwidget.py index 7020956e..99f95fc4 100644 --- a/LunaTranslator/LunaTranslator/gui/usefulwidget.py +++ b/LunaTranslator/LunaTranslator/gui/usefulwidget.py @@ -266,8 +266,8 @@ class MySwitch(QWidget): x + self.sizeHint().height() // 2 + offset, y + self.sizeHint().height() // 2, ), - self.sizeHint().height() * 0.35, - self.sizeHint().height() * 0.35, + int(self.sizeHint().height() * 0.35), + int(self.sizeHint().height() * 0.35), ) def mouseReleaseEvent(self, event) -> None: @@ -475,12 +475,9 @@ class resizableframeless(saveposwindow): super().resizeEvent(e) def mousePressEvent(self, event: QMouseEvent): - if isqt5: - gpos = event.globalPos() - else: - gpos = event.globalPosition().toPoint() if event.button() != Qt.MouseButton.LeftButton: return + gpos = QCursor.pos() self.startxp = gpos - self.pos() self.starty = gpos.y() self.startx = gpos.x() @@ -513,11 +510,7 @@ class resizableframeless(saveposwindow): def mouseMoveEvent(self, event): pos = event.pos() - if isqt5: - gpos = event.globalPos() - else: - gpos = event.globalPosition().toPoint() - + gpos = QCursor.pos() if isinrect(pos, self._corner_youxia): self.setCursor(Qt.CursorShape.SizeFDiagCursor) elif isinrect(pos, self._corner_zuoshang): @@ -1059,8 +1052,10 @@ class QWebWrap(abstractwebview): def __init__(self, p=None) -> None: super().__init__(p) - from PyQt5.QtWebEngineWidgets import QWebEngineView - + if isqt5: + from PyQt5.QtWebEngineWidgets import QWebEngineView + else: + from PyQt6.QtWebEngineWidgets import QWebEngineView self.internal = QWebEngineView(self) # self.internal.page().urlChanged.connect( # lambda qurl: self.on_load.emit(qurl.url()) diff --git a/LunaTranslator/LunaTranslator/qtsymbols.py b/LunaTranslator/LunaTranslator/qtsymbols.py index 59fea0ea..32f241c2 100644 --- a/LunaTranslator/LunaTranslator/qtsymbols.py +++ b/LunaTranslator/LunaTranslator/qtsymbols.py @@ -4,6 +4,9 @@ try: from PyQt5.QtGui import QIconEngine,QIntValidator,QStandardItem,QStandardItemModel,QImageWriter,QIcon,QTextCharFormat,QTextBlockFormat,QResizeEvent,QTextCursor,QFontMetricsF,QMouseEvent,QImage,QPainter,QRegion,QCloseEvent,QFontDatabase,QKeySequence,QPixmap,QCursor,QColor,QFont,QPen,QPainterPath,QBrush,QFontMetrics,QShowEvent,QWheelEvent,QPaintEvent from PyQt5.QtCore import QObject,pyqtSignal,Qt,QSize,QByteArray,QBuffer,QPointF,QPoint,QRect,QEvent,QModelIndex,QTimer,QRectF,QVariantAnimation,QUrl isqt5 = True + class LineHeightTypes: + LineDistanceHeight=QTextBlockFormat.LineHeightTypes.LineDistanceHeight + FixedHeight=QTextBlockFormat.LineHeightTypes.FixedHeight except: #from traceback import print_exc #print_exc() @@ -11,4 +14,8 @@ except: from PyQt6.QtWidgets import QFrame,QListView,QCheckBox,QAbstractItemView,QTextEdit,QTableView,QHeaderView,QColorDialog,QSpinBox,QDoubleSpinBox,QComboBox,QDialogButtonBox,QMainWindow,QMessageBox,QDialog,QGridLayout,QTextBrowser,QGraphicsDropShadowEffect,QWidget,QSizePolicy,QScrollArea,QApplication,QPushButton,QSystemTrayIcon,QPlainTextEdit,QMenu,QFileDialog,QKeySequenceEdit,QLabel,QSpacerItem,QWidgetItem,QLayout,QTextBrowser,QLineEdit,QFormLayout,QSizePolicy,QTabWidget,QTabBar,QSplitter,QListWidget,QListWidgetItem,QHBoxLayout,QVBoxLayout,QSizeGrip,QFontComboBox,QProgressBar,QRadioButton,QButtonGroup,QSlider,QToolTip,QGroupBox from PyQt6.QtGui import QIconEngine,QIntValidator,QAction,QStandardItem,QStandardItemModel,QImageWriter,QIcon,QTextCharFormat,QTextBlockFormat,QResizeEvent,QTextCursor,QFontMetricsF,QMouseEvent,QImage,QPainter,QRegion,QCloseEvent,QFontDatabase,QKeySequence,QPixmap,QCursor,QColor,QFont,QPen,QPainterPath,QBrush,QFontMetrics,QShowEvent,QWheelEvent,QPaintEvent from PyQt6.QtCore import QObject,pyqtSignal,Qt,QSize,QByteArray,QBuffer,QPointF,QPoint,QRect,QEvent,QModelIndex,QTimer,QRectF,QVariantAnimation,QUrl - isqt5 = False \ No newline at end of file + isqt5 = False + + class LineHeightTypes: + LineDistanceHeight=QTextBlockFormat.LineHeightTypes.LineDistanceHeight.value + FixedHeight=QTextBlockFormat.LineHeightTypes.FixedHeight.value \ No newline at end of file diff --git a/LunaTranslator/LunaTranslator/rendertext/textbrowser.py b/LunaTranslator/LunaTranslator/rendertext/textbrowser.py index e1872c7b..c341da65 100644 --- a/LunaTranslator/LunaTranslator/rendertext/textbrowser.py +++ b/LunaTranslator/LunaTranslator/rendertext/textbrowser.py @@ -1,4 +1,3 @@ -from PyQt5.QtWidgets import QWidget from qtsymbols import * from myutils.config import globalconfig from rendertext.somefunctions import dataget @@ -263,11 +262,7 @@ class TextBrowser(QWidget, dataget): for i in range(self.blockcount, self.textbrowser.document().blockCount()): b = self.textbrowser.document().findBlockByNumber(i) tf = b.blockFormat() - if isqt5: - lht = QTextBlockFormat.LineHeightTypes.LineDistanceHeight - else: - lht = 4 - tf.setLineHeight(fh, lht) + tf.setLineHeight(fh, LineHeightTypes.LineDistanceHeight) self.textcursor.setPosition(b.position()) self.textcursor.setBlockFormat(tf) self.textbrowser.setTextCursor(self.textcursor) @@ -552,11 +547,7 @@ class TextBrowser(QWidget, dataget): b = self.textbrowser.document().findBlockByNumber(i) tf = b.blockFormat() - if isqt5: - lht = QTextBlockFormat.LineHeightTypes.FixedHeight - else: - lht = 2 - tf.setLineHeight(fasall + fha, lht) + tf.setLineHeight(fasall + fha, LineHeightTypes.FixedHeight) self.textcursor.setPosition(b.position()) self.textcursor.setBlockFormat(tf) self.textbrowser.setTextCursor(self.textcursor) diff --git a/LunaTranslator/LunaTranslator/rendertext/webview.py b/LunaTranslator/LunaTranslator/rendertext/webview.py index f1bc59d8..403c94bc 100644 --- a/LunaTranslator/LunaTranslator/rendertext/webview.py +++ b/LunaTranslator/LunaTranslator/rendertext/webview.py @@ -1,4 +1,3 @@ -from PyQt5.QtWidgets import QWidget from qtsymbols import * from rendertext.somefunctions import dataget import gobject, uuid, json, os, functools @@ -49,10 +48,17 @@ class TextBrowser(QWidget, dataget): self._qweb_query_word() def _qwmenu(self, pos): - from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView + if isqt5: + from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineView + + web_menu = self.webivewwidget.internal.page().createStandardContextMenu() + else: + from PyQt6.QtWebEngineWidgets import QWebEngineView + from PyQt6.QtWebEngineCore import QWebEnginePage + + web_menu = self.webivewwidget.internal.createStandardContextMenu() loadinspector = QAction("Inspect") - web_menu = self.webivewwidget.internal.page().createStandardContextMenu() if ( self.webivewwidget.internal.page().action( QWebEnginePage.WebAction.InspectElement @@ -60,7 +66,7 @@ class TextBrowser(QWidget, dataget): not in web_menu.actions() ): web_menu.addAction(loadinspector) - action = web_menu.exec_(self.webivewwidget.mapToGlobal(pos)) + action = web_menu.exec(self.webivewwidget.mapToGlobal(pos)) if action == loadinspector: diff --git a/LunaTranslator/run_test.bat b/LunaTranslator/run_test.bat new file mode 100644 index 00000000..5df81311 --- /dev/null +++ b/LunaTranslator/run_test.bat @@ -0,0 +1,2 @@ +python.exe -B LunaTranslator\LunaTranslator_main.py test +pause \ No newline at end of file