Skip to content

Commit

Permalink
🔧 build(sip): PyQt5 support wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
892768447 committed Apr 28, 2024
1 parent e943272 commit 2311dfe
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 24 deletions.
8 changes: 1 addition & 7 deletions Test/WigglyWidget/LibWigglyWidget/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ set(CMAKE_INSTALL_DIR "${CMAKE_SOURCE_DIR}/dist")
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets)

add_library(LibWigglyWidget SHARED WigglyWidget_global.h wigglywidget.cpp
wigglywidget.h)
add_library(LibWigglyWidget STATIC wigglywidget.cpp wigglywidget.h)

target_link_libraries(LibWigglyWidget PRIVATE Qt${QT_VERSION_MAJOR}::Widgets)

Expand All @@ -29,8 +28,3 @@ install(
ARCHIVE DESTINATION "${CMAKE_INSTALL_DIR}/lib")

install(FILES wigglywidget.h DESTINATION "${CMAKE_INSTALL_DIR}/include")

message(
NOTICE
"****${Qt5Core_INCLUDE_DIRS} ${Qt5Widgets_INCLUDE_DIRS} ${Qt5Gui_INCLUDE_DIRS}"
)
12 changes: 0 additions & 12 deletions Test/WigglyWidget/LibWigglyWidget/WigglyWidget_global.h

This file was deleted.

14 changes: 13 additions & 1 deletion Test/WigglyWidget/LibWigglyWidget/wigglywidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@
#include <QBasicTimer>
#include <QWidget>

#include "WigglyWidget_global.h"
#ifdef Q_OS_WIN
#include <QtCore/qglobal.h>

#if defined(WIGGLYWIDGET_LIBRARY)
#define WIGGLYWIDGET_EXPORT Q_DECL_EXPORT
#else
#define WIGGLYWIDGET_EXPORT Q_DECL_IMPORT
#endif
#endif

