Skip to content

Commit bd54b64

Browse files
committed
✨ feat(PyQt): update demo
1 parent b76b1d3 commit bd54b64

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

QLiteHtmlSip/sip/QLiteHtmlWidget/QLiteHtmlWidget.sip

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,29 @@ public:
3232
// void setResourceHandler(const ResourceHandler &handler);
3333
void setResourceHandler(SIP_PYCALLABLE /AllowNone/);
3434
%MethodCode
35-
if (a0 == Py_None || a0 == OnResourceHandler) {
35+
if (a0 == Py_None || a0 == OnResourceHandler)
36+
{
3637
sipCpp->setResourceHandler(nullptr);
37-
} else {
38+
}
39+
else
40+
{
3841
Py_XDECREF(OnResourceHandler);
3942
OnResourceHandler = a0;
4043
Py_INCREF(OnResourceHandler);
41-
sipCpp->setResourceHandler([&](const QUrl &url) {
44+
sipCpp->setResourceHandler([&](const QUrl &url)
45+
{
4246
PyObject *res = sipCallMethod(0, OnResourceHandler, "D", &url, sipType_QUrl);
4347
QByteArray result;
44-
if (res != nullptr) {
48+
if (res != nullptr)
49+
{
4550
PyObject *buf = Py_None;
4651
sipParseResult(0, OnResourceHandler, res, "O", &buf);
47-
if (buf != Py_None && SIPBytes_Check(buf))
52+
if (buf != Py_None && PyBytes_Check(buf))
4853
{
49-
result = QByteArray::fromRawData(SIPBytes_AsString(buf), SIPBytes_Size(buf));
50-
Py_DECREF(buf);
51-
buf = NULL;
54+
result = QByteArray::fromRawData(PyBytes_AsString(buf), PyBytes_Size(buf));
5255
}
56+
Py_DECREF(buf);
57+
buf = NULL;
5358
}
5459
Py_XDECREF(res);
5560
res = NULL;

QLiteHtmlSip/tests/test_markdown.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class MainWindow(QMainWindow):
3434

3535
def __init__(self, *args, **kwargs) -> None:
3636
super().__init__(*args, **kwargs)
37+
self._cpath = ""
3738
self._changeTimer = QTimer(self)
3839
self._changeTimer.setSingleShot(True)
3940
self._changeTimer.timeout.connect(self.onChange)
@@ -51,12 +52,21 @@ def __init__(self, *args, **kwargs) -> None:
5152
self._mainWidget.addWidget(self._textEdit)
5253
self._mainWidget.addWidget(self._htmlWidget)
5354
self.setCentralWidget(self._mainWidget)
54-
self._htmlWidget.setResourceHandler(self.onHandler)
55+
self._htmlWidget.setResourceHandler(self.onResourceHandler)
56+
self._htmlWidget.linkClicked.connect(self.onLinkClicked)
5557

56-
def onHandler(self, url):
57-
print(url)
58+
def onResourceHandler(self, url):
59+
try:
60+
url = url.url().lstrip("/")
61+
print(os.path.join(self._cpath, url))
62+
return open(os.path.join(self._cpath, url), "rb").read()
63+
except Exception as e:
64+
print("onHandler:", e)
5865
return b""
5966

67+
def onLinkClicked(self, url):
68+
print("onLinkClicked:", url)
69+
6070
def sizeHint(self):
6171
return QSize(800, 600)
6272

@@ -66,7 +76,7 @@ def openFile(self):
6676
)
6777
if fileName:
6878
self.setWindowTitle(fileName)
69-
os.chdir(os.path.dirname(fileName))
79+
self._cpath = os.path.dirname(fileName)
7080
try:
7181
with open(fileName, "r", encoding="utf-8") as f:
7282
self._textEdit.setPlainText(f.read())

0 commit comments

Comments
 (0)