Files
kefu/deploy/dpi-adaptation-20260916/qt_smoke.py
T
2026-09-21 10:34:06 +08:00

45 lines
2.4 KiB
Python

"""Real Qt WebEngine rendering in an isolated process at each DPI factor."""
from pathlib import Path
import sys,json,os
os.environ['QT_QPA_PLATFORM']='offscreen'
sys.path.insert(0,'C:/wechat_rpa')
from PySide6.QtCore import QTimer,Qt
from PySide6.QtWidgets import QApplication,QMainWindow
from wechat_gui_qt import ConsoleShell,MainWindow
from desktop_window import DesktopWindowBar
QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
app=QApplication([])
host=QMainWindow();host._console_view='auto';host._console_state_pending=False
host._push_console_state=lambda **_:host.console_shell.set_state(json.dumps({'view':'auto','demo':False,'system':{'scale':'自动'}}))
host.setMenuBar(DesktopWindowBar(host));host.console_shell=ConsoleShell(host);host.setCentralWidget(host.console_shell)
host.resize(960,540);host.show()
folder=Path(__file__).resolve().parent/'qa';factor=os.environ.get('QT_SCALE_FACTOR','1')
state={'done':False}
def result(value):
if state['done']:return
try:
data=json.loads(value)
if not data.get('ready'):
QTimer.singleShot(150,check);return
data['qtDpr']=host.devicePixelRatioF();data['factor']=factor
assert abs(data['dpr']-data['qtDpr'])<.01,data
assert abs(data['zoom']-.85)<.001,data
assert data['contentWidth']<=data['mainWidth']+2,data
assert data['titleWidth']>0,data
image=host.grab();data['imageWidth']=image.width();data['logicalWidth']=host.width()
assert abs(image.width()/host.width()-data['qtDpr'])<.01,data
image.save(str(folder/f'qt-{factor}x.png'))
(folder/f'qt-{factor}x.json').write_text(json.dumps(data,indent=2),encoding='utf-8')
print(json.dumps(data),flush=True);state['done']=True;app.exit(0)
except Exception as exc:
print(repr(exc),flush=True);state['done']=True;app.exit(1)
def check():
host.console_shell.page().runJavaScript('''JSON.stringify({ready:!!document.querySelector('h1'),
dpr:devicePixelRatio,zoom:Number(document.getElementById('stage').style.zoom),
titleWidth:document.querySelector('h1')?.getBoundingClientRect().width,
mainWidth:document.getElementById('main').clientWidth,contentWidth:document.getElementById('main').scrollWidth})''',result)
QTimer.singleShot(800,check)
QTimer.singleShot(25000,lambda:app.exit(2) if not state['done'] else None)
raise SystemExit(app.exec())