#ifdef Q_OS_WIN
class WIGGLYWIDGET_EXPORT WigglyWidget : public QWidget {
#else
class WigglyWidget : public QWidget {
#endif
Q_OBJECT

public:
Expand Down
45 changes: 45 additions & 0 deletions Test/WigglyWidget/PyQtWrapper/TestWigglyWidget.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Created on 2024/04/26
@author: Irony
@site: https://pyqt.site | https://github.com/PyQt5
@email: [email protected]
@file: TestWigglyWidget.py
@description:
"""

import os
import sys

sys.path.append(
os.path.join(os.path.dirname(os.path.abspath(__file__)), "build/WigglyWidget")
)

from PyQt5.QtWidgets import QApplication, QLineEdit, QVBoxLayout, QWidget
from WigglyWidget import WigglyWidget


class TestWigglyWidget(QWidget):
def __init__(self, *args, **kwargs):
super(TestWigglyWidget, self).__init__(*args, **kwargs)
self._layout = QVBoxLayout(self)
self._lineEdit = QLineEdit(self)
self._wigglyWidget = WigglyWidget(self)
self._layout.addWidget(self._lineEdit)
self._layout.addWidget(self._wigglyWidget)

self._lineEdit.textChanged.connect(self._wigglyWidget.setText)
self._lineEdit.setText("pyqt.site")


if __name__ == "__main__":
import cgitb
import sys

cgitb.enable(format="text")
app = QApplication(sys.argv)
w = TestWigglyWidget()
w.show()
sys.exit(app.exec_())
46 changes: 46 additions & 0 deletions Test/WigglyWidget/PyQtWrapper/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Specify sip v6 as the build system for the package.
[build-system]
requires = ["sip >=5.3, <7", "PyQt-builder >=1.9, <2"]
build-backend = "sipbuild.api"

# Specify the PEP 621 metadata for the project.
[project]
name = "WigglyWidget"
version = "0.1.0"
description = "Python bindings for the WigglyWidget library"
urls.homepage = "https://github.com/PyQt5/PyQt"
dependencies = ["PyQt5 (>=5.15.0, <6.0.0)"]

[[project.authors]]
name = "Irony"
email = "[email protected]"

# Specify a PyQt-based project.
[tool.sip]
project-factory = "pyqtbuild:PyQtProject"

# Specify the PEP 566 metadata for the project.
[tool.sip.metadata]
name = "WigglyWidget"
summary = "Python bindings for the WigglyWidget library"
home-page = "https://github.com/PyQt5/PyQt"
author = "Irony"
author-email = "[email protected]"
requires-dist = "PyQt5 (>=5.15.0, <6.0.0)"

# Configure the project.
[tool.sip.project]
tag-prefix = "WigglyWidget"
sip-include-dirs = [
"/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PyQt5/bindings",
"/usr/local/lib64/python3.6/site-packages/PyQt5/bindings",
"C:/soft/Python311/Lib/site-packages/PyQt5/bindings",
]

# Configure the building of the fib bindings.
[tool.sip.bindings.WigglyWidget]
qmake-QT = ["core", "gui", "widgets"]
headers = ["wigglywidget.h"]
include-dirs = ["../dist/include"]
libraries = ["LibWigglyWidget"]
library-dirs = ["../dist/lib"]
4 changes: 4 additions & 0 deletions Test/WigglyWidget/PyQtWrapper/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PyQt5
sip
PyQt5-sip
PyQt-builder
16 changes: 16 additions & 0 deletions Test/WigglyWidget/PyQtWrapper/sip/WigglyWidget/WigglyWidget.sip
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class WigglyWidget : QWidget
{
%TypeHeaderCode
#include "wigglywidget.h"
%End

public:
WigglyWidget(QWidget *parent /TransferThis/ = 0);

public slots:
void setText(const QString &newText);

protected:
virtual void paintEvent(QPaintEvent *);
virtual void timerEvent(QTimerEvent *);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%Module(name=WigglyWidget, keyword_arguments="Optional", use_limited_api=True)


%Import QtCore/QtCoremod.sip
%Import QtWidgets/QtWidgetsmod.sip

%DefaultSupertype sip.simplewrapper

%Include WigglyWidget.sip
19 changes: 19 additions & 0 deletions Test/WigglyWidget/PySideWrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
cmake_minimum_required(VERSION 3.14)
cmake_policy(VERSION 3.14)

# Enable policy to not use RPATH settings for install_name on macOS.
if(POLICY CMP0068)
cmake_policy(SET CMP0068 NEW)
endif()

# Enable policy to run automoc on generated files.
if(POLICY CMP0071)
cmake_policy(SET CMP0071 NEW)
endif()

project(PySideWrapper)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(BINDINGS_HEADER_FILE "${CMAKE_CURRENT_SOURCE_DIR}/bindings.h")
set(BINDINGS_TYPESYSTEM_FILE "${CMAKE_CURRENT_SOURCE_DIR}/bindings.xml")
set(BINDINGS_OUTPUT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dist")
Expand Down
5 changes: 2 additions & 3 deletions Test/WigglyWidget/PySideWrapper/TestWigglyWidget.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
@description:
"""

import os
import sys


from PySide2.QtWidgets import QApplication, QLineEdit, QVBoxLayout, QWidget
from PySide2.WigglyWidget import WigglyWidget
from WigglyWidget import WigglyWidget


class TestWigglyWidget(QWidget):
def __init__(self, *args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion Test/WigglyWidget/PySideWrapper/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
PySide2
PySide2==5.15.2.1

https://download.qt.io/official_releases/QtForPython/shiboken2-generator/shiboken2_generator-5.15.2.1-5.15.2-cp35.cp36.cp37.cp38.cp39.cp310-none-win_amd64.whl; platform_machine == "x86_64" and platform_system == "Windows"

Expand Down
11 changes: 11 additions & 0 deletions Test/WigglyWidget/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# WigglyWidget

## Build

Windows

1. 使用 `QtCreator` 打开项目 `CMakeLists.txt`,勾选对应的Qt版本。
2.`QtCreator` 中通过 `项目`->`构建`->`构建步骤`->`详情` 里勾选 `all``install`
3. 进入 `PyQtWrapper` 目录,打开`vs cmd`,运行 `python -m pip install -r requirements.txt`
4. `sip-build --verbose --tracing --qmake=你的Qt目录下的qmake.exe路径`,等待编译完成
5. `python TestWigglyWidget.py` 进行测试

0 comments on commit 2311dfe

Please sign in to comment.