209 lines
6.7 KiB
Python
209 lines
6.7 KiB
Python
# ============================================================
|
||
# vqt6 — Viper 的 Qt6 绑定包
|
||
#
|
||
# 公共 Pythonic API(基于 C 桥接层)
|
||
# 用户用法:
|
||
# import vqt6
|
||
# app = vqt6.QApplication([])
|
||
# win = vqt6.QWidget()
|
||
# win.setWindowTitle("Hello")
|
||
# win.show()
|
||
# app.exec()
|
||
# ============================================================
|
||
|
||
import t
|
||
from stdint import *
|
||
from vqt6._types import *
|
||
from vqt6._qtcore import *
|
||
from vqt6._qtwidgets import *
|
||
from vqt6._qtgui import *
|
||
from vqt6._qtnetwork import *
|
||
|
||
|
||
# ============================================================
|
||
# 高级 API 类(Pythonic 包装,不透明指针)
|
||
# ============================================================
|
||
@t.NoVTable
|
||
class QApplication:
|
||
"""QApplication 包装(不透明指针)"""
|
||
_ptr: QtAppPtr
|
||
|
||
def __init__(self, argv: list[str] | t.CPtr):
|
||
# 把 argv 转换为 argc/argv 数组
|
||
argc: t.CInt = 1
|
||
# Viper 端 argv 是 c_char_p 指针数组
|
||
# 这里仅传空 argv,避免 C ABI 复杂性
|
||
argv_ptr: t.CPtr = t.CPtr(0)
|
||
self._ptr = vqt6_qapplication_create(argc, argv_ptr)
|
||
if self._ptr is None:
|
||
raise RuntimeError("Failed to create QApplication")
|
||
|
||
def exec(self) -> int:
|
||
return vqt6_qapplication_exec(self._ptr)
|
||
|
||
def quit(self):
|
||
vqt6_qapplication_quit(self._ptr)
|
||
|
||
def exit(self, code: int):
|
||
vqt6_qapplication_exit(self._ptr, code)
|
||
|
||
def process_events(self):
|
||
vqt6_qapplication_process_events(self._ptr)
|
||
|
||
def desktop_width(self) -> int:
|
||
return vqt6_qapplication_desktop_width(self._ptr)
|
||
|
||
def desktop_height(self) -> int:
|
||
return vqt6_qapplication_desktop_height(self._ptr)
|
||
|
||
|
||
@t.NoVTable
|
||
class QWidget:
|
||
"""QWidget 包装"""
|
||
_ptr: QtWidgetPtr
|
||
|
||
def __init__(self, parent: "QWidget | None" = None):
|
||
if parent is not None and parent._ptr is not None:
|
||
self._ptr = vqt6_qwidget_create_with_parent(parent._ptr)
|
||
else:
|
||
self._ptr = vqt6_qwidget_create()
|
||
if self._ptr is None:
|
||
raise RuntimeError("Failed to create QWidget")
|
||
|
||
def show(self):
|
||
vqt6_qwidget_show(self._ptr)
|
||
|
||
def hide(self):
|
||
vqt6_qwidget_hide(self._ptr)
|
||
|
||
def close(self) -> int:
|
||
return vqt6_qwidget_close(self._ptr)
|
||
|
||
def setWindowTitle(self, title: str):
|
||
title_qs: QtStringPtr = vqt6_qstring_create(title)
|
||
vqt6_qwidget_set_window_title(self._ptr, title_qs)
|
||
vqt6_qstring_free(title_qs)
|
||
|
||
def windowTitle(self) -> str:
|
||
result: QtStringPtr = vqt6_qwidget_window_title(self._ptr)
|
||
buf: bytes = stdlib.malloc(1024)
|
||
vqt6_qstring_to_utf8(result, buf, 1024)
|
||
s: str = string.strndup(buf, 1024)
|
||
vqt6_qstring_free(result)
|
||
return s
|
||
|
||
def resize(self, w: int, h: int):
|
||
vqt6_qwidget_resize(self._ptr, w, h)
|
||
|
||
def move(self, x: int, y: int):
|
||
vqt6_qwidget_move(self._ptr, x, y)
|
||
|
||
def setGeometry(self, x: int, y: int, w: int, h: int):
|
||
vqt6_qwidget_set_geometry(self._ptr, x, y, w, h)
|
||
|
||
def setEnabled(self, enabled: int):
|
||
vqt6_qwidget_set_enabled(self._ptr, enabled)
|
||
|
||
def setVisible(self, visible: int):
|
||
vqt6_qwidget_set_visible(self._ptr, visible)
|
||
|
||
def isVisible(self) -> int:
|
||
return vqt6_qwidget_is_visible(self._ptr)
|
||
|
||
def setToolTip(self, tip: str):
|
||
tip_qs: QtStringPtr = vqt6_qstring_create(tip)
|
||
vqt6_qwidget_set_tool_tip(self._ptr, tip_qs)
|
||
vqt6_qstring_free(tip_qs)
|
||
|
||
|
||
@t.NoVTable
|
||
class QLabel:
|
||
"""QLabel 包装"""
|
||
_ptr: QtLabelPtr
|
||
|
||
def __init__(self, text: str = "", parent: "QWidget | None" = None):
|
||
text_qs: QtStringPtr = vqt6_qstring_create(text)
|
||
if parent is not None and parent._ptr is not None:
|
||
self._ptr = vqt6_qlabel_create_with_text_parent(text_qs, parent._ptr)
|
||
elif text_qs is not None:
|
||
self._ptr = vqt6_qlabel_create_with_text(text_qs)
|
||
else:
|
||
self._ptr = vqt6_qlabel_create()
|
||
vqt6_qstring_free(text_qs)
|
||
if self._ptr is None:
|
||
raise RuntimeError("Failed to create QLabel")
|
||
|
||
def setText(self, text: str):
|
||
text_qs: QtStringPtr = vqt6_qstring_create(text)
|
||
vqt6_qlabel_set_text(self._ptr, text_qs)
|
||
vqt6_qstring_free(text_qs)
|
||
|
||
def text(self) -> str:
|
||
result: QtStringPtr = vqt6_qlabel_text(self._ptr)
|
||
buf: bytes = stdlib.malloc(1024)
|
||
vqt6_qstring_to_utf8(result, buf, 1024)
|
||
s: str = string.strndup(buf, 1024)
|
||
vqt6_qstring_free(result)
|
||
return s
|
||
|
||
def setAlignment(self, alignment: int):
|
||
vqt6_qlabel_set_alignment(self._ptr, alignment)
|
||
|
||
|
||
@t.NoVTable
|
||
class QPushButton:
|
||
"""QPushButton 包装"""
|
||
_ptr: QtPushButtonPtr
|
||
|
||
def __init__(self, text: str = "", parent: "QWidget | None" = None):
|
||
text_qs: QtStringPtr = vqt6_qstring_create(text)
|
||
if parent is not None and parent._ptr is not None:
|
||
self._ptr = vqt6_qpushbutton_create_with_text_parent(text_qs, parent._ptr)
|
||
elif text_qs is not None:
|
||
self._ptr = vqt6_qpushbutton_create_with_text(text_qs)
|
||
else:
|
||
self._ptr = vqt6_qpushbutton_create()
|
||
vqt6_qstring_free(text_qs)
|
||
if self._ptr is None:
|
||
raise RuntimeError("Failed to create QPushButton")
|
||
|
||
def setText(self, text: str):
|
||
text_qs: QtStringPtr = vqt6_qstring_create(text)
|
||
vqt6_qpushbutton_set_text(self._ptr, text_qs)
|
||
vqt6_qstring_free(text_qs)
|
||
|
||
def text(self) -> str:
|
||
result: QtStringPtr = vqt6_qpushbutton_text(self._ptr)
|
||
buf: bytes = stdlib.malloc(1024)
|
||
vqt6_qstring_to_utf8(result, buf, 1024)
|
||
s: str = string.strndup(buf, 1024)
|
||
vqt6_qstring_free(result)
|
||
return s
|
||
|
||
def click(self):
|
||
vqt6_qpushbutton_click(self._ptr)
|
||
|
||
def setEnabled(self, enabled: int):
|
||
vqt6_qwidget_set_enabled(self._ptr, enabled)
|
||
|
||
|
||
# ============================================================
|
||
# 模块导出
|
||
# ============================================================
|
||
__all__ = [
|
||
# 高层类
|
||
"QApplication", "QWidget", "QLabel", "QPushButton",
|
||
# 类型
|
||
"QtObjectPtr", "QtStringPtr", "QtByteArrayPtr", "QtWidgetPtr", "QtAppPtr",
|
||
"QtLabelPtr", "QtPushButtonPtr",
|
||
"QtColorPtr", "QtFontPtr", "QtPixmapPtr", "QtImagePtr", "QtPainterPtr",
|
||
"QtTcpSocketPtr", "QtHostAddressPtr", "QtNetworkRequestPtr", "QtNetworkReplyPtr",
|
||
# 常量
|
||
"QT_KEY_ESCAPE", "QT_KEY_RETURN", "QT_KEY_A", "QT_KEY_Z",
|
||
"QT_LEFT_BUTTON", "QT_RIGHT_BUTTON", "QT_MID_BUTTON",
|
||
"QT_HORIZONTAL", "QT_VERTICAL",
|
||
"QT_ALIGN_LEFT", "QT_ALIGN_CENTER", "QT_ALIGN_RIGHT",
|
||
"QT_PORT_HTTP", "QT_PORT_HTTPS", "QT_PORT_FTP",
|
||
"QT_OK", "QT_CANCEL", "QT_YES", "QT_NO", "QT_CLOSE",
|
||
]
